栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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 Mapmap) 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进行取值即可

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/957224.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号