SpringMVC 获取请求数据的注解 @RequestBody与实体类RequestEntity 的基本用法

在 Servlet 中有格HttpServletRequest 的方法可以使使我们可以轻松获取请求数据, 包括请求头、请求体。而在 SpringMVC 中一方面可以利用 Servlet 的 API 来获取请求数据;另一方面也提供了注解和实体类的方式来更加方便的获取。话不多说,直接上代码。
其中,前端代码为 themleaf 引擎构造出来,分别构造出两个输入用户数据的文本框,在后端进行分别测试 @RequestBody 与RequestEntity

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.themleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form th:action="@{/testHttpRequestBody}" method="post">
    用户名:<input type="text" name="username"><br>
    密码:<input type="password" name="password"><br>
    爱好:<input type="checkbox" name="hobby" value="a">a
    <input type="checkbox" name="hobby" value="b">b
    <input type="checkbox" name="hobby" value="c">c<br>
    <input type="submit" value="登录">
</form>
<form th:action="@{/testHttpRequestEntity}" method="post">
    用户名:<input type="text" name="username"><br>
    密码:<input type="password" name="password"><br>
    爱好:<input type="checkbox" name="hobby" value="a">a
    <input type="checkbox" name="hobby" value="b">b
    <input type="checkbox" name="hobby" value="c">c<br>
    <input type="submit" value="登录">
</form>
</body>
</html>

后端代码

package com.wpp.controller;

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


@Controller
@RequestMapping(value = "/")
public class HttpController {
    @RequestMapping(value = "testHttpRequestBody")
    public String testHttpRequestBody(@RequestBody  String request){
        System.out.println(request);
        return "success";
    }
    @RequestMapping(value = "testHttpRequestEntity")
    public String testHttpRequestBody(RequestEntity<String> requestEntity){
        System.out.println("body:"+requestEntity.getBody());
        System.out.println("header:"+requestEntity.getHeaders());
        System.out.println("url:"+requestEntity.getUrl());
        return "success";
    }
}

分别展示请求数据
展示 RequestBody 为

username=admin&password=123456&hobby=a&hobby=b&hobby=c

展示 RequestEntity 实体类为

body:username=admin&password=123456&hobby=a&hobby=b&hobby=c
header:[host:"localhost:8080", connection:"keep-alive", content-length:"54", cache-control:"max-age=0", upgrade-insecure-(。。。。省略)
url:http://localhost:8080/SpringMVC/testHttpRequestEntity

会发现 RequestBody 仅仅是对于请求体的数据。 RequestEntity 更为全面 url、请求体、请求体、请求方法等都能获得。
对于使用方法上,@RequestBody 是注解的方式,RequestEntity 则是一个实体类。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值