When using Spring, what is considered best practice when both JSON and XML is to be returned?
Say I have a method:
@RequestMapping(value="items/{id}", method = RequestMethod.GET)
public Item getItem(@PathVariable, Long id) {
// Find and return item.
}
How can I invoke this in a style like:
localhost:8080/app-name/items/1.xml and localhost:8080/app-name/items/1.json and get the data in their respective formats?
This is discussed in the Spring documentation here.
You can use two separate URLs to determine the type to return
You can use accept headers. However from a browser you are unable to set the accept headers. Therefore using separate URL’s gives more control.