一、简介
HttpMessageConverter 报文信息转换器,将请求报文转换为 Java 对象,或将 Java 对象转换为响应报文。
HttpMessageConverter 提供了两个注解和两个类型:@RequestBody,@ResponseBody,RequestEntity 和 ResponseEntity。
二、@RequestBody
@RequestBody 注解可以获取请求体,只需要在控制器方法设置一个形参,使用 @RequestBody 进行标识,当前请求的请求体就会为当前注解所标识的形参赋值,但是如果传输的数据包含中文,就会出现乱码问题,具体如何解决这个问题,后面会说到,如下:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
</body>
<form th:action="@{/testRequestBody}" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit">
</form>
</html>
@Controller
public class RequestController {
@RequestMapping("/testRequestBody")
public String testRequestBody(@RequestBody String requestBody){
System.out.println("requestBody:"+requestBody);
return "success";
}
}
三、RequestEntity
RequestEntity 封装请求报文的一种类型,需要在控制器方法的形参中设置该类型的形参,当前请求的请求报文就会赋值给该形参,可以通过 getHeaders() 获取请求头信息,通过 getBody() 获取请求体信息,如下:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
</body>
<form th:action="@{/testRequestEntity}" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit">
</form>
</html>
@Controller
public class RequestController {
@RequestMapping("/testRequestEntity")
public String testRequestEntity(RequestEntity<String> requestEntity){
System.out.println("requestHeader:"+requestEntity.getHeaders());
System.out.println("requestBody:"+requestEntity.getBody());
return "success";
}
}
四、@ResponseBody
@ResponseBody 用于标识一个控制器方法,可以将该方法的返回值直接作为响应报文的响应体响应到浏览器。
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
</body>
<a th:href="@{/testResponseBody}">使用 @ResponseBody 直接返回内容</a>
</html>
@Controller
public class RequestController {
@RequestMapping("/testResponseBody")
@ResponseBody
public String testResponseBody(){
return "I am success";
}
}
五、SpringMVC 处理 json
5.1 返回实体对象
我们来尝试一下,将一个对象返回给浏览器,看下是什么效果,如下:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
</body>
<a th:href="@{/testResponseBodyUser}">使用 testResponseBodyUser 直接返回内容</a>
</html>
public class User {
private Integer id;
private String username;
private String password;
private int age;
private String sex;
// setter、getter、有参构造、无参构造
}
@Controller
public class RequestController {
@RequestMapping("/testResponseBodyUser")
@ResponseBody
public User testResponseBodyUser(){
return new User(10001,"admin","123456",20,"男");
}
}
可以看到,发生了服务器异常,报错的原因是浏览器只能接收字符串类型的数据,无法识别返回给它的对象类型的数据,需要将对象转换成 JSON 即可。
5.2 返回 JSON
1、导入 jackson 的依赖
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.1</version>
</dependency>
2、在 SpringMVC 的核心配置文件中开启 mvc 的注解驱动,此时在 HandlerAdaptor 中会自动装配一个消息转换器:MappingJackson2HttpMessageConverter,可以将响应到浏览器的 Java 对象转换为 Json 格式的字符串,如下:
<mvc:annotation-driven />
3、在处理器方法上使用 @ResponseBody 注解进行标识
4、将 Java 对象直接作为控制器方法的返回值返回,就会自动转换为 Json 格式的字符串,如下:
@Controller
public class RequestController {
@RequestMapping("/testResponseBodyUser")
@ResponseBody
public User testResponseBodyUser(){
return new User(10001,"admin","123456",20,"男");
}
}
5、浏览器的页面显示效果如下:
六、SpringMVC 处理 ajax
1、添加请求超链接,并通过 vue 和 axios 处理点击事件,如下:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<div id="app">
<a @click="testAxios" th:href="@{/testAxios}">SpringMVC处理ajax</a>
</div>
<script type="text/javascript" th:src="@{/static/js/vue.js}"></script>
<script type="text/javascript" th:src="@{/static/js/axios.min.js}"></script>
<script type="text/javascript">
new Vue({
el:"#app",
methods:{
testAxios:function (event) {
axios({
method:"post",
url:event.target.href,
params:{
username:"admin",
password:"123456"
}
}).then(function (response) {
alert(response.data);
});
event.preventDefault();
}
}
});
</script>
</body>
</html>
2、添加控制器方法,如下:
@Controller
public class RequestController {
@RequestMapping("/testAxios")
@ResponseBody
public String testAxios(String username, String password){
System.out.println(username+","+password);
return "hello,axios";
}
}
3、界面效果如下:
七、@RestController 注解
@RestController 注解是 springMVC 提供的一个复合注解,标识在控制器的类上,就相当于为类添加了 @Controller 注解,并且为其中的每个方法添加了 @ResponseBody 注解,即下面两个类的作用是一样的。
@RestController
public class RequestController {
@RequestMapping("/testWorld")
public String testWorld(String username, String password){
System.out.println(username+","+password);
return "hello,world";
}
@RequestMapping("/testAxios")
public String testAxios(String username, String password){
System.out.println(username+","+password);
return "hello,axios";
}
}
@Controller
public class RequestController {
@RequestMapping("/testWorld")
@ResponseBody
public String testWorld(String username, String password){
System.out.println(username+","+password);
return "hello,world";
}
@RequestMapping("/testAxios")
@ResponseBody
public String testAxios(String username, String password){
System.out.println(username+","+password);
return "hello,axios";
}
}
八、ResponseEntity
ResponseEntity 用于控制器方法的返回值类型,该控制器方法的返回值就是响应到浏览器的响应报文,常见的使用场景就是文件的上传和下载。