@RequestBody对象为空,异常Required request body is missing错误解决
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)
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错误解决的更多相关文章
- @RequestBody对象为空,异常Required request body is missing
1.异常 org.springframework.http.converter.HttpMessageNotReadableException: Required request body is mi ...
- [已解决]报错:Required request body is missing
问题代码: res = requests.post(getXxxxList_url, headers=headers, data={}) 对象网站: angular4 apache 通过验证 (coo ...
- 前端传送JSON数据,报Required request body is missing
声明: 后端为Java,采用SSM框架 前端一个JSON.stringify()传来的json字符串,后端一般用@RequestBody标签来定义一个参数接收 但问题在于,当我使用get方式传JSON ...
- 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 ...
- DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing
客户端当发送空的json字符串时,请求RestController时,报错: DefaultHandlerExceptionResolver : Failed to read HTTP message ...
- Nginx出现“413 Request Entity Too Large”错误解决方法
Nginx出现“413 Request Entity Too Large”错误解决方法 2011-03-25 13:49:55| 分类: 默认分类 | 标签:413 request entit ...
- vs2015 建立项目报错:值不能为空,参数名:path1的错误解决与“未将对象引用到对象的实例”
“值不能为空,参数名:path1” 的错误.原因就是安卓sdk的路径不正确. 最简单的解决办法如下: 找到C:\Program Files (x86)\Android\android-sdk.进入文件 ...
- Nginx出现413 Request Entity Too Large错误解决方法
Nginx出现的413 Request Entity Too Large错误,这个错误一般在上传文件的时候出现,打开nginx主配置文件nginx.conf,找到http{}段,添加 解决方法就是 打 ...
- Nginx 出现413 Request Entity Too Large 错误解决方法(上传大小限制)
| 时间:2013-09-05 20:19:14 | 阅读数:485075 [导读] Nginx出现的413 Request Entity Too Large错误,这个错误一般在上传文件的时候出现,打 ...
随机推荐
- struts2-自定义拦截器-struts2标签
1 自定义拦截器 1.1 架构 1.2 拦截器创建 创建方式1 创建方式2 创建方式3 ★★★★ 1.3 拦截器api 放行 前后处理 不放行,直接跳转到一个结果页面 不执行后续的拦截器以及Actio ...
- oracle pl/sql远程连接过程
之前没用过oracle,现在公司用到就记录下安装过程吧.安装PL/SQL工具,安装oracle11G工具.打开PL/SQL 进行配置.
- springboot整合mybatis通用Mapper
参考: https://blog.csdn.net/x18707731829/article/details/82814095 https://www.jianshu.com/p/6d2103451d ...
- Leetcode151. Reverse Words in a String翻转字符串里的单词
给定一个字符串,逐个翻转字符串中的每个单词. 示例: 输入: "the sky is blue", 输出: "blue is sky the". 说明: 无空格 ...
- c++新特性实验(5)声明与定义:属性列表(C++11 起)
1.初识属性 1.1 实验A: noreturn 属性 [[ noreturn ]] static void thread1(void *data){ cout << "nore ...
- SSM-5zookeeper在LINUX上自启
把zookeeper做成服务 1.进入到/etc/rc.d/init.d目录下,新建一个zookeeper脚本 [root@zookeeper ~]# cd /etc/rc.d/init.d/ [ro ...
- nvm与nrm
nvm:切换Node版本 nrm:管理npm包的镜像源 nvm的安装,可以参考官网,windows系统直接https://github.com/coreybutler/nvm-windows/re ...
- 记录centos7下tomcat部署war包过程
记录centos7下tomcat部署war包过程 1.官网下载tomcat安装包.gz结尾的 2.上传到/usr/local/ ,并解压到tomcat目录下 3.进入tomcat/bin目录,运行./ ...
- opencv4 java 验证码噪点 8邻域降噪
工程下载地址https://download.csdn.net/download/qq_16596909/11503962 程序运行后,同样会把图片存放在以下路径 首先来看一下原图 二值化后,可以把这 ...
- linux升级或安装程序后无法进入图形界面
报错如下: Failed to start the X server (your graphical interface). lt is likely that it is not set up co ...