HttpMessageNotReadableException: Required request body is missing

使用 postman 请求服务异常:请求体消失

org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public com.xxx.demo.common.JsonRespModel> com.xxx.demo.controller.FreshMenController.getFreshMenSignedList(com.xxx.demo.model.search.FreshMenSearch) throws java.lang.Exception
	at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:161)
	at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:131)
	at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)
	at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:167)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:134)
	......

因为服务端接口声明 @RequestBody 从消息体中获取客户端参数信息,一直未能获取到,我将 @RequestBody 去掉就好使了。当然不可能将其删除,根据提示源码问题位置分析。

从源码提示错误位置分析:Object arg = readWithMessageConverters(inputMessage, parameter, paramType) 读取转换后的 Object arg 值为null, 就导致 org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing ,只能说明我请求的参数信息不规范所致。

package org.springframework.web.servlet.mvc.method.annotation;
public class RequestResponseBodyMethodProcessor{
  ...
	@Override
	protected  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;
	}
...
}

Header 声明的内容不规范,常见如下:

  • Content-Type :内容编码类型,客户端请求服务器端的数据格式;
  • Content-length:用于描述HTTP消息实体的传输长度;
  • Host: 指定访问站点
    举栗子:

主要针对一个IP对应多个域名的情况才有必要处理该参数,127.0.0.1
对应域名:http://server1、http://server2、http://server3,访问 http://server1
时,dns 解析IP:127.0.0.1,服务会根据 header 头部中 host 字段,使用 server1
这个域名的网站程序对请求做响应;

  • user-agent:访问者是通过什么工具来代理请求;
  • accept:内容编码类型,服务端响应客户端的数据格式;
  • accept-encoding:浏览器发给服务器,声明浏览器支持的编码类型;
  • connection:关于http 请求的是否保持长连接。
    HttpMessageNotReadableException: Required request body is missing_第1张图片
    将其声明后,请求接口正常
    HttpMessageNotReadableException: Required request body is missing_第2张图片

学习记录,希望能帮到各位!如有不对,请指正

你可能感兴趣的:(java)