关于加入@RequestBody后请求报错:
Required request body is missing:
这个错误是由于Controller中加入了@RequestBody后却收不到指定请求体而发生的,这时候就要检查前端或其它请求传入的数据了:
1-请求中需要的数据没有或不是JSON格式
2-请求方式是GET(@RequestBody不支持)
比如IDEA工具Restful Client,我们可以看到它的请求头Header、请求参数Request Parameters、Request Body是分开的。
而菜鸡作者以前用Restful Client的时候经常只用请求参数Request Parameters提交,用到@RequestBody后发现无法正常访问(需要的请求体丢失):
2020-07-23 14:01:12.262 ERROR 9896 --- [nio-8091-exec-3] c.maimeng.exception.RRExceptionHandler : Required request body is missing: public com.maimeng.common.utils.R com.maimeng.controller.UserController.login(com.maimeng.entity.User,java.lang.String)
org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public com.maimeng.common.utils.R com.maimeng.controller.UserController.login(com.maimeng.entity.User,java.lang.String)
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)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:888)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
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:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
此时我们在Restful Client右边的Request Body栏加上JSON数据便可正常访问了,但稍微提一下@Request Body可是不接收GET请求的