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

7

30

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

Inseverable answered 24/3, 2015 at 1:52 Comment(3)
Would you post your code for the AjaxResponse class also your url is "/BusinessReimbursment/addDepartment" but you request mapping is only "addDepartment", should it be "/addDepartment" or "/BusinessReimbursment/addDepartment".Sentinel
There is no issue in url. Because it work without requestBody.Inseverable
public class AjaxResponse { private boolean success; private String message; public boolean isSuccess() { return success; } public void setSuccess(boolean success) { this.success = success; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }Inseverable
I
1

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.

Inseverable answered 28/3, 2015 at 10:55 Comment(1)
Request method 'POST' not supported Invalid CSRF token found for http://localhost:8080/BusinessReimbursment/addDepartment DispatcherServlet with name 'dispatcher' processing POST request for [/BusinessReimbursment/403] Looking up handler method for path /403 HttpRequestMethodNotSupportedException: Request method 'POST' not supported HttpRequestMethodNotSupportedException: Request method 'POST' not supported HttpRequestMethodNotSupportedException: Request method 'POST' not supported WARN o.s.web.servlet.PageNotFound - Request method 'POST' not supported – Inseverable
M
34

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.

Mccurry answered 24/3, 2015 at 3:22 Comment(8)
I hv mention GET n POST in request mapping annotation. If I pass post response then it gives error which I hv mention in post..Inseverable
Please read my answer again. You can't use GET in your @RequestMapping annotation along with @RequestBody since a GET request cannot have a body. In general GET should only be used for read-only operations like getting a record or searching. Remove GET completely and try again.Mccurry
I changed Method: POST, and Removed RequestMethod:GET From @requestMapping annotion i get following error which i hv mention in post.Inseverable
Request method 'POST' not supported Invalid CSRF token found for http://localhost:8080/BusinessReimbursment/addDepartment DispatcherServlet with name 'dispatcher' processing POST request for [/BusinessReimbursment/403] Looking up handler method for path /403 HttpRequestMethodNotSupportedException: Request method 'POST' not supported HttpRequestMethodNotSupportedException: Request method 'POST' not supported HttpRequestMethodNotSupportedException: Request method 'POST' not supported WARN o.s.web.servlet.PageNotFound - Request method 'POST' not supportedInseverable
Invalid CSRF token is a completely different problem.Demosthenes
Since when you can't send a request body with an HTTP GET request? More here: https://mcmap.net/q/40417/-http-get-with-request-bodyImmortalize
I was incorrect about that in my answer from 4 years ago. If you'd like to submit an edit to change the word "can't" to "shouldn't", feel free.Mccurry
@Mccurry Actually you're right to some extent. The RestTemplate of the Spring framework does throw the message body away if the request is GET even if the HTTP spec no longer disallows this. I clearly saw it in its source code.Uniformed
N
19

I also had the same problem. I use "Postman" for JSON request. The code itself is not wrong. I simply set the content type to JSON (application/json) and it worked, as you can see on the image below

My Code

Northumberland answered 18/8, 2017 at 3:22 Comment(2)
Is this a question? If so, post it as a separate question. You can add a link to this question, if necessary.Florist
Next time, don't turn your whole post into a link, better add a link at the end. Also, OP didn't say anything about Postman. If you want to address this issue it seems like a good idea to show what was the particular mistake and how to reproduce the error.Florist
S
5

Try this:

@RequestBody(required = false) String str

Slat answered 6/6, 2018 at 20:28 Comment(3)
No, this will probably not helpDialectic
I don't understand why this worked, but it did and my requestbody went through were before it did not...Genagenappe
@Slat this is same as not using annotation at all and it would still work, not a solutionUnlace
S
3

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.

 $.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;
  }
Sentinel answered 25/3, 2015 at 4:2 Comment(0)
I
1

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.

Inseverable answered 28/3, 2015 at 10:55 Comment(1)
Request method 'POST' not supported Invalid CSRF token found for http://localhost:8080/BusinessReimbursment/addDepartment DispatcherServlet with name 'dispatcher' processing POST request for [/BusinessReimbursment/403] Looking up handler method for path /403 HttpRequestMethodNotSupportedException: Request method 'POST' not supported HttpRequestMethodNotSupportedException: Request method 'POST' not supported HttpRequestMethodNotSupportedException: Request method 'POST' not supported WARN o.s.web.servlet.PageNotFound - Request method 'POST' not supported – Inseverable
L
1

In my side, it is because POSTMAN setting issue, but I don't know why, maybe I copy a query from other. I simply create a new request in POSTMAN and run it, it works.

Leon answered 16/11, 2020 at 3:49 Comment(0)
N
0

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.RELEASE version.

Nepheline answered 24/4, 2020 at 6:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.