@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错误,这个错误一般在上传文件的时候出现,打 ...
随机推荐
- web api中允许跨域访问
①添加owin的引用 ②添加owin.Cors的引用 ③在WebApiConfig中添加 config.EnableCors(new EnableCorsAttribute("*" ...
- 关于hive表同步类型问题
今天华为做实施的时候发现kylin做刷cube的时候发现源表数据类型不适合刷到kylin提供查询接口.问了下同事发现一个比较简单的解决办法. 源表是String类型,做hive视图可以做个hive表提 ...
- 【html、CSS、javascript-7】Dom
文档对象模型(Document Object Model,DOM)是一种用于HTML和XML文档的编程接口.它给文档提供了一种结构化的表示方法,可以改变文档的内容和呈现方式.我们最为关心的是,DOM把 ...
- g++编译多个源原文件和头文件(转载)
(头文件名是:Sales_item.hpp 源文件名:7-31.cpp和Sales_item.cpp)(点击我下载源文件) 方法1: 我用的命令是:g++ -o 7-31 7-31.cpp Sales ...
- java的boolean和Boolean
boolean是基本数据类型Boolean是它的封装类,和其他类一样,有属性有方法,可以new,例如: Boolean bl= new Boolean("true"); // bo ...
- rabbitmq启用和禁用web界面管理插件
rabbitmq默认安装启动以后,是没有开启web管理界面的,通过rabbitmq-plugins list命令可列出插件的启用和禁用状态. 使用rabbitmq-plugins enable xxx ...
- 洛谷P1062 数列 [2017年6月计划 数论03]
P1062 数列 题目描述 给定一个正整数k(3≤k≤15),把所有k的方幂及所有有限个互不相等的k的方幂之和构成一个递增的序列,例如,当k=3时,这个序列是: 1,3,4,9,10,12,13,… ...
- Unity 同一Text文本修改不同的字体大小和字体颜色
类似Html,在color和size对应的<>str</>中,就能修改str的相关属性, 下面的代码就是把time改为字体颜色为红色,大小为40: 而前面的"Time ...
- vue+ElementUI项目中,input只能输入正整数的验证
代码如下: <el-input v-model="famount" placeholder="请输入内容" @keyup.native=&q ...
- Django项目:CRM(客户关系管理系统)--02--01PerfectCRM基本配置ADMIN02
三.CRM项目表结构设计 from django.db import models # Create your models here. """ #运行 Terminal ...