官方的第一个列子,Building a RESTful Web Service
1.目标
建立一个服务,接收HTTP的GET请求,格式如http://localhost:8080/greeting。
返回的结果为
{"id":1,"content":"Hello, World!"}
可以在查询字符串路径后面增加参数
http://localhost:8080/greeting?name=User
返回的结果为
{"id":1,"content":"Hello, User!"}
2.代码
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>spring-guide</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>rest-service</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</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>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Greeing类
package com.example.restservice;
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;
}
}
Greeting为资源类,存储id和data数据
GreetingController类
package com.example.restservice;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.GetMapping;
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();
@GetMapping("/greeting")
public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
return new Greeting(counter.incrementAndGet(), String.format(template, name));
}
}
@RestController,作用建立RESTful的web服务和HTTP请求。
@GetMapping,确保HTTP的GET请求/greeting,对应调用greeting()方法,返回新的Greeting类对象。
@RequestParam,变量接收查询字符串的相应的变量值。(查询字符串为问号后面部分)
传统的MVC controller和RESTful web服务区别在于,MVC controller是依赖视图技术渲染Greeting数据到html上。RESTful web的返回值是JSON直接作为HTTP response。@RestController包括@Controller和@ResponseBody。
RestServiceApplication
package com.example.restservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RestServiceApplication {
public static void main(String [] args){
SpringApplication.run(RestServiceApplication.class);
}
}
@SpringBootApplication包括:
@Configuration,将类标记为应用程序上下文的bean。
@EnableAutoConfiguration,告诉SpringBoot将bean添加到classpath
@ComponentScan,告诉Spring在com/example中查找components, configurations和services,让他们找到controller。
3.运行
访问一次http://localhost:8080/greeting,返回
{"id":1,"content":"Hello, World!"}
再次访问 http://localhost:8080/greeting,返回
{"id":2,"content":"Hello, User!"}
id从1到2, 同一个GreetingController实例,跨过了多个请求,属性
counter会在每次调用时增加。
备注:
什么是RESTful
RESTful架构是对MVC架构改进后所形成的一种架构,通过使用事先定义好的接口与不同的服务联系起来。在RESTful架构中,浏览器使用POST,DELETE,PUT和GET四种请求方式分别对指定的URL资源进行增删改查操作。因此,RESTful是通过URI实现对资源的管理及访问,具有扩展性强、结构清晰的特点。
引用自 https://baike.baidu.com/item/RESTful/4406165?fr=aladdin