Efren Efren - 3 months ago 23
JSON Question

How to Use Content Negotiation in Spring Data Rest?

I am trying to expose my data via Rest Api in difetents formats (xml,json,rdf,jsonld) and I am using Spring-Data-Rest-Framework and I know that it is posible using @RequestMapping in the Controller, buts in Spring Data Rest I only have a entity and a repository that use @RepositoryRestResource notation that does not support @RequestMapping notation.

Can somebody explain me the way to achieve expose my data in difente formats using Spring Data Rest ?

I would like to expecifice the format at the end of the URL like ( ?format=json or ?format=xml)

Answer

http://docs.spring.io/spring-data/rest/docs/current/reference/html/#representations.mapping

Currently, only JSON representations are supported. Other representation types can be supported in the future by adding an appropriate converter and updating the controller methods with the appropriate content-type.

You would have to build your own converter for XML or other types. As well the standard way to define content type is in the Accept header. If you want to use an override then you need to add a filter to read the query parameter and set the header.