@RequestBody和@RequestParam的区别

1、@RequestBody什么时候用

当请求方式为post请求,且content-type为application-json的时候

@RequestMapping(value = "register.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponseJsonResult register(@RequestBody UserBO userBO) {
        System.out.println(userBO.getUsername());
        return ServerResponseJsonResult.ok();
    }

备注:如果使用@RequestBody,那么接收参数必须是以对象形式,比如UserBO userBO,不能是sString username这种,不然会报错

2、@RequestParam

 请求方式既可以是get也可以是post,Content-Type 为 application/x-www-form-urlencoded 

注解@RequestParam接收的参数是来自HTTP请求体或请求url的QueryString中

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public IMOOCJSONResult list(@RequestParam String userId) {
        if (StringUtils.isBlank(userId)) {
            return IMOOCJSONResult.errorMsg("");
        }
}

 

posted @ 2021-03-16 14:43  zhaobao1830  阅读(113)  评论(0编辑  收藏  举报