gpt4 book ai didi

java - 所需的请求正文内容缺失 : org. springframework.web.method.HandlerMethod$HandlerMethodParameter

转载 作者:IT老高 更新时间:2023-10-28 20:57:59 24 4
gpt4 key购买 nike

将 JSON 数据从 JSP 传递到 ResponseBody 中的 Controller 时出错。

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 调用:

$.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 MappingJackson2HttpMessageConverter mappingJacksonHttpMessageConverter = new MappingJackson2HttpMessageConverter();

List<HttpMessageConverter<?>> httpMessageConverter = new ArrayList<HttpMessageConverter<?>>();
httpMessageConverter.add(mappingJacksonHttpMessageConverter);

String[] supportedHttpMethods = { "POST", "GET", "HEAD" };

annotationMethodHandlerAdapter.setMessageConverters(httpMessageConverter);
annotationMethodHandlerAdapter.setSupportedMethods(supportedHttpMethods);

return annotationMethodHandlerAdapter;
}

请帮助我摆脱困境。我正在使用 Spring 4,jakson 2.3.0

如果我尝试 POST 请求,它会给出:org.springframework.web.HttpRequestMethodNotSupportedException:不支持请求方法“POST”

最佳答案

您不应发送带有 HTTP GET 请求的请求正文。您应该修改 addDepartment() 使其仅支持 POST,并将您的 JSON 发布到该端点。如果您想获取有关部门的信息,您应该创建一个单独的 Controller 方法来执行此操作(并且不需要请求正文)。

此外,请仔细检查您的端点定义,因为您在 $.ajax 调用中拼错了“reimbursement”。

关于java - 所需的请求正文内容缺失 : org. springframework.web.method.HandlerMethod$HandlerMethodParameter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29223683/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com