`

springboot @RequestBody 和 @RequestParam

阅读更多
一 在路径中 在PathVariable后面接入“uid”就可以了。
@PathVariable
@RequestMapping(value="/{uid}", method=RequestMethod.GET)
    public List<Map<String, Object>> getUser(@PathVariable("uid") Integer id) {
        return userService.getUserById(id);
    }

二 RequestParam 在url路由上还是在请求的body上
url这样写:http://xxxxx?phoneNum=xxxxxx,也就是说被@RequestParam修饰的参数最后通过key=value的形式放在http请求的Body传过来

@RequestMapping(value="", method=RequestMethod.POST)
    public String postUser(@RequestParam(value="phoneNum", required=true) String phoneNum ) String userName) {
       

三 RequestBody
RequestBody能把简单json结构参数转换成实体类,如下代码
public String testUser(@RequestBody User user){

RequestParam注解接收的参数是来自于requestHeader中,即请求头,也就是在url中,格式为xxx?username=123&password=456,而RequestBody注解接收的参数则是来自于requestBody中,即请求体中。


@RequestParm用于绑定controller上的参数,可以是多个参数,也可以是一个Map集合,GET,POST均可
@RequestParm中name属性是指定参数名,required属性默认为ture,表示必传。若为false则为非必传。属性有defaultValue默认值选项,若该参数为null时,会将默认值填充到参数上。
•只支持Content-Type: 为 application/x-www-form-urlencoded编码的内容。Http协议中,如果不指定Content-Type,则默认传递的参数就是application/x-www-form-urlencoded类型)

requestParam(@RequestParam(name = "string",required = false) String str,@RequestParam(name = "integer",defaultValue = "123456") int integer){

@RequestBody绑定一个对象实体



区别              @RequestParam               @RequestBody
content-type      仅支持x-www-form-urlencoded 支持json格式
请求类型          ALL                         除了GET
注解个数          可多个                      只能一个

用post 可以用对象或map(啥数值都有不安全)来接收,查询时用指定入参

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics