• @RequestBody对象为空,异常Required request body is missing错误解决


    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
  • 相关阅读:
    vuex基础-Vuex是什么呢?
    (一)熟练HTML5+CSS3,每天复习一遍
    重学巩固你的Vuejs知识体系(下)
    重学巩固你的Vuejs知识体系(上)
    苹果上架的整个流程
    写给前端程序员的英文学习指南
    19期-当你在百度搜索关键字的时候,哪个网站会排在最前面?今天给大家科普一下“网站SEO”
    Linux命令入门篇(三)
    iOS面试基础知识 (二)
    iOS面试基础知识 (一)
  • 原文地址:https://www.cnblogs.com/jpfss/p/11194233.html
Copyright © 2020-2023  润新知