【Spring官方指南學習】Spring構建一個 restful web service
【Spring學習筆記,稍作記錄,主要參考Spring官網 http://spring.io/guides】
構建一個restful web service
理解如何用spring去構建一個簡單的 “hello world” Restful web service.
【即將建立】
-
建立HTTP GET的request請求:
http://localhost:8080/greeting返回一個JSON格式的響應:
{“id”: 1, “content”: “Hello, World!”} -
定製響應的內容,在請求地址裡面加入一個可選的name引數:
在返回的響應內容中,name引數將會替代掉預設的”World”:
{“id”:1, “content”: “Hello, User!”}
【環境需要】
- 大約15min
- JDK1.8+
- Maven 3.0+
- IDE:eclipse或者idea, 此處推薦STS(Spring Tool Suite)
【實現步驟】
-
構建工程
2. Maven, pom檔案
<?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.5.1.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-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.jayway.jsonpath</groupId> <artifactId>json-path</artifactId> <scope>test</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>
3. 建立model、controller類
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;
}
}
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", defaultValue="World") String name) {
return new Greeting(counter.incrementAndGet(),
String.format(template, name));
}
}
在完成這一步之後,其實這個restful service就可以運行了,按照老的方式就是打成war 包,釋出 到伺服器上就可以了。
但是對月開發來說,釋出步驟太繁瑣了,下面Spring boot裡面提供了一個更便捷的方式,非常適合 開發的時候除錯介面使用, 個人比較喜歡O(∩_∩)O~
4. @SpringBootApplication Spring註解
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@SpringBootApplication是
@Configuration
@EnableAutoConfiguration
@ComponentScan
的集合,功能集大成者~
main()方法中直接使用Spring boot的SpringApplication.run()方法就可以直接執行整個工程 了。
5. 【測試】
執行工程,三種方法
1. 打成jar,命令列執行 java -jar jar名字
2. 打成war,釋出到伺服器
3. Spring boot
這裡使用Spring boot,main()方法直接執行。
釋出之後,可訪問如下,