1.restful web service 理解
1.我们以前MVC的返回数据都是通过一个View去返回一些数据,现在我们只需要返回一个对象等;
2.可能觉得很抽象,我们下面通过一个案例去理解
2.案例
2.1.引入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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
</parent>
<groupId>com.gaoxinfu.demo.spring.web</groupId>
<artifactId>demo-spring-web</artifactId>
<version>1.0-SNAPSHOT</version>
<name>demo-spring-web</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!--web container -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--bootstrap.yml load -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
这里其实主要是parent和web的引入
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2.2.创建controller
package com.gaoxinfu.demo.spring.web.restservice;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @Description:
* @Author: gaoxinfu
* @Date: 2021-01-12 09:31
*/
@RestController
@RequestMapping(path = "/greet")
public class GreetingController {
private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();
@GetMapping("/hello")
public Greeting hello(@RequestParam(value = "name", defaultValue = "World") String name) {
return new Greeting(counter.incrementAndGet(), String.format(template, name));
}
}
/**
* @Description:
* @Author: gaoxinfu
* @Date: 2021-01-12 09:30
*/
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;
}
}
2.3.解析
2.3.1.@GetMapping
1.@GetMapping确保Http Get类型的请求/greet/hello能够匹配到方法hello()
2.RequestParam 可以参考下面的地址介绍,不再赘述
https://blog.csdn.net/u014636209/article/details/112478682
3.当然HTTP请求类型,不仅仅有Get,还有Post等等请求类型
2.3.2.@RestController
1.对于返回值,我们定义了一个内部对象类,去封装了其属性(id和content)
2.从上面的返回值 new Greeting(),我们可以发现,传统的MVC Controller和Restful Web Controller的区别很明显的我们发现,
其实就是返回值Response Body的创建方式的区别;
传统MVC Controller:通过通过View技术去将greeting数据渲染成html
(relying on a view technology to perform server-side rendering of the greeting data to HTML)
Restful Web Controller:通过返回个Greeting对象,对象将被直接通过json的方式写入到Http的reponse之中;
3.@RestController = @Controller + @ResponseBody
@RestController注解作为类上面,标示这个每一个方法可以返回一个domain object 对象替代传统mvc的view视图;
4.由于Spring的Http 消息message转换器的支持,我们不需要手动的做任何的转换,就可以将Greeting对象转换为JSON;
在classpath下Jackson 2 会被加载,Spring的 MappingJackson2HttpMessageConverter自动的被选择使用,将the Greeting转换为JSON