I read a lot about versioning REST APIs, f.e. in this thread: Best practices for API versioning?
Because of that I would like to use the HTTP-Accept-Header to indicate which version the client is asking for.
But how can I apply this in my application? Which changes had therefore be made? How does the marshaller know, which version should be used? Do I have to register my type?
What I know is that I have to change the content of the @Produces-Annotation
@GET
@Path("/locations")
@Produces("application/vnd.mycompany-v1+xml")
Location[] getLocations();
But what else has to be changed?
You can use the
Variantmechanisms of JAX-RS.See the Java EE 6 Tutorial, too.
Edit
There is no automatic way to marshal an entity according to the selected variant. This requires some manual work. For example:
If the versions are different enough, I’d use a JAXB annotated class for each version. Each such class would then only contain those properties that are valid for this version. JAX-RS takes care to marshal them to JSON.