SpringMVC学习总结(七)@RequestBody/RequestEntity/@ResponseBody/SpringMVC处理Json和Ajax/ResponseEntity/文件上传和下载

HttpMessageConverter,报文信息转换器,将请求报文转换为Java对象,或将Java对象转换为响应报文

HttpMessageConverter提供了两个注解和两个类型:@RequestBody,@ResponseBody,RequestEntity,ResponseEntity

一、获取请求信息

(一)@RequestBody注解

@RequestBody可以获取请求体,需要在控制器方法设置一个形参,使用@RequestBody进行标识,当前请求的请求体就会为当前注解所标识的形参赋值。

案例
index.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
<h1>首页</h1>
<form th:action="@{/testRequestBody}" method="post">
    用户名:<input type="text" name="username"><br>
    密码:<input type="password" name="password"><br>
    <input type="submit">
</form>
</body>
</html>

controller:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class MyController {
   
	//访问首页
    @RequestMapping("/")
    public String toIndex(){
   
        return "index";
    }
    
    @RequestMapping("/testRequestBody")
    public String testRequestBody(@RequestBody String requestBody){
   
        System.out.println("请求体信息:"+requestBody);
        return "target";
    }
}

启动Tomcat:
在这里插入图片描述
在这里插入图片描述
IDEA输出:
在这里插入图片描述

(二)RequestEntity类

RequestEntity是封装请求报文的一种类型,它可以获取完整报文信息。需要在控制器方法的形参中设置该类型的形参,当前请求的请求报文就会赋值给该形参,可以通过getHeaders()获取请求头信息,通过getBody()获取请求体信息。

案例
index.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
<h1>首页</h1>
<form th:action="@{/testRequestEntity}" method="post">
    用户名:<input type="text" name="username"><br>
    密码:<input type="password" name="password"><br>
    <input type="submit">
</form>
</body>
</html>

controller:

import org.springframework.http.RequestEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class MyController {
   
	//访问首页
    @RequestMapping("/")
    public String toIndex(){
   
        return "index";
    }
    
    @RequestMapping("/testRequestEntity")
    public String testRequestBody(RequestEntity<String> requestEntity){
   
        System.out.println("请求头信息:"+requestEntity.getHeaders());
        System.out.println("请求体信息:"+requestEntity.getBody());
        return "target";
    }
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

二、向浏览器响应数据

(一)通过HttpServletResponse响应浏览器数据

案例
index.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
<h1>首页</h1>
<a th:href="@{/testResponse}">通过ServletAPI响应数据到浏览器</a>
</body>
</html>

controller:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值