1.异常

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

情况一、

2.问题展示

    1. @RequestMapping(value = "/somewhere", method = POST)
      1. public SomeResponse someHandler(@RequestBody XXXDTO xxxDTO) { ... }
          1. 当入参DTO对象为空时,
        1.  
        1. @RequestBody对应http请求body,当请求body为空时,异常!
        2. org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public com.rpc.common.Result<com.rpc.common.dto.PageDto<com.order.dto.OrderListDTO>> com.gateway.controller.OrderController.listOrder(com.*.*.order.dto.ListOrderPageDTO)
        3.         at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:154)
        4.         at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:128)
        5.         at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)
        6.         at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:158)
        7.         at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:128)
        8.         at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
        9.         at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
        10.         at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
        11.         at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
        12.         at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
        13.         at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
        14.         at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
        15.         at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
        16.         at javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
        17.         at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
        18.         at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
        19.         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
        20.         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        21.         at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        22.         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
        23.         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        24.         at org.springframework.boot.web.filter.ApplicationContextHeaderFilter.doFilterInternal(ApplicationContextHeaderFilter.java:55)

        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

        1. 1 @Override
        2. 2 protected <T> Object readWithMessageConverters(NativeWebRequest webRequest, MethodParameter parameter,
        3. 3 Type paramType) throws IOException, HttpMediaTypeNotSupportedException, HttpMessageNotReadableException {
        4. 4
        5. 5 HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
        6. 6 ServletServerHttpRequest inputMessage = new ServletServerHttpRequest(servletRequest);
        7. 7
        8. 8 Object arg = readWithMessageConverters(inputMessage, parameter, paramType);
        9. 9 if (arg == null) {
        10. 10 if (checkRequired(parameter)) {
        11. 11 throw new HttpMessageNotReadableException("Required request body is missing: " +
        12. 12 parameter.getMethod().toGenericString());
        13. 13 }
        14. 14 }
        15. 15 return arg;
        16. 16 }
        17. 17
        18. 18 protected boolean checkRequired(MethodParameter parameter) {
        19. 19 return (parameter.getParameterAnnotation(RequestBody.class).required() && !parameter.isOptional());
        20. 20 }

        看上图,最终是RequestBody注解的required属性。!parameter.isOptional()代表是否支持null,如果参数类型支持null,则是false,最终不用校验required.

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

        看上图,默认是true.我们只需要@RequestBody (required=false)

        3.解决办法

        1)@RequestBody (required=false)

        2) 不要让DTO对象为空


        情况二、

        springMvc的新注解:GetMapping 不支持@RequestBody ,使用PostMapping后面我改成以下代码就没有报错了

        @PostMapping(value="/schedules/findUserSchedule",produces = MediaType.APPLICATION_JSON_VALUE)

        public List<xxxxxx> findUser(@RequestBody xxxxxx xxxxx) {

        log.debug("查询用户日程", xxxxxx);

        }

        或者是使用了get提交方式

        情况三:

        如果不是因为上面的两种情况,

        请看这里:

        1. 我们在传输json数据的时候,假如json数据为空,那么就会报一个错误,就是Required request body is missing
        2. 这个的意思就是我们这个接口必须要传输json格式的数据,假如没有数据,就会报错返回错误的信息。

        原文地址:https://blog.csdn.net/q1035331653/article/details/80370818

        @RequestBody对象为空,异常Required request body is missing错误解决的更多相关文章

        1. @RequestBody对象为空,异常Required request body is missing

          1.异常 org.springframework.http.converter.HttpMessageNotReadableException: Required request body is mi ...

        2. [已解决]报错:Required request body is missing

          问题代码: res = requests.post(getXxxxList_url, headers=headers, data={}) 对象网站: angular4 apache 通过验证 (coo ...

        3. 前端传送JSON数据,报Required request body is missing

          声明: 后端为Java,采用SSM框架 前端一个JSON.stringify()传来的json字符串,后端一般用@RequestBody标签来定义一个参数接收 但问题在于,当我使用get方式传JSON ...

        4. Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public xxxxxxxx.

          最近在使用 springBoot开发的时候, 使用PostMan访问接口,  返回一个 404 ,  后台报一个 warn : Failed to read HTTP message: org.spr ...

        5. DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing

          客户端当发送空的json字符串时,请求RestController时,报错: DefaultHandlerExceptionResolver : Failed to read HTTP message ...

        6. Nginx出现“413 Request Entity Too Large”错误解决方法

          Nginx出现“413 Request Entity Too Large”错误解决方法 2011-03-25 13:49:55|  分类: 默认分类 |  标签:413  request  entit ...

        7. vs2015 建立项目报错:值不能为空,参数名:path1的错误解决与“未将对象引用到对象的实例”

          “值不能为空,参数名:path1” 的错误.原因就是安卓sdk的路径不正确. 最简单的解决办法如下: 找到C:\Program Files (x86)\Android\android-sdk.进入文件 ...

        8. Nginx出现413 Request Entity Too Large错误解决方法

          Nginx出现的413 Request Entity Too Large错误,这个错误一般在上传文件的时候出现,打开nginx主配置文件nginx.conf,找到http{}段,添加 解决方法就是 打 ...

        9. Nginx 出现413 Request Entity Too Large 错误解决方法(上传大小限制)

          | 时间:2013-09-05 20:19:14 | 阅读数:485075 [导读] Nginx出现的413 Request Entity Too Large错误,这个错误一般在上传文件的时候出现,打 ...

        随机推荐

        1. json字符串和对象的相互转换

          JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,是理想的数据交换格式. 同时,JSON是 JavaScript 原生格式,这 ...

        2. androidstudio实现增量更新步骤

          本文demo和参考例子参考-传送  门:http://blog.csdn.net/duguang77/article/details/17676797 一.增量更新优点:节省客户端和服务器端流量 增量 ...

        3. 树形结构的数据渲染(element-ui&VUE)

          在最开始学习的时候,渲染树形数据没有好好理解. 在实际的运用开发中,彻底的走了一遍树形数据,渲染角色权限的业务逻辑. 首先先发送请求获取全部权限树形结构, 其次发送请求获取当前用户的权限, 最后,通过 ...

        4. windows 标准错误重定向

          最近在windows上运行tensorflow的时候,出现很多stderr 的信息,干扰了正常的输出:所以我们需要使用操作把这些输出屏蔽: 参考链接:https://support.microsoft ...

        5. 通过Struts2Web应用框架深入理解MVC

          Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet. 一.用法简介: 1.Eclipse新建Dynamic Web Project, 项目名:Struts2Pro ...

        6. redis为什么快

          今天面试的时候被问到的一个问题,大致说了几点.回去又研究了一下. 大致分为几点: 1:Redis是纯内存数据库,一般都是简单的存取操作,线程占用的时间很多,时间的花费主要集中在IO上,所以读取速度快. ...

        7. java学习补全 1

          CLASSPATH java执行命令时利用此路径加在需要的.class文件 字符串常量就是String 类的匿名对象 String类在直接复制的情况下只会保留一块堆内存 a="hhh&quo ...

        8. hive如何配置支持事务及insert、update、delete

          Hive从0.14版本开始支持事务和行级更新,但缺省是不支持的,需要一些附加的配置.要想支持行级insert.update.delete,需要配置Hive支持事务. 一.Hive具有ACID语义事务的 ...

        9. Vue Router 相关

          1. 路由传参: 编程式的导航 router.push this.$router.push("home"); this.$router.push({ name: 'news', p ...

        10. spark应用程序引用别的jar包

          第一种方式 操作:将第三方jar文件打包到最终形成的spark应用程序jar文件中 应用场景:第三方jar文件比较小,应用的地方比较少 第二种方式 操作:使用spark-submit提交命令的参数: ...