body is missing request required_@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.gateway.controller.OrderController.listOrder(com.mljr.lease.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 @Override2 protected Object readWithMessageConverters(NativeWebRequest webRequest, MethodParameter parameter,3 Type paramType) throwsIOException, HttpMediaTypeNotSupportedException, HttpMessageNotReadableException {4

5 HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);6 ServletServerHttpRequest inputMessage = newServletServerHttpRequest(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 returnarg;16 }17

18 protected booleancheckRequired(MethodParameter parameter) {19 return (parameter.getParameterAnnotation(RequestBody.class).required() && !parameter.isOptional());20 }

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

1 public @interfaceRequestBody {2

3 /**

4 * Whether body content is required.5 *

Default is {@codetrue}, leading to an exception thrown in case6 * there is no body content. Switch this to {@codefalse} if you prefer7 * {@codenull} to be passed when the body content is {@codenull}.8 *@since3.29 */

10 boolean required() default true;11

12 }

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

3.解决方案:

1)@RequestBody (required=false)

2) 不要让DTO对象为空

原文:http://www.cnblogs.com/dennyzhangdd/p/7780221.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值