HttpMessageConverter是报文请求信息转换器,其提供了将请求信息转换文Java对象以及将Java对象转换为相应信息的方法:
@RequestBody、@ResponseBody、RequestEntity、ResponseEntity
带Request的是将请求转换为Java对象,带Response的是将Java对象转换为相应信息。
@RequestBody注解可以将请求信息赋值给String的形参,例如下面这样的页面的形式
<form th:action="@{/testRequestBody}" method="post">
<input type="text" name="username"/>
<input type="password" name="password"/>
<input type="submit" name="TestRequestBody"/>
form>
后台会是这样
@RequestMapping(value = "/testRequestBody")
// 该形参前的@RequestBody注解会将Request信息注入给形参
public String testRequestBody(@RequestBody String RequestBody) {
System.out.println(RequestBody); //username=admin&password=asdasdsa&TestRequestBody=%E6%8F%90%E4%BA%A4
return "success";
}
RequestEntity是一个类,也可以获取参数信息(请求头和请求体)
@RequestMapping(value = "/testRequestEntity")
public String testRequestEntity(RequestEntity<String> requestEntity) {
System.out.println(requestEntity.getHeaders()); //获取请求头 [host:"localhost:8080", connection:"keep-alive", content-length:"63", cache-control:"max-age=0", sec-ch-ua:""Chromium";v="112", "Google Chrome";v="112", "Not:A-Brand";v="99"", sec-ch-ua-mobile:"?0", sec-ch-ua-platform:""Windows"", upgrade-insecure-requests:"1", origin:"http://localhost:8080", user-agent:"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 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.7", sec-fetch-site:"same-origin", sec-fetch-mode:"navigate", sec-fetch-user:"?1", sec-fetch-dest:"document", referer:"http://localhost:8080/springMVC/", accept-encoding:"gzip, deflate, br", accept-language:"en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7", cookie:"Idea-cd4fc388=5f928943-4086-4741-8f67-92fff7302594", Content-Type:"application/x-www-form-urlencoded;charset=UTF-8"]
System.out.println(requestEntity.getBody()); //获取请求体 username=admin&password=1111&TestRequestBody=%E6%8F%90%E4%BA%A4
return "success";
}
@ResponseBody注解会将返回值作为返回体进行返回,例如下面代码
@RequestMapping(value="/testResponseBody")
@ResponseBody
public String testResponseBody() {
return "failure";
}
就会将failure作为字符串显示在页面上,而不加该注解则会由Thymeleaf解析显示该显示的页面
导入json的必要依赖
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>2.12.1version>
dependency>
json是一种javascript的数据交换格式
@RequestMapping ("/testResponseUser")
@ResponseBody
public User testResponseUser() {
//如果将对象作为返回值,则json会自动将对象自动转换为String类型的字符串
//json数组
return new User(1001, "Lili", "1999", "女", 24);
}
Ajax默认是不进行跳转的请求,可以方便的实现很多功能
<div id="app">
<a @click="testAxios" th:href="@{/testAxios}">SpringMVC处理Ajaxa>
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) {
//使用ajax发送请求
axios({
//post请求
method : "post",
url : event.target.href,
params : {
username : "admin",
password : "123456"
}
}).then(function(response){
alert(response.data);
});
event.preventDefault();
}
}
})
script>
@RestController注解是用于标识控制层类的注解,被其标识的注解都会视为在该控制层的每一个控制器方法上都添加了@ResponseBody注解。