Reputation: 2322
The goal is to create a rest-full web service using JAX-RS that will selectively return the result either in json or xml format, depending on the user request. For example, if the user issues a GET request in the following fashion the results will be returned in json format:
http://api.myurl.com/order/1234.json
Similarly, if the user issues a get in the following request, the results will be returned in xml format:
http://api.myurl.com/order/123.xml
I don't want to use request parameters to do this (i.e. http://api.myurl.com/order/123?format=json
). Using the .json
or .xml
post-fix seems more intuitive to me.
What would be the best strategy for doing this using the JAX-RS api?
Upvotes: 1
Views: 1574
Reputation: 137567
One way would be to use @Path
annotations more thoroughly:
@GET
@Path("/order/{id}.xml")
@Produces("application/xml")
public Order getOrderAsXML(@PathParam("id") int id) {
return realGetOrder(id);
}
@GET
@Path("/order/{id}.json")
@Produces("application/json")
public Order getOrderAsJSON(@PathParam("id") int id) {
return realGetOrder(id);
}
private Order realGetOrder(int id) {
// ...
}
However I'd be inclined to have a single method serving up both and let the client and supporting JAX-RS framework use content negotiation to decide the serialization method.
Upvotes: 2