SpringMVC获取请求参数的各种方法

3 篇文章 0 订阅
2 篇文章 0 订阅

1.使用注解@PathVariable,获取路径中的占位符

在RESTful风格API中,经常在路径中传递参数,比如路径localhost:8081/test/collection/1路径中最后的1代表id传递给后端

@RequestMapping("/test/collection/{id}")//如果少了参数会报404
public Integer testPathVariable(@PathVariable("id") Integer id){
    System.out.println(id);
    //最终在浏览器输出1
    return id;
}

2.前端传递参数的两种格式

在这仅讨论GET、POST请求方式。总体将传递参数分为两种方式:

GET请求中,通常在路径中使用?与&进行传参,如localhost:8081/test?user=adimin&password=123,然后在POST请求中与之对应也有一种请求头Content-Type:application/x-www-form-urlencoded,也会将请求参数通过?与&拼接在请求体中。

POST请求中,还有另外一种请求头Content-Type:application/json,传递JSON字符串给后端,如{"username":"admin","password":"123456"}

3 请求头为application/x-www-form-urlencoded,SpinrgMVC获取参数的四种方式

以下四种方式对应的请求路径为:localhost:8081/test?user=adimin&password=123或使用POST请求以x-www-form-urlencoded方式发送请求体

3.1 使用HttpServletRequest

正如原生Servlet一样,SpringMVC也保留了HttpServletRequest,可以通过getParameter()获取参数

    @GetMapping("/test")
    public String testHttpServletRequest(HttpServletRequest request)
    {

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        return username+password
    }

3.2 使用同名参数(推荐)

@GetMapping("/test")
//参数名字必须相同
public String testHttpServletRequest(String username, String password)
{
    return username+password
}

3.3 使用@RequestParam注解(推荐)

@GetMapping("/test")
//注解的value值对应传过来的参数名
public String testHttpServletRequest(@RequestParam("username")String name, @RequestParam("password")String password)
{
    return name+password
}

3.4 使用实体类对象(推荐)

//实体类必须有参数名对应的属性,且属性get/set方法
@Data
public class User{
	private String username;
	private String password;
}
@GetMapping("/test")
public String testHttpServletRequest(User user)
{
    return user.getName()+user.getPassword();
}

以上结果都为
在这里插入图片描述

4.请求头为application/json

以下方法都是基于SpringMvc默认的Json数据解析器Jackson,使用ObjectMapper对象的readValue()方法进行解析

4.1 使用HttpServletRequest

	@PostMapping("/test")
public String testRequestBody(HttpServletRequest request) throws IOException {
    ObjectMapper objectMapper = new ObjectMapper();
    User user = objectMapper.readValue(request.getInputStream(), User.class);
    return user.getName()+user.getPassword();
}

4.2 使用@RequestBody注解,配合Map(推荐)

使用@RequestBody注解配合Map,Map中的key为传过来的参数名,值为对应的值

@PostMapping("/test")
public String testRequestBody(@RequestBody Map<String,String>map) throws IOException {
    String username=map.get("username");
    String password=map.get("password");
    return username+password;
}

4.3使用@RequestBody注解,配合实体类(推荐)

@PostMapping("/test")
public String testRequestBody(@RequestBody User user){
   return user.getName()+user.getPassword();
}

总结

无论是GET还是POST请求,主要分成两种传参方式:&与JSON字符串,具体由请求头决定。

&方式关注@RequestParam注解!!JSON字符串方式关注@RequestBody注解!!再配合形参/实体类对象/Map进行取值即可

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值