SpringMVC异常: Required request body content is missing

1、项目异常信息:

org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing

2、异常代码片段:

    @ApiOperation(httpMethod = "POST", value = "数据权限信息保存")
    @RequestMapping(value="/insert", method={RequestMethod.POST})
    @ResponseBody
    public Result insert(@RequestBody @ApiParam(name="用户对象", value="json格式对象", required=true) *** entity) ;

3、SpringMVC源代码分析:

@Override
 protected <T> Object readWithMessageConverters(NativeWebRequest webRequest, MethodParameter parameter,
           Type paramType) throws IOException, HttpMediaTypeNotSupportedException, HttpMessageNotReadableException {

        HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
         ServletServerHttpRequest inputMessage = new ServletServerHttpRequest(servletRequest);
 
        Object arg = readWithMessageConverters(inputMessage, parameter, paramType);
          if (arg == null) {
            if (checkRequired(parameter)) {
                throw new HttpMessageNotReadableException("Required request body is missing: " +
                       parameter.getMethod().toGenericString());
            }
        }
         return arg;
    }

    protected boolean checkRequired(MethodParameter parameter) {
       return (parameter.getParameterAnnotation(RequestBody.class).required() && !parameter.isOptional());
   }

  从上述源代码checkRequired方法中可以看出:RequestBody 注解属性required的属性值, parameter.isOptiona判断请求参数是否为空。

public @interface RequestBody {
 
     /**
     * Whether body content is required.
      * <p>Default is {@code true}, leading to an exception thrown in case
      * there is no body content. Switch this to {@code false} if you prefer
      * {@code null} to be passed when the body content is {@code null}.
     * @since 3.2
     */
    boolean required() default true;
 }

从上述代码可以看出:RequestBody注解required的属性值,默认取值为:true.

4、解决办法:@RequestBody(required = false)

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值