缺少必需的请求正文内容

缺少必需的请求正文内容

本文介绍了缺少必需的请求正文内容:org.springframework.web.method.HandlerMethod $ HandlerMethodParameter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ResponseBody中将JSON数据从JSP传递到控制器时出错。

Error to Pass JSON data from JSP to controller in 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电话:

$.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

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

please help me to get out out of that.I m using Spring 4, jakson 2.3.0

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

If i try to POST request it gives:org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported

推荐答案

对不起伙计们......实际上是因为我需要一个csrf令牌才能解决这个问题。
我已经实现了spring security并启用了csrf。通过ajax调用,我需要传递csrf令牌。

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.

这篇关于缺少必需的请求正文内容:org.springframework.web.method.HandlerMethod $ HandlerMethodParameter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 05:56