Required request body is missing的问题及解决

 更新时间:2023年12月01日 09:43:06   作者:三省同学  
这篇文章主要介绍了Required request body is missing的问题及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

报错

使用@RequestBody注解参数

@PostMapping("/info")
InfoDTO saveInfo(@RequestBody InfoDTO InfoDTO);

请求不传任何参数时,有如下错误

Required request body is missing: xxx throws java.lang.InterruptedException
org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:163)
org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:133)
org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)
org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:179)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:146)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1067)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
javax.servlet.http.HttpServlet.service(HttpServlet.java:645)
...

原因

查看源码

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
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,在并没有正文内容的情况下引发异常。

如果希望在正文内容为null时传递null,请将其切换为false。

源码报错位置:

RequestResponseBodyMethodProcessor

protected <T> Object readWithMessageConverters(NativeWebRequest webRequest, MethodParameter parameter,
		Type paramType) throws IOException, HttpMediaTypeNotSupportedException, HttpMessageNotReadableException {

	HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
	Assert.state(servletRequest != null, "No HttpServletRequest");
	ServletServerHttpRequest inputMessage = new ServletServerHttpRequest(servletRequest);

	Object arg = readWithMessageConverters(inputMessage, parameter, paramType);
	if (arg == null && checkRequired(parameter)) {
		throw new HttpMessageNotReadableException("Required request body is missing: " +
				parameter.getExecutable().toGenericString(), inputMessage);
	}
	return arg;
}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • 详解使用spring aop实现业务层mysql 读写分离

    详解使用spring aop实现业务层mysql 读写分离

    本篇文章主要介绍了使用spring aop实现业务层mysql 读写分离,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-01-01
  • Java中一个for语句导致无穷大死循环的例子

    Java中一个for语句导致无穷大死循环的例子

    这篇文章主要介绍了Java中一个for语句导致无穷大死循环的例子,本文给出的是一个很特别的例子,这个例子会跟你所想的结果不一样,需要的朋友可以参考下
    2015-06-06
  • Java通过 Socket 实现 TCP服务端

    Java通过 Socket 实现 TCP服务端

    这篇文章主要介绍了Java通过 Socket 实现 TCP服务端的相关资料,需要的朋友可以参考下
    2017-05-05
  • Spring Boot实现WebSocket实时通信

    Spring Boot实现WebSocket实时通信

    本文主要介绍了Spring Boot实现WebSocket实时通信,包含实现实时消息传递和群发消息等功能,具有一定的参考价值,感兴趣的可以了解一下
    2024-05-05
  • JavaMail实现带附件的邮件发送

    JavaMail实现带附件的邮件发送

    这篇文章主要为大家详细介绍了JavaMail实现带附件的邮件发送,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-08-08
  • Java中使用trim()无法去除空格的解决方法

    Java中使用trim()无法去除空格的解决方法

    这篇文章主要介绍了Java中使用trim()方法无法去除空格的问题,并分析了原因,文章提出了两种解决方案:使用正则表达式替换和使用String.strip()方法,需要的朋友可以参考下
    2025-12-12
  • Springboot动态配置AOP切点详解

    Springboot动态配置AOP切点详解

    这篇文章主要介绍了Springboot动态配置AOP切点详解,Springboot 可以定义注解切点去拦截注解修饰的类方法以及execution(xxxx)切点去拦截具体的类方法,默认情况下我们都会使用注解@PointCut去定义切点,然后定义切面拦截切点,需要的朋友可以参考下
    2023-09-09
  • Java使用HashMap实现并查集

    Java使用HashMap实现并查集

    这篇文章主要为大家详细介绍了Java使用HashMap实现并查集,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-11-11
  • Elasticsearch 在 Java 中的使用示例教程

    Elasticsearch 在 Java 中的使用示例教程

    本文介绍了如何在Java中使用Elasticsearch,涵盖了连接、基本CRUD 操作、复杂查询、索引管理和聚合操作等方面的内容,通过示例,开发者可以一步步地掌握如何在 Java 项目中集成 Elasticsearch,并利用其强大的搜索和分析功能来构建高效的应用程序,感兴趣的朋友一起看看吧
    2024-08-08
  • java接收ios文件上传的示例代码

    java接收ios文件上传的示例代码

    这篇文章主要为大家详细介绍了java接收ios文件上传的示例代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-05-05

最新评论