在ResponseBody中将JSON数据从JSP传递到控制器时出错。
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);
}
});
控制器:
@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:请求方法&#39; POST&#39;不支持
答案 0 :(得分:25)
您不应发送带有HTTP GET请求的请求正文。您应该修改addDepartment()
以便它仅支持POST,并将您的JSON POST到该端点。如果要获取有关某个部门的信息,您应该创建一个单独的控制器方法来执行该操作(并且不需要请求主体)。
此外,仔细检查您的端点定义,因为您在$.ajax
电话中拼错了“报销”。
答案 1 :(得分:5)
答案 2 :(得分:2)
我对你的代码进行了一些小的修改,并用我拥有的弹簧项目对它进行了测试,并且它可以工作,如果我使用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;
}
答案 3 :(得分:2)
试试这个:
@RequestBody(required = false)String str
答案 4 :(得分:1)
对不起伙计们..实际上是因为我需要一个csrf令牌才能解决这个问题。 我已经实现了spring security并启用了csrf。通过ajax调用,我需要传递csrf标记。
答案 5 :(得分:1)
在我这方面,这是因为POSTMAN设置问题,但我不知道为什么,也许我是从其他人复制查询。我只是在POSTMAN中创建一个新请求并运行它,它就可以工作。
答案 6 :(得分:0)
如果它在Postman上运行,请尝试新的Spring版本,因为'org.springframework.boot'2.2.2.RELEASE版本会引发“缺少所需的请求正文内容”异常。
尝试 2.2.6.RELEASE 版本。