1.异常

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

情况一、

2.问题展示


  1. @RequestMapping(value = "/somewhere", method = POST)
  2. public SomeResponse someHandler(@RequestBody XXXDTO xxxDTO) { ... }
  3. 当入参DTO对象为空时,
@RequestBody对应http请求body,当请求body为空时,异常!
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)
        at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:154)
        at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:128)
        at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)
        at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:158)
        at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:128)
        at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
        at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
        at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
        at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at org.springframework.boot.web.filter.ApplicationContextHeaderFilter.doFilterInternal(ApplicationContextHeaderFilter.java:55)

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

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

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

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

情况三:

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

请看这里:

我们在传输json数据的时候,假如json数据为空,那么就会报一个错误,就是Required request body is missing
这个的意思就是我们这个接口必须要传输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. Python学习之函数(多层函数)、re模块的正则匹配--计算复杂加减乘除

    头疼,其实这个程序在我看的视频当中是当做re模块的运用来进行测试的,而到了我这里就成了简化版的了,因为我实在是做吐了,恕小弟无能,只能做简化版的.为何说是简化版呢,因为要求是给的计算式是多层嵌套的小括 ...

  2. Windows 的 80 端口被 System 进程占用解决方案

    通过 Windows 的资源监视器(win+R:resmon)可以看到 80 端口已经被占用,下图是已经解决好了,没能截图被占用的情况,下面给出解决方案. PS:贴出两个好用的 windows cmd ...

  3. TZ_06_SpringMVC的入门程序

    SpringMVC的入门程序 1. 创建WEB工程,引入开发的jar包 1. 具体的坐标如下 2. 配置核心的控制器(配置DispatcherServlet) 1. 在web.xml配置文件中核心控制 ...

  4. Google自带截图工具的使用

    转载自:http://chromecj.com/utilities/2017-12/859.html

  5. Axure 工具的使用

    Axure RP是一款专业的快速原型设计工具,Axure RP是美国Axure Software Solution公司旗舰产品,是一个专业的快速原型设计工具,让负责定义需求和规格.设计功能和界面的专家 ...

  6. POJ1151 离散化求矩形面积的并

    /*第一道离散化的题目,虽然是水题,不过还是很高兴...*/ #include<cstdio> #include<algorithm> #include<cstring& ...

  7. jodatime 计算时间差_统计程序运行耗时

    https://blog.csdn.net/De_Moivre/article/details/79775661 记录开始执行的时间 DateTime startDateTime=new DateTi ...

  8. JAVA:在0-99间产生100个不重复的随机数

    Random rand = new Random(); boolean[] bool = new boolean[100]; int[] number = new int[100]; int rand ...

  9. python 关于如何把json文件里面的多条数据删除,只保留自己需要的条目

    参考博客: https://www.cnblogs.com/bigberg/p/6430095.html https://zhidao.baidu.com/question/7173208338528 ...

  10. LINUX访问文件配置

    访问文件 /etc/host.conf 告诉网络域名服务器如何查找主机名.(通常是 /etc/hosts,然后就是名称服务器:可通过 netconf 对其进行更改) /etc/hosts 包含(本地网 ...