HttpMessageConverter(报文信息转换器),将请求报文转换为Java对象,或将Java对象转换为响应报文,HttpMessageConverter提供了两个注解和两个类型:@RequestBody、@ResponseBody、RequestEntity、ResponseEntity。
1.@RequestBody
@RequestBody可以获取请求体,需要在控制器方法设置一个形参,使用@RequestBody进行标识,当前请求的请求体就会为当前注解所标识的形参赋值.
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>hello world!</h1>
<!--新增请求-->
<form th:action="@{/testRequestBody}" method="post">
id:<input type="number" name="id"><br>
用户名:<input type="text" name="userName"><br>
密码:<input type="text" name="passWord"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
@PostMapping("/testRequestBody")
public String test1(@RequestBody String requestBody){
System.out.println("requestBody"+requestBody);
return "success";
}
测试结果
2.@ResponseBody
@ResponseBody用于标识一个控制器方法,可以将该方法的返回值直接作为响应报文的响应体响应到浏览器.
@ResponseBody
@GetMapping("/testResponseBody")
public String test2(){
return "hello response body!";
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>hello world!</h1>
<a th:href="@{/testResponseBody}">测试responseBody</a>
</body>
</html>
测试结果
3.@RestController注解
@RestController注解是springMVC提供的一个复合注解,标识在控制器的类上,就相当于为类添加了@Controller注解,并且为其中的每个方法添加了@ResponseBody注解
@RestController
public class LoginController {
@GetMapping("/testResponseBody")
public String test2(){
return "hello response body!";
}
}
4.RequestEntity
RequestEntity封装请求报文的一种类型,需要在控制器方法的形参中设置该类型的形参,当前请求的请求报文就会赋值给该形参,可以通过getHeaders()获取请求头信息,通过getBody()获取请求体信息.
@GetMapping("/testRequestEntity")
public String test3(RequestEntity<String> requestEntity){
System.out.println("requestEntity="+requestEntity.toString());
return "success";
}
输出结果
requestEntity=
<GET http://localhost:8080/springMVC/testRequestEntity,
[
host:"localhost:8080",
connection:"keep-alive",
sec-ch-ua:""Chromium";v="92", " Not A;Brand";v="99", "Google Chrome";v="92"",
sec-ch-ua-mobile:"?0",
upgrade-insecure-requests:"1",
user-agent:"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36", accept:"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
sec-fetch-site:"same-origin",
sec-fetch-mode:"navigate",
sec-fetch-user:"?1",
sec-fetch-dest:"document",
referer:"http://localhost:8080/springMVC/index",
accept-encoding:"gzip, deflate, br",
accept-language:"zh-CN,zh;q=0.9",
cookie:"Webstorm-7effe112=8927e87f-4a0c-489f-9f3a-ff41f7add87f; p_h5_u=92A83FE7-6B51-4CB4-BB49-C371C61597E4; selectedStreamLevel=OD"
]>
5.ResponseEntity
ResponseEntity用于控制器方法的返回值类型,该控制器方法的返回值就是响应到浏览器的响应报文
可以看到ResponseEntity是用作返回值类型, 具体文件下载功能代码请见下篇博客。
6.SpringMVC处理json
@ResponseBody处理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注解进行标识,将Java对象直接作为控制器方法的返回值返回,就会自动转换为Json格式的字符串。
@PostMapping("/testResponseBody")
@ResponseBody
public User test1(User user){
System.out.println("user:"+user);
return user;
}
测试结果
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>hello world!</h1>
<!--新增请求-->
<form th:action="@{/testResponseBody}" method="post">
id:<input type="number" name="id"><br>
用户名:<input type="text" name="userName"><br>
密码:<input type="text" name="passWord"><br>
<input type="submit" value="提交">
</form>
</body>
</html>