[該教程翻譯自Spring官方,並進行適當刪減。]
你將搭建的
你將搭建一個可以接受Http Get 請求的web service,
- http://localhost:8080/greeting
- {"id":1,"content":"Hello, World!"}
一個文本編輯器,JDK1.6及以上,Maven 3.0+或者Gradle 1.11+。(本文將使用Maven)
下面是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>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>1.1.5.RELEASE</version>
- </parent>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- </dependencies>
- <properties>
- <start-class>hello.Application</start-class>
- </properties>
- <build>
- <plugins>
- <plugin>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>2.3.2</version>
- </plugin>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
- <repositories>
- <repository>
- <id>spring-releases</id>
- <url>http://repo.spring.io/libs-release</url>
- </repository>
- </repositories>
- <pluginRepositories>
- <pluginRepository>
- <id>spring-releases</id>
- <url>http://repo.spring.io/libs-release</url>
- </pluginRepository>
- </pluginRepositories>
- </project>
新建項目
首先你新建一個符合Maven規範的目錄結構, src/main/java/hello
- └── src
- └── main
- └── java
- └── hello
在hello目錄下,新建一個Greeting類作爲“問候”的javabean。代碼清單如下:
- 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;
- }
- }
在下面的步驟你將會看到,spring將使用Jackson JSON自動將Greeting的對象轉成JSON字符串。
接下來是新建一個類做控制器。
在Spring構建一個Restful風格的web service,需要一個處理請求的控制器。
同樣在hello目錄,代碼清單如下:
- package hello;
- import java.util.concurrent.atomic.AtomicLong;
- import org.springframework.web.bind.annotation.RequestMapping;
- 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("/greeting")
- public Greeting greeting(@RequestParam(value="name", required=false, defaultValue="World") String name) {
- return new Greeting(counter.incrementAndGet(),
- String.format(template, name));
- }
- }
寫過Spring MVC的對@RequestMapping和@RequestParam等一定不陌生。
傳統的MVC控制器和Restful 風格Web Service控制器的主要區別是,產生HTTP 響應的方式不同。不同於依賴視圖技術將數據解析成HTML,這個控制器填充並返回一個對象。
這段代碼使用Spring4.o新的註解:RestController,表明該類的每個方法返回對象而不是視圖。它實際就是@Controller和@ResponseBody混合使用的簡寫方法。
Greeting對象會被轉換成JSON字符串,這得益於Spring 的HTTP消息轉換支持,你不必人工處理。由於Jackson2在classpath裏,Spring的MappingJackson2HttpMessageConverter會自動完成這一工作。
儘管你可以將這個服務打包成傳統的WAR文件部署到應用服務器,但下面將會創建一個獨立的應用,使用main方法可以將所有東西打包到一個可執行的jar文件。並且,你將使用Sping對內嵌Tomcat servlet容器的支持,作爲HTPP 運行時環境,沒必要部署成一個tomcat外部實例。
- package hello;
- import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
- import org.springframework.boot.SpringApplication;
- import org.springframework.context.annotation.ComponentScan;
- @ComponentScan
- @EnableAutoConfiguration
- public class Application {
- public static void main(String[] args) {
- SpringApplication.run(Application.class, args);
- }
- }
main方法使用了SpringApplication工具類。這將告訴Spring去讀取Application的元信息,並在Spring的應用上下文作爲一個組件被管理。
@ComponentScan註解告訴Spring在hello包下遍歷帶有@Component註解的類。這將保證Spring能找到並註冊GreetingController,因爲它被@RestController標記,這也是@Component的一種。
@EnableAutoConfiguration註解會基於你的類加載路徑的內容切換合理的默認行爲。比如,因爲應用要依賴內嵌版本的tomcat(tomcat-embed-core.jar),所有一個tomcat服務器會被啓動並代替你進行合理的配置。再比如,因爲應用要依賴Spring 的 MVC框架(spring-webmvc.jar),一個Spring MVC的DispatcherServlet將被配置並註冊,不再需要web.xml文件。自動配置是很強大靈活的機制。
使用maven可以這樣執行,
- mvn clean package
然後,
- java -jar target/gs-rest-service-0.1.0.jar
運行後無意外會出現Spring Boot結果:
在瀏覽器測試:
帶參數的測試:
轉自:http://blog.csdn.net/chenloveit/article/details/38779149?utm_source=tuicool