SpringMVC:HttpMessageConverter
作者:互联网
HttpMessageConverter
报文信息转换器。
将请求报文
转换成java对象
,将java对象
转换成响应报文
。
HttpMessageConverter提供了两个注解和两个类型:@RequestBody,@ResponseBody,RequestEntity,ResponseEntity
@RequestBody
<form th:action="@{/testRequest}" method="post">
<input type="text" name="username"><br>
<input type="text" name="password"><br>
<input type="submit" value="提交"><br>
</form>
@RequestMapping("/testRequest")
public String testRequest(@RequestBody String requestBody){
System.out.println(requestBody);
return "success";
}
//打印结果为username=admin&password=admin
RequestEntity
<form th:action="@{/testRequestEntity}" method="post">
<input type="text" name="username"><br>
<input type="text" name="password"><br>
<input type="submit" value="提交"><br>
</form>
@RequestMapping("/testRequestEntity")
public String testRequestEntity(RequestEntity<String> requestEntity){
System.out.println(requestEntity.getHeaders());
System.out.println(requestEntity.getBody());
return "success";
}
//打印结果为[host:"localhost:8080", connection:"keep-alive", content-length:"25", cache-control:"max-age=0", sec-ch-ua:""Google Chrome";v="95", "Chromium";v="95", ";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/95.0.4638.54 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_4/", accept-encoding:"gzip, deflate, br", accept-language:"zh-CN,zh;q=0.9", cookie:"JSESSIONID=265D329EF9F022E755E85C6065DC7E4F", Content-Type:"application/x-www-form-urlencoded;charset=UTF-8"]
//另一结果为username=123&password=123
@ResponseBody
@RequestMapping("/testResponseBody")
@ResponseBody
public String testResponseBody(){
return "hello world";
}
//结果为:在浏览器中的一个新页面上打印hello world
@ResponseBody处理json的步骤:
a>导入jackson的依赖
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.1</version>
</dependency>
b>在SpringMVC的核心配置文件中开启mvc的注解驱动,此时在HandlerAdaptor中会自动装配一个消息转换器:MappingJackson2HttpMessageConverter,可以将响应到浏览器的Java对象转换为Json格式的字符串
<mvc:annotation-driven />
c>在处理器方法上使用@ResponseBody注解进行标识
d>将Java对象直接作为控制器方法的返回值返回,就会自动转换为Json格式的字符串
@RequestMapping("/testResponseUser")
@ResponseBody
public User testResponseUser(){
return new User(1001,"admin","123456",23,"男");
}
浏览器的页面中展示的结果:
{"id":1001,"username":"admin","password":"123456","age":23,"sex":"男"}
处理ajax
使用Vue响应ajax
<div id="div">
<a @click="testAjax" th:href="@{/testAjax}">hello</a>
</div>
<script>
var vue = new Vue({
el:"#div",
methods:{
//点击事件
testAjax:function (event){
axios({
//请求方法
method:"post",
//请求url
url:event.target.href,
//请求参数
params:{
username:"admin",
password:"admin"
}
//回调函数——指请求成功或是失败执行的函数。
}).then(function (response){
alert(response.data);
})
//用来取消event的默认动作。
event.preventDefault();
}
}
});
</script>
@RestController
@RestController注解是springMVC提供的一个复合注解,标识在控制器的类上,就相当于为类添加了@Controller注解,并且为其中的每个方法添加了@ResponseBody注解
ResponseEntity
ResponseEntity用于控制器方法的返回值类型,该控制器方法的返回值就是响应到浏览器的响应报文
1、文件下载
使用ResponseEntity实现下载文件的功能
@RequestMapping("/testDown")
public ResponseEntity<byte[]> testResponseEntity(HttpSession session) throws IOException {
//获取ServletContext对象
ServletContext servletContext = session.getServletContext();
//获取服务器中文件的真实路径
String realPath = servletContext.getRealPath("/static/img/1.jpg");
//创建输入流
InputStream is = new FileInputStream(realPath);
//创建字节数组
byte[] bytes = new byte[is.available()];
//将流读到字节数组中
is.read(bytes);
//创建HttpHeaders对象设置响应头信息
MultiValueMap<String, String> headers = new HttpHeaders();
//设置要下载方式以及下载文件的名字
headers.add("Content-Disposition", "attachment;filename=1.jpg");
//设置响应状态码
HttpStatus statusCode = HttpStatus.OK;
//创建ResponseEntity对象
ResponseEntity<byte[]> responseEntity = new ResponseEntity<>(bytes, headers, statusCode);
//关闭输入流
is.close();
return responseEntity;
}
标签:HttpMessageConverter,SpringMVC,ResponseBody,sec,ResponseEntity,注解,new,admin 来源: https://www.cnblogs.com/Boerk/p/15869542.html