I have a method to which I want to post some json data, that looks like this
@RequestMapping(value = "/m1", method = RequestMethod.POST)
public Object m1(@RequestBody Map<String, ?> body) {
// do something
}
This works great when I set the content-type header to application/json when I post, but fails with an error if I don’t (it cannot deserialize the post body into the map because it doesn’t know how)
What would I have to configure in spring to make it use application/json as a default when no header is specified?
The class that converts the JSON to your object is called an
HttpMessageConverter. I assume you are using the default Jackson one that comes with Spring. You can write a customMessageConverter, that will always return true in it’s supports method with your response object type and then just call the Jackson httpconverter in your readInternal and writeInternal methods. If you do this however, be careful, as once it’s registered in your requesthandler, it will be asked on all@ResponseBodyand@RequestBodyrequests.