We plan to use content type negitiation as a form of versioning for our rest API but it seems more complex than it should be. Given the follwing example code :
@RestController
@RequestMapping("/products")
ProductController {
.......
@RequestMapping(value = "/{productID}", method = RequestMethod.GET, produces = "productVersion2/json")
public ResponseEntity<ProductV2> getVersion2(@PathVariable String productID) {
.......
return new ResponseEntity<ProductV2>(product, HttpStatus.OK);
The correct method is being called when test this from e.g postman, but i get a HTTP 406 Not Acceptable as a response. I have been looking several places, but I have not found a a page with a good explanation of what i need to do to make this work.
The response is to be parsed with json just like all other requests, but the response object is modified. The thought is that we by doing this can support several "versions" of the same API and we can gradually make clients move over to the new api, while still having the same uri/resource to access.
Can anyone point to a good tutirial or a step by step guide of how this can be solved in spring boot ?
I have read this article : http://spring.io/blog/2013/05/11/content-negotiation-using-spring-mvc/#combined-controller but I was not able to get a clear anderstanding of what was needed to make it work
Your media type is wrong. To use custom media types for API versioning you could use application/productVersion2+json
instead of productVersion2/json
. I suspect you get the 406 because Spring Boot has no way to find out how to serialize your object into JSON because productVersion2/json
isn't a valid media type for json data.
There is more than one way to pick a media type to do this, I've googled a more comprehensive document here.