1.异常

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

2.问题复现:

@RequestMapping(value = "/somewhere", method = POST)
public SomeResponse someHandler(@RequestBody XXXDTO xxxDTO) { ... } 当入参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)
 @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());
}

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

 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; }

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

3.解决方案:

1)@RequestBody (required=false)

2) 不要让DTO对象为空

												

@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. Required request body is missing-请求接口报错

    一.问题由来 自己目前在做一个小程序的后台,已经写好了项目中的很多的接口,同时也在进行一些修改,比如添加拦截器,统一校验一个固定的参数是否正确. 在自己添加拦截器之前,这些接口都可以正常访问,可是在添 ...

  7. c#中判断对象为空的几种方式(字符串等)

    (1)先了解几个与空类型相关的关键字和对象  Null : 关键字表示不引用任何对象的空引用,它是所有引用类型变量的默认值,在2.0版本之前也就只有引用变量类型可以为null,如(string a=n ...

  8. Javascript 中判断对象为空

    发现了一个巧妙的实现: 需要检查一个对象(Object)是否为空,即不包含任何元素.Javascript 中的对象就是一个字典,其中包含了一系列的键值对(Key Value Pair).检查一个对象是 ...

  9. js 判断是否为空对象、空数组

    当需要判断参数是否为空时,总希望 js 能够提供原生的判断方法,可惜并没有,只能自己封装了. function isEmpty(obj) { // 检验 undefined 和 null if(!ob ...

随机推荐

  1. Server in ASP.NET-Core

    .NET-Core Series Server in ASP.NET-Core DI in ASP.NET-Core Routing in ASP.NET-Core Error Handling in ...

  2. 乐橙谷浅析JAVA程序员就业前景

    不知道大家对Java就业前景了解多少.随着信息化的发展,IT培训受倒了越来越多人的追捧.在开发领域,JAVA培训成为了许多人的首选!JAVA应用广泛,JAVA培训就业前景良好!目前,虽然JAVA人才的 ...

  3. 利用百度地图WEB服务APIGeoCoding API批量地址解析

    Geocoding API包括地址解析和逆地址解析功能: 地理编码:即地址解析,由详细到街道的结构化地址得到百度经纬度信息,例如:“北京市海淀区中关村南大街27号”地址解析的结果是“lng:116.3 ...

  4. PHP通过URL获取文件大小

    function getFileSize($url){ $url = parse_url($url); if($fp = @fsockopen($url['host'],empty($url['por ...

  5. 【转】Python装饰器与面向切面编程

    原文请参考: http://www.cnblogs.com/huxi/archive/2011/03/01/1967600.html 今天来讨论一下装饰器.装饰器是一个很著名的设计模式,经常被用于有切 ...

  6. ZOJ2006 一道很尴尬的string操作题

    ZOJ2006(最小表示法) 题目大意:输出第一个字符串的最小字典序字串的下标! 然后我居然想试一试string的erase的能力,暴力一下,然后20msAC了,尴尬的数据.......... #in ...

  7. 浅析php curl_multi_*系列函数进行批量http请求

    何起: 一系列 数量很大 数据不热 还希望被蜘蛛大量抓取的页面,在蜘蛛抓取高峰时,响应时间会被拉得很高. 前人做了这样一个事儿:页面分3块,用3个内部接口提供,入口文件用curl_multi_*系列函 ...

  8. MVC中重写RoleProvider角色管理

    /* 数据表SQL脚本 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FK_UsersInRoles_Ro ...

  9. doubi -- 初创

    doubi -- 初创 [背景分析] 一直在苦苦探寻人生的价值和意义.在这"二八"年华,对IT工作有点厌倦了.每天都是无休止地问题定位,需求会议.碎片化的时间写出来的代码都是无比的 ...

  10. 【特效】jquery选项卡插件,页面多个选项卡统一调用

    把选项卡整合了一下,写成个插件,这样可以整个站,或整个页面有多个选项卡时,统一调用.代码的具体注意事项已经写进注释了.用于js获取元素的class名称必须有,选项卡本身的样式,另再取一个名字来设置(本 ...