簡述
這篇文章簡述了使用Spring Cloud創建一個簡單的”hello world”的Restful Web Service流程。
你將構建什麼
你將構建一個接收HTTP GET請求的服務:
http://localhost:8080/greeting
並且在請求時返回一個JSON格式的字符串:
{"id":1,"content":"Hello, World!"}
你也可以指定name
參數:
http://localhost:8080/greeting?name=User
name
參數會覆蓋默認的”World”值,並且體現在返回值:
{"id":1,"content":"Hello, User!"}
你需要什麼
1、jdk 1.8 及以上
2、Gradle 2.3+ or Maven 3.0+
3、你也可以在你的IDE中直接導入代碼:
1)、Spring Tool Suite(STS)
2)、IntelliJ IDEA
Maven支持
<?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.7.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>
構建測試工程
Create a resource representation class
package com.zooper.demo;
public class Greeting {
private long id;
private String name;
public Greeting(long id, String name) {
super();
this.id = id;
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Create a resource controller
在Spring構建RESTful web services中,Http請求被使用爲一個controller.這些組件很容易被@RestController
註解識別,並且在GreetingController
中使用GET
方式請求/greeting
返
回了一個Greeting
對象:
package com.zooper.demo;
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));
}
}
Make the application executable
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
標記的class作爲一個bean資源在應用環境中定義。@EnableAutoConfiguration
告訴Spring Boot 根據classpath配置、其它beans和各種屬性配置去添加beans。- 通常你會在一個Spring MVC應用中使用
@EnableWebMvc
,但是Spring Boot 在classpath中看到spring-webmvc時會自動使用該註解。 @ComponentScan
告訴Spring在當前包中查詢其它的部件、配置和服務,並允許它去找到controllers.
在main()方法中調用了SpringApplication.run()方法去開始一個應用。
執行了以上操作後,你就可以訪問:
http://localhost:8080/greeting
或者指定name值:
http://localhost:8080/greeting?name=zhangsan
返回的是一個json格式的字符串:
{"id":3,"name":"Hello, zhangsan!"}