Could not find acceptable representation
The POST
request doesn't work because Spring doesn't know what kind of data it's expecting. So you will need to tell spring that you're expecting APPLICATION_JSON_VALUE
so it knows how to process. consumes=
will, as you probably guessed, tell Spring what the incoming POST
body context type.
@RequestMapping(value = "xyz", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody AbcDTO registerHotel(@RequestBody AbcDTO aaa) {
System.out.println(aaa.toString());
return aaa;
// I'm not able to map JSON into this Object
}
With PostMapping
@PostMapping(value = "xyz", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody AbcDTO registerHotel(@RequestBody AbcDTO aaa) {
System.out.println(aaa.toString());
return aaa;
// I'm not able to map JSON into this Object
}
As you can see I have also added something else called, produces=
this will instruct Spring how to format the response body of that request. So frontend receives JSON
formatted body, not just random text.
I just spent half a day on this error, and finally discovered that Spring's ContentNegotiationConfigurer by default favours the path extension if it's present. I had this particular mapping:
@PostMapping(value="/convert/{fileName:.+}",produces=MediaType.APPLICATION_JSON_VALUE)
public Map<String, String> convert(@PathVariable String fileName, @RequestBody String data) {
// Do conversion
}
Now when I posted to this controller with a filename "outputfile.pdf", Spring would simply assume the response had to be PDF, completely ignoring the "produces" parameter for the PostMapping.
The problem can be fixed fixed with ContentNegotiationConfigurer.favorPathExtension(false). As of Spring web 5.3 this should be the default, but still isn't.