Building a RESTful Web Service
最终的效果
你会创建一个能处理http://localhost:8080/greeting
的get请求的web服务,它会回复一个如图所示的json。
你还可以加入一个自定义的name参数,回复将会如图所示:
注意到id每次访问都会递增。
你需要的
- 大概10分钟时间
- 一个你喜欢的编辑器或者java IDE
- JDK1.8以上
- Maven 3.2以上
如何完成这个简单的项目
用Spring Initializr创建一个项目
注意:需要Spring Web依赖
pom
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.0.RELEASE</version> <relativePath/> </parent> <groupId>xyz.eternal.fire</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description>
<properties> <java.version>14</java.version> </properties>
<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> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies>
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
</project>
|
创建一个Greeting类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package xyz.eternal.fire.demo;
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; } }
|
SpringWeb内置了Jackson JSON,这个Greeting类最终会被自动转化成JSON。
创建一个Resource Controller
在Spring里,HTTP请求会被标记有Controller的类处理。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package xyz.eternal.fire.demo;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.atomic.AtomicLong;
@RestController public class GreetingController { private static final String template = "Hello, %s!"; private final AtomicLong counter = new AtomicLong();
@GetMapping("/greeting") public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) { return new Greeting(counter.incrementAndGet(), String.format(template, name)); } }
|
@GetMapping注解保证了对/greeting的get请求会被greeting方法处理。
@RequestParam注解可以把url里的name参数注入到方法参数String name里,如果没找到这个参数,将使用默认的“world”。
greeting方法内部返回了一个Greeting类的对象,这个对象将会被自动转成JSON。
说明
传统MVC controller和RESTful web service controller的区别
区别主要体现在HTTP RESPONSE的建立上。传统的MVC controller要利用模板引擎把数据渲染成HTML,然而RESTful web service controller直接返回一个Greeting对象,这个对象将会被转换成JSON然后直接写到Response里。
@RestController注解
相当于@Controller+@ResponseBody
自动生成的Application类
1 2 3 4 5 6
| @SpringBootApplication public class RESTfulApplication { public static void main(String[] args) { SpringApplication.run(RESTfulApplication.class, args); } }
|
main方法里只有一行,没啥好说的。
@SpringBootApplication注解
相当于@Configuration+@EnableAutoConfiguration+@ComponentScan
@Configuration
说明这是一个配置类,里面可能有很多Bean。
@EnableAutoConfiguration
告诉Spring Boot自动添加位于classpath下的Bean,设置。
@ComponentScan
告诉Spring 去寻找当前package下的Components。
运行
可以使用maven命令:java -jar target/gs-rest-service-0.1.0.jar生成jar包运行。
可以在IDE里直接运行。