当前位置: 代码迷 >> 综合 >> @requestBody 与@requestparam区别
  详细解决方案

@requestBody 与@requestparam区别

热度:69   发布时间:2024-02-05 00:17:00.0

@requestBody 与@requestparam区别

  • content-type知识点
  • Controller获取参数方式
    • @PathVariable 获取URL中的参数
    • HttpServletRequest
    • 不加注解获取参数
    • @RequestParam 注解获取参数
    • bean方式获取参数
  • 小结
    • @RequestBody注解
    • List参数问题

今天在做项目实战回顾的时候发现@RequestParam注解和@requestBody注解不太了解。在网上搜索下了下,发现了篇好文章。防止将来丢失,在此做下记录。谢谢

content-type知识点

在http请求头里面会看到content-type参数,官方定义是:

MediaType,即是Internet Media Type,互联网媒体类型;也叫做MIME类型,在Http协议消息头中,使用Content-Type来表示具体请求中的媒体类型信息。

通俗理解为请求内容的编码类型。
常见的编码格式:

application/x-www-form-urlencoded:数据被编码为名称/值对。这是标准的编码格式。
multipart/form-data: 数据被编码为一条消息,页上的每个控件对应消息中的一个部分。
text/plain: 数据以纯文本形式(text/json/xml/html)进行编码,其中不含任何控件或格式字符。

前端表单里的enctype属性为编码方式,常用的有两种:application/x-www-form-urlencoded和multipart/form-data,默认为application/x-www-form-urlencoded。上传文件时通常是用multipart/form-data
如果想要往后端传json串,那么就要指定content-type为application/json。而后端要接收这样的json串就要用@ReqeustBody注解修饰参数。

Controller获取参数方式

@PathVariable 获取URL中的参数

@RequestMapping(value = "/{name}/f", method = RequestMethod.POST)public String g(@PathVariable String name, Integer age) {return "name:" + name + ",age:" + age;}

HttpServletRequest

 @RequestMapping(value = "/a", method = RequestMethod.POST)public String a(HttpServletRequest request, HttpServletResponse response) {return "name:" + request.getParameter("name") + ",age:" + request.getParameter("age");}

11.PNG
这是最基本的获取参数的方式,对于GET和POST请求都适用。一般在拦截器里面会用这种方式获取参数。HttpServletRequest可以获取请求头的完整信息,在一次请求的生命周期内,可以通过下面的方式获取Request对象(当然也可以获取response对象):

HttpServletRequest httpServletRequest = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();

不加注解获取参数

@RequestMapping(value = "/b", method = RequestMethod.POST)
public String b(String name, Integer age) {return "name:" + name + ",age:" + age;
}

如果不传参数则默认为null。如果参数是基本类型,则必须要传,不然会报500错。所以最好使用封装类型Integer/String等接受或者用bean包装起来,bean的话会给bean的属性默认值0。
指定参数名和方法的参数名一致。

@RequestParam 注解获取参数

@RequestMapping(value = "/d", method = RequestMethod.POST)
public String d(@RequestParam(value = "sss") String name, @RequestParam(value = "age", required = false) Integer age) {return "name:" + name + ",age:" + age;
}

@RequestParam注解可以指定参数的名字和是否必传,除此之外,都和上面的不加注解的方式类似。
另外需要说明的一点是如果参数是基本类型,同时指定required = false,还是会报500的错。因为没有传的参数都会直接把null赋过来,基本类型是无法接受null值的,所以会报错。
另外@RequestParam的name参数和value参数是一样的作用,因为他们指定对方互相为别名。

bean方式获取参数

@RequestMapping(value = "/c", method = RequestMethod.POST)
public String c(User user) {return "name:" + user.getName() + ",age:" + user.getAge();
}
public class User {private String name;private int age;
}

推荐使用这种方式,就算age是基本类型int,如果前端没传过来,也会默认给0值。

小结

以上的获取参数的方式,在postman里面都是用params填写的,也就是参数会被加到url里面去。
如果把参数放入body里面去当作表单或者用x-www-form-urlencoded提交,同样是可以的。

@RequestBody注解

@RequestMapping(value = "/e", method = RequestMethod.POST)
public String e(Integer age, @RequestBody String name) {return "name:" + name + ",age:" + age;
}

@RequestBody注解的参数必须以body的形式提交
先看使用x-www-form-urlencoded提交

在这里插入图片描述
name的值变成了name=pzx&age=18,显然不是想要的结果。这里是因为String修饰的name,要是其他类型就会直接报不支持的类型错误,@RequestBody注解其实是不太支持x-www-form-urlencoded格式的。@requestBody注解常用来处理content-type不是默认的application/x-www-form-urlcoded编码的内容,比如说:application/json或者是application/xml等。一般情况下来说常用其来处理application/json类型。

@RequestMapping(value = "/f", method = RequestMethod.POST)
public String f(@RequestBody User user) {return "name:" + user.getName() + ",age:" + user.getAge();
}

15.PNG
前端传值时要指定content-type为applicati/json,后端使用bean接受即可。
如果是使用@RequestBody修饰,那么就不能通过HttpServletRequest获取参数。
@RequestBody参数只支持传入一个对象参数,它实际上是将输入流的body体作为一个整体进行转换,而body整体只有一份,解析完成之后会关闭输入流。如果传入多个参数则会报错。除非自定义参数解析器。

List参数问题

上面介绍的都是基础内容,接下来介绍遇到的问题,以及解决办法。

    @RequestMapping(value = "/h", method = RequestMethod.POST)public String h(List<String> name, Integer age) {return "name:" + name + ",age:" + age;}@RequestMapping(value = "/i", method = RequestMethod.POST)public String i(@RequestParam List<String> name, Integer age) {return "name:" + name + ",age:" + age;}前端发送请求过来,不管是form还是x-www-form-urlencoded格式,/h接口报错,/i接口成功/h接口:
"status": 500,
"error": "Internal Server Error",
"exception": "org.springframework.beans.BeanInstantiationException",
"message": "Failed to instantiate [java.util.List]: Specified class is an interface",
/i接口:
name:[pzx, hyy],age:18
接下来看@RequestBody的@RequestMapping(value = "/j", method = RequestMethod.POST)public String j(@RequestBody List<String> names) {return "name:" + names;}@RequestMapping(value = "/k", method = RequestMethod.POST)public String k(@RequestBody List<User> users) {return "users:" + users;}/j输入:["pzx","hyy"]输出:name:[pzx, hyy]/k输入:[{"name":"pzx","age":18},{"name":"hyy","age":17}]输出:users:[User(name=pzx, age=18), User(name=hyy, age=17)]

转载哈:
作者:史莱克同学的码农录
链接:https://www.jianshu.com/p/30848dc6d032
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  相关解决方案