Search code examples
spring-mvcannotations

@RequestBody optional (e.g. required=false)


Is there a way to make the @RequestBody annotation options (e.g. required=false) like RequestParam supports?

My main path through the code is with a POST. However, I'd like to also support a basic GET request via a browser basic http request for debugging purposes. However when I try to do that I get a 415 unsupported media error.


Solution

  • My main path through the code is with a POST. However, I'd like to also support a basic GET request via a browser

    To do this, use the method attribute of the @RequestMapping annotation, e.g.

    @RequestMapping(value="/myPath", method=RequestMethod.GET)
    public String doSomething() {
       ...
    }
    
    @RequestMapping(value="/myPath", method=RequestMethod.POST)
    public String doSomething(@RequestBody payload) {
       ...
    }