Java 缺少所需的请求正文内容:org.springframework.web.method.HandlerMethod$HandlerMethodParameter
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29223683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Required request body content is missing: org.springframework.web.method.HandlerMethod$HandlerMethodParameter
提问by chaitanya dalvi
Error to Pass JSON data from JSP to controller in ResponseBody.
将 JSON 数据从 JSP 传递到 ResponseBody 中的控制器时出错。
07:13:53.919 DEBUG o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Resolving exception from handler [public com.chaitanya.ajax.AjaxResponse com.chaitanya.web.controller.DepartmentController.addDepartment(com.chaitanya.ajax.AjaxResponse)]:
org.springframework.http.converter.HttpMessageNotReadableException: Required request body content is missing: org.springframework.web.method.HandlerMethod$HandlerMethodParameter@98d8d36c
07:13:54.106 DEBUG o.s.w.s.m.a.ResponseStatusExceptionResolver - Resolving exception from handler [public com.chaitanya.ajax.AjaxResponse com.chaitanya.web.controller.DepartmentController.addDepartment(com.chaitanya.ajax.AjaxResponse)]: org.springframework.http.converter.HttpMessageNotReadableException: Required request body content is missing: org.springframework.web.method.HandlerMethod$HandlerMethodParameter@98d8d36c
07:13:54.125 DEBUG o.s.w.s.m.s.DefaultHandlerExceptionResolver - Resolving exception from handler [public com.chaitanya.ajax.AjaxResponse com.chaitanya.web.controller.DepartmentController.addDepartment(com.chaitanya.ajax.AjaxResponse)]: org.springframework.http.converter.HttpMessageNotReadableException: Required request body content is missing: org.springframework.web.method.HandlerMethod$HandlerMethodParameter@98d8d36c
07:1
Ajax Call:
阿贾克斯调用:
$.ajax({
url: "/BusinessReimbursment/addDepartment",
method: 'POST',
dataType: 'json',
data: "{\"message\":\"abc\",\"success\":true}",
contentType: 'application/json',
mimeType: 'application/json',
success: function(data) {
alert(data.id + " " + data.name);
commit(true);
},
error:function(data,status,er) {
alert("error: "+data+" status: "+status+" er:"+er);
}
});
Controller:
控制器:
@RestController
public class DepartmentController {
@Autowired
@Qualifier("departmentService")
private DepartmentService departmentService;
@RequestMapping(value="/addDepartment", method={RequestMethod.POST})
public @ResponseBody AjaxResponse addDepartment(@RequestBody AjaxResponse departmentDTO){
AjaxResponse response=new AjaxResponse();
return response;
}
AppConfig.java
应用程序配置文件
@Bean
@豆角,扁豆
public RequestMappingHandlerAdapter annotationMethodHandlerAdapter()
{
final RequestMappingHandlerAdapter annotationMethodHandlerAdapter = new RequestMappingHandlerAdapter();
final MappingHymanson2HttpMessageConverter mappingHymansonHttpMessageConverter = new MappingHymanson2HttpMessageConverter();
List<HttpMessageConverter<?>> httpMessageConverter = new ArrayList<HttpMessageConverter<?>>();
httpMessageConverter.add(mappingHymansonHttpMessageConverter);
String[] supportedHttpMethods = { "POST", "GET", "HEAD" };
annotationMethodHandlerAdapter.setMessageConverters(httpMessageConverter);
annotationMethodHandlerAdapter.setSupportedMethods(supportedHttpMethods);
return annotationMethodHandlerAdapter;
}
please help me to get out out of that. I m using Spring 4, jakson 2.3.0
请帮我摆脱困境。我正在使用 Spring 4, jakson 2.3.0
If i try to POST request it gives:org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported
如果我尝试 POST 请求,它会给出:org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported
采纳答案by chaitanya dalvi
Sorry guys.. actually because of a csrf token was needed I was getting that issue. I have implemented spring security and csrf is enable. And through ajax call I need to pass the csrf token.
抱歉,伙计们……实际上是因为需要 csrf 令牌,所以我遇到了这个问题。我已经实现了 spring 安全并且启用了 csrf。通过 ajax 调用,我需要传递 csrf 令牌。
回答by ach
You shouldn't send a request body with an HTTP GET request. You should modify addDepartment()
so that it only supports POST, and POST your JSON to that endpoint. If you want to GET information about a department, you should create a separate controller method that does that (and does not require a request body).
您不应发送带有 HTTP GET 请求的请求正文。您应该修改addDepartment()
以使其仅支持 POST,并将您的 JSON 发布到该端点。如果您想获取有关部门的信息,您应该创建一个单独的控制器方法来执行此操作(并且不需要请求正文)。
Also, double-check your endpoint definitions since you have misspelled "reimbursement" in the $.ajax
call.
此外,请仔细检查您的端点定义,因为您在$.ajax
呼叫中拼错了“reimbursement” 。
回答by faljbour
I made some minor modification to you code and tested it with a spring project that I have and it works, The logic will only work with POST if I use GET it throws an error with invalid request. Also in your ajax call I commented out commit(true), the browser debugger flagged and error that it is not defined. Just modify the url to fit your Spring project architecture.
我对您的代码进行了一些小的修改,并使用我拥有的 spring 项目对其进行了测试,并且可以正常工作,如果我使用 GET,则该逻辑仅适用于 POST,它会引发无效请求的错误。同样在你的 ajax 调用中,我注释掉了 commit(true),浏览器调试器标记了它没有定义的错误。只需修改 url 以适合您的 Spring 项目架构。
$.ajax({
url: "/addDepartment",
method: 'POST',
dataType: 'json',
data: "{\"message\":\"abc\",\"success\":true}",
contentType: 'application/json',
mimeType: 'application/json',
success: function(data) {
alert(data.success + " " + data.message);
//commit(true);
},
error:function(data,status,er) {
alert("error: "+data+" status: "+status+" er:"+er);
}
});
@RequestMapping(value="/addDepartment", method=RequestMethod.POST)
public AjaxResponse addDepartment(@RequestBody final AjaxResponse departmentDTO)
{
System.out.println("addDepartment: >>>>>>> "+departmentDTO);
AjaxResponse response=new AjaxResponse();
response.setSuccess(departmentDTO.isSuccess());
response.setMessage(departmentDTO.getMessage());
return response;
}
回答by Rujal Shrestha
回答by Juan
Try this:
尝试这个:
@RequestBody(required = false) String str
@RequestBody(required = false) 字符串 str
回答by Szabó Gábor
If it's working from Postman, try new Spring version, becouse the 'org.springframework.boot' 2.2.2.RELEASE version can throw "Required request body content is missing" exception.
Try 2.2.6.RELEASEversion.
如果它是从 Postman 工作的,请尝试新的 Spring 版本,因为 'org.springframework.boot' 2.2.2.RELEASE 版本会抛出“Required request body content is missing”异常。
尝试2.2.6.RELEASE版本。