2016-03-21 73 views
0

我是在春天新的。我正在嘗試構建我的第一個Web服務,並且我想連接到數據庫,獲取數據並將其作爲JSON返回。如何從STS(彈簧工具套件)連接到數據庫(oracle)

我使用彈簧的例子https://spring.io/guides/gs/rest-service,我改變了一下,所以我可以得到一個戰爭文件。

戰爭文件將部署的我的服務器是Wildfly 8.2.0。

我想連接到oracle 11g數據庫並運行SQL查詢。你可以幫我嗎?

我的代碼張貼如下。

My project format in eclipse

Application.java:

package hello; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.boot.builder.SpringApplicationBuilder; 
import org.springframework.boot.context.web.SpringBootServletInitializer; 

@SpringBootApplication 
public class Application extends SpringBootServletInitializer { 


@Override 
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 
    return application.sources(Application.class); 
} 


public static void main(String[] args) throws Exception { 
    SpringApplication.run(Application.class, args); 
} 


} 

Greeting.java:

package hello; 

public class Greeting { 

private final long id; 
private final String content; 

public Greeting(long id, String content) { 
    this.id = id; 
    this.content = content; 
} 

public long getId() { 
    return id; 
} 

public String getContent() { 
    return content; 
} 
} 

GreetingController.java:

import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RequestParam; 
import org.springframework.web.bind.annotation.RestController; 


@RestController 
public class GreetingController { 


private static final String template = "Hello, %s!"; 
private final AtomicLong counter = new AtomicLong(); 


@RequestMapping(value="/greeting",method=RequestMethod.GET) 
public List<Greeting> greeting(@RequestParam(value="name", defaultValue="World") String name, 
           @RequestParam(value="content", defaultValue="World") String content,HttpServletResponse response) { 
    List<Greeting> list_greet = new ArrayList<Greeting>(); 
    list_greet.add(new Greeting(counter.incrementAndGet(), 
           String.format(template, name))); 
    list_greet.add(new Greeting(counter.incrementAndGet(), 
           String.format(template, content))); 
    list_greet.add(new Greeting(counter.incrementAndGet(), 
           String.format(template, name))); 
    list_greet.add(new Greeting(counter.incrementAndGet(), 
           String.format(template, name))); 
    //to have webservice work,beacause cors cut it out. 
    response.setHeader("Access-Control-Allow-Origin", "*"); 
    response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE"); 
    response.setHeader("Access-Control-Max-Age", "3600"); 
    response.setHeader("Access-Control-Allow-Headers", "x-requested-with"); 


    return list_greet; 
} 
} 

的pom.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
<modelVersion>4.0.0</modelVersion> 

<groupId>org.springframework</groupId> 
<artifactId>gs-rest-service</artifactId> 
<version>0.1.0</version> 
<packaging>war</packaging> 

<parent> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-parent</artifactId> 
    <version>1.3.3.RELEASE</version> 
</parent> 

<dependencies> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-web</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-tomcat</artifactId> 
     <scope>provided</scope> 
    </dependency> 
</dependencies> 

<properties> 
    <java.version>1.8</java.version> 
</properties> 


<build> 
    <plugins> 
     <plugin> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-maven-plugin</artifactId> 
     </plugin> 
    </plugins> 
</build> 

<repositories> 
    <repository> 
     <id>spring-releases</id> 
     <url>https://repo.spring.io/libs-release</url> 
    </repository> 
</repositories> 
<pluginRepositories> 
    <pluginRepository> 
     <id>spring-releases</id> 
     <url>https://repo.spring.io/libs-release</url> 
    </pluginRepository> 
</pluginRepositories> 
</project> 

在此先感謝!

回答