2016-03-21 6 views
0

Ich bin neu im Frühjahr. Ich versuche, meinen ersten Webdienst zu erstellen, und ich möchte eine Verbindung zu einer Datenbank herstellen, Daten abrufen und es als JSON zurückgeben.So verbinden Sie von STS (Feder Tool Suite) zu Datenbank (Oracle)

Ich benutze das Federbeispiel https://spring.io/guides/gs/rest-service und ändere es ein wenig, damit ich eine Kriegsdatei bekommen kann.

Mein Server, den die WAR-Datei bereitstellen wird, ist Wildfly 8.2.0.

Ich möchte eine Verbindung zur Oracle 11g-Datenbank herstellen und SQL-Abfragen ausführen. Kannst du mir helfen?

Mein Code ist unten veröffentlicht.

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> 

Vielen Dank im Voraus!

Antwort

Verwandte Themen