Take the 2-minute tour ×

I have a jax-rs rest service for which I've written an Android client.

The rest service exposes methods to CRUD reports. A http get request returns a list of reports in an xml structure.

Now, if I access the service from a browser, I want the http get request to return html instead of xml.

Can I overload the method to return different results for different clients or do I have to implement a separate rest method for browser clients?

I also considered using the same xml structure for both and including an xml stylesheet for the browser, but I can't figure out how to put the xslt link in the autogenerated xml.

share|improve this question
add comment

3 Answers

Use the HTTP Accept Request Header

share|improve this answer
This seems to be what I'm looking for, but could you clarify for me? If the request comes from the browser, is the preferred accept mime type normally html? –  Videre Dec 20 '11 at 12:13
The accept header your Android client sends should indicate a strong preference for application/xml. Browsers will indicate either a preference for text/html or give that and XML equal weight with */*. If the service has a weak preference for HTML it will deliver HTML to browsers and XML to your client. –  Quentin Dec 20 '11 at 12:15
add comment
up vote 1 down vote accepted

While Quentin's answer is a good pointer in the right direction, it is not jax-rs specific.

The jax-rs specification defines the annotation @Produces in section 3.5

Application classes can declare the supported request and response media types using the @Consumes and 23 @Produces annotations respectively. These annotations MAY be applied to a resource method, a resource 24 class, or to an entity provider (see section 4.2.3). Use of these annotations on a resource method overrides 25 any on the resource class or on an entity provider for a method argument or return type. In the absence of 26 either of these annotations, support for any media type (“/”) is assumed.

The mime type declared with @Produces is automatically matched with the HTTP Accept Request Header of the request. That means that to differentiate between client requests, you can define resource methods with different @Produces declarations.

In my case I needed a method that returned xml and a method that returned html, so I declared two methods in the resource /reports:

public String getReportsAsHtml(){...}


public List<Report> getReportsAsXml() {...}

Please refer to Quentin's comment about Accept Header preferences for the final part of the puzzle

share|improve this answer
add comment

Just send it as XML all the time and use a stylesheet. In this case it sounds like the stylesheet can be pretty primitive, so just use CSS. Here is an example of an XML document that uses a CSS stylesheet and where that stylesheet is backwards compatible to older versions of IE.


Notice in the stylesheet the duplication to counter the unnecessary mention of namespace declarations. That is how I was able to achieve compatibility with IE.

share|improve this answer
add comment

Your Answer


