| HttpMessageConverter,报文信息转换器,将请求报文转换为Java对象,或将Java对象转换为响应报文 |
| HttpMessageConverter提供了两个注解和两个类型:@RequestBody,@ResponseBody,RequestEntity,ResponseEntity |
| @RequestBody可以获取请求体,需要在控制器方法设置一个形参,使用@RequestBody进行标识,当 |
| 前请求的请求体就会为当前注解所标识的形参赋值 |
| |
| # 前端 |
| <form th:action="@{/testRequestBody}" method="post"> |
| <input type="text" name="username"> |
| <input type="text" name="password"> |
| <input type="submit" value="测试@RequestBody"> |
| </form> |
| |
| # 后端 |
| @RequestMapping("/testRequestBody") |
| public String testRequestBody(@RequestBody String requestBody){ |
| System.out.println("requestBody:"+requestBody); |
| return "success"; |
| } |
| |
| # 测试 |
| requestBody:username=admin&password=123456 |
| RequestEntity封装请求报文的一种类型,需要在控制器方法的形参中设置该类型的形参,当前请求的 |
| 请求报文就会赋值给该形参,可以通过getHeaders()获取请求头信息,通过getBody()获取请求体信息 |
| |
| # 前端 |
| <form th:action="@{/testRequestEntity}" method="post"> |
| <input type="text" name="username"> |
| <input type="text" name="password"> |
| <input type="submit" value="测试RequestEntity"> |
| </form> |
| |
| # 后端 |
| @RequestMapping("/testRequestEntity") |
| public String testRequestEntity(RequestEntity<String> requestEntity){ |
| |
| System.out.println("请求头:"+requestEntity.getHeaders()); |
| System.out.println("请求体:"+requestEntity.getBody()); |
| return "success"; |
| } |
| |
| # 测试 |
| requestHeader:[host:"localhost:8080", |
| connection:"keep-alive", content-length:"27", |
| cache-control:"max-age=0", |
| sec-ch-ua:"" Not A;Brand";v="99", "Chromium";v="90", "Google Chrome";v="90"", |
| sec-ch-ua-mobile:"?0", upgrade-insecure-requests:"1", origin:"http://localhost:8080", |
| user-agent:"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, likeGecko) Chrome/90.0.4430.93 Safari/537.36"] |
| requestBody:username=admin&password=123 |
- 使用HttpServletResponse返回数据
| @RequestMapping("/testResponse") |
| public void testResponse(HttpServletResponse response) throws IOException { |
| response.getWriter().print("hello,response"); |
| } |
| |
| # http: |
| @ResponseBody用于标识一个控制器方法,可以将该方法的返回值直接作为响应报文的响应体响应到浏览器 |
| |
| @RequestMapping(value = "/testResponseBody", produces = "text/html;charset=UTF-8") |
| @ResponseBody |
| public String testResponseBody(){ |
| return "成功"; |
| } |
| |
| # http: |
| @RequestMapping("/testResponseUser") |
| @ResponseBody |
| public User testResponseUser(){ |
| return new User(1001, "admin", "123456", 23, "男"); |
| } |
| |
| |
| |
| |
| |
| <dependency> |
| <groupId>com.fasterxml.jackson.core</groupId> |
| <artifactId>jackson-databind</artifactId> |
| <version>2.12.1</version> |
| </dependency> |
| |
| 息转换器:MappingJackson2HttpMessageConverter,可以将响应到浏览器的Java对象转换为Json格 |
| 式的字符串 |
| <mvc:annotation-driven /> |
| |
| |
| @RequestMapping("/testAxios") |
| @ResponseBody |
| public String testAxios(String username, String password){ |
| System.out.println(username+","+password); |
| return "hello,axios"; |
| } |
| |
| # 前端 |
| axios({ |
| method:"post", |
| url:event.target.href, |
| params:{ |
| username:"admin", |
| password:"123456" |
| } |
| }).then(function (response) { |
| alert(response.data); |
| }); |
| @RestController注解是springMVC提供的一个复合注解,标识在控制器的类上,就相当于为类添加了 |
| @Controller注解,并且为其中的每个方法添加了@ResponseBody注解 |
| ResponseEntity用于控制器方法的返回值类型,该控制器方法的返回值就是响应到浏览器的响应报文 |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
· 面试官:你是如何进行SQL调优的?