Required request body content is missing: org.springframework.web.method.HandlerMethod$HandlerMethodParameter

chaitanya dalvi picture chaitanya dalvi · Mar 24, 2015 · Viewed 140.5k times · Source

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 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 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;
}

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

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

Answer

ach picture ach · Mar 24, 2015

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

Also, double-check your endpoint definitions since you have misspelled "reimbursement" in the $.ajax call.