Follwing the examples from Krams. What does the below return, will the Person object be converted to JSON or XML based on the header of submitted request ?
@RequestMapping(value = "/person",
method = RequestMethod.POST,
headers="Accept=application/xml, application/json")
public @ResponseBody Person addPerson(@RequestBody Person person) {
logger.debug("Provider has received request to add new person");
// Call service to here
return personService.add(person);
}
So when I submit data as json I get json back, and the same for xml. Or is something else going on ?
It depends on
Accepthttp request header. If it’s json, you get json, if it’s xml you get xml.That’s exactly what this part of your code says.:
You can send one mime type and receive another without problems.
edit
Both
headersandproduces/consumesparameters only say what can be produced/consumed by the requestmapping. They don’t force any particular serialization. The type of request/response is decided entirely in request headers. If the dispatcher doesn’t find mapping withproducesandconsumes(or headers) matching the request headers you’ll get an error.