I want to create a REST service that can return either JSON or XML. What request parameter do I set in the request to request a certain mime type? I know how to set it in the response, but there must be a way to request a certain one. Currently I do it in the URL
restServlet/engine/2WS2345
jsonServlet/engine/2WS2345
This gets me json or xml. But I thought I read that there is a parameter to set in the request. I am using JAVA...
You can do this with Restlet using annotations in your code and either let the content-negotiation operate depending on the user-agent's
Accept
header or specify the extension in the URI (using Restlet's TunnelService and MetadataService). Here is an example (based on Restlet 2):Content-negotiation works via the Accept header:
curl -H "Accept: text/plain" http://localhost:8182/test
returnsHello!
curl -H "Accept: application/xml" http://localhost:8182/test
returns<test>Hello</test>
It also works via the extension (thanks to
getTunnelService().setExtensionsTunnel(true)
):curl http://localhost:8182/test.txt
returnsHello!
curl http://localhost:8182/test.xml
returns<test>Hello</test>
There's a default list of extension to media-type mapping, but this can be configured via the MetadataService.
If you are using jersey you can easily configure the method with @Produces annotation. @Produces({"application/xml", "application/json"})
Good thing is you can still have a JAXB object as a return type. It will automatically be changed to the required format. Unless MIME type is specified in the Accept Header it will always send xml in the above case.
Ref http://jersey.java.net/nonav/documentation/1.6/user-guide.html