body is missing request required_@RequestBody 异常:Required request body is missing

一、异常信息:

"exception": "org.springframework.http.converter.HttpMessageNotReadableException",

"message": "Required request body is missing: public

com.game.manager.security.web.vo.response.Responsecom.game.manager.security.web.controller.RoleCont

roller.listRole(com.game.manager.security.web.vo.request.ListRoleReq)"

Controller代码:

@ApiOperation(tags="获取所有角色信息",value="获取所有角色信息")

@GetMapping("/listRole")

@ResponseBody

public Response listRole(@RequestBody ListRoleReq listRoleReq){

return Response.success("");

}

三、源码分析:

异常的抛出点在RequestResponseBodyMethodProcessor的readWithMessageConverters方法,

@Override

protected Object readWithMessageConverters(NativeWebRequest webRequest, MethodParameter parameter,

Type paramType) throws IOException, HttpMediaTypeNotSupportedException, HttpMessageNotReadableException {

HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);

ServletServerHttpRequest inputMessage = new ServletServerHttpRequest(servletRequest);

Object arg = readWithMessageConverters(inputMessage, parameter, paramType);

if (arg == null) {

if (checkRequired(parameter)) {

throw new HttpMessageNotReadableException("Required request body is missing: " +

parameter.getMethod().toGenericString());

}

}

return arg;

}

而arg 的返回时在AbstractMessageConverterMethodArgumentResolver的readWithMessageConverters进行。

@SuppressWarnings("unchecked")

protected Object readWithMessageConverters(HttpInputMessage inputMessage, MethodParameter parameter,

Type targetType) throws IOException, HttpMediaTypeNotSupportedException, HttpMessageNotReadableException {

MediaType contentType;

boolean noContentType = false;

try {

contentType = inputMessage.getHeaders().getContentType();

}

catch (InvalidMediaTypeException ex) {

throw new HttpMediaTypeNotSupportedException(ex.getMessage());

}

if (contentType == null) {

noContentType = true;

contentType = MediaType.APPLICATION_OCTET_STREAM;

}

Class> contextClass = (parameter != null ? parameter.getContainingClass() : null);

Class targetClass = (targetType instanceof Class ? (Class) targetType : null);

if (targetClass == null) {

ResolvableType resolvableType = (parameter != null ?

ResolvableType.forMethodParameter(parameter) : ResolvableType.forType(targetType));

targetClass = (Class) resolvableType.resolve();

}

HttpMethod httpMethod = ((HttpRequest) inputMessage).getMethod();

Object body = NO_VALUE;

try {

inputMessage = new EmptyBodyCheckingHttpInputMessage(inputMessage);

for (HttpMessageConverter> converter : this.messageConverters) {

Class> converterType = (Class>) converter.getClass();

if (converter instanceof GenericHttpMessageConverter) {

GenericHttpMessageConverter> genericConverter = (GenericHttpMessageConverter>) converter;

if (genericConverter.canRead(targetType, contextClass, contentType)) {

if (logger.isDebugEnabled()) {

logger.debug("Read [" + targetType + "] as \"" + contentType + "\" with [" + converter + "]");

}

if (inputMessage.getBody() != null) {

inputMessage = getAdvice().beforeBodyRead(inputMessage, parameter, targetType, converterType);

body = genericConverter.read(targetType, contextClass, inputMessage);

body = getAdvice().afterBodyRead(body, inputMessage, parameter, targetType, converterType);

}

else {

body = getAdvice().handleEmptyBody(null, inputMessage, parameter, targetType, converterType);

}

break;

}

}

else if (targetClass != null) {

if (converter.canRead(targetClass, contentType)) {

if (logger.isDebugEnabled()) {

logger.debug("Read [" + targetType + "] as \"" + contentType + "\" with [" + converter + "]");

}

if (inputMessage.getBody() != null) {

inputMessage = getAdvice().beforeBodyRead(inputMessage, parameter, targetType, converterType);

body = ((HttpMessageConverter) converter).read(targetClass, inputMessage);

body = getAdvice().afterBodyRead(body, inputMessage, parameter, targetType, converterType);

}

else {

body = getAdvice().handleEmptyBody(null, inputMessage, parameter, targetType, converterType);

}

break;

}

}

}

}

catch (IOException ex) {

throw new HttpMessageNotReadableException("I/O error while reading input message", ex);

}

if (body == NO_VALUE) {

if (httpMethod == null || !SUPPORTED_METHODS.contains(httpMethod) ||

(noContentType && inputMessage.getBody() == null)) {

return null;

}

throw new HttpMediaTypeNotSupportedException(contentType, this.allSupportedMediaTypes);

}

return body;

}

由于我们的ContenType是Json格式,因此由JsonConvert进行处理。而GET方法请求试没有传输body因此无法获取body信息。导出返回的body为null,因此抛出异常。

二、导致原因

通过源码的分析我们得出。因为GET方法请求试没有传输body信息。导致Spring处理body的时候为null,抛出异常。只用我们将GET方法修改成POST方法即可访问。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`RuntimeException: Required request body is missing` 这个错误通常出现在HTTP请求处理中,特别是当你使用的是需要提交数据(请求体)但实际请求中没有提供请求体的情况下。在Web服务或API开发中,许多API都是基于HTTP协议的,并且可能期望客户端在POST、PUT等方法中包含一个有效的请求体,这个体通常用于携带数据或者请求的操作参数。 具体来说,这个异常可能是因为: 1. 使用了需要请求体的HTTP方法(如POST或PUT),但请求头没有设置`Content-Type`(比如默认的`application/x-www-form-urlencoded`或`application/json`),也没有发送任何数据。 2. 你正在使用支持JSON或表单数据格式的API,但请求中没有包含JSON对象或者表单数据。 3. 在使用某些库或框架,如Spring MVC或Express.js,没有正确设置或填充请求体,导致服务器无法识别请求内容。 解决这个问题的方法包括: 1. 确保你的请求包含了正确的`Content-Type`,如果是JSON,可能是`application/json`。 2. 对于POST或PUT请求,检查是否正确构造并发送了JSON数据或表单数据。 3. 如果是在使用某个库或框架,查阅其文档,看看如何正确地构建和发送请求。 相关问题: 1. 如何在Java中设置HTTP POST请求体? 2. 使用什么工具可以在浏览器开发者工具中检查HTTP请求的头部和主体? 3. 如何在Node.js中用Express处理带有请求体的HTTP请求?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值