1

I have a restful web service resource for which I've defined both XML and HTML methods. For desktop browsers the service accurately produces HTML, and for the Android client I've written it accurately produces XML.

When it comes to mobile browsers however, the service produces XML, where it should produce HTML.

One method is annotated with

@Produces({MediaType.TEXT_HTML}) 

And the other with

@Produces({MediaType.APPLICATION_XML}) 

Do I need to look at more than the Accept header to get the desired result?

Update: The acceptable media types, intercepted from the mobile browser request, are:

INFO: application/xml

INFO: application/xhtml+xml

INFO: image/png

INFO: text/html; q=0.9

INFO: text/plain; q=0.8

INFO: /; q=0.5

2
  • 1
    You will have to intercept your RESTful service to see what your mobile request headers are. Commented Jan 16, 2012 at 10:08
  • @TheEliteGentleman. I have added the content of the accept header, does that tell you anything? Commented Jan 16, 2012 at 11:27

1 Answer 1

2

This post explains how browsers based on webkit (most mobile browsers), and apparently also IE use accept headers that prioritize XML over HTML. Based on that and this SO question, it seems better to not to rely solely on the accept header of the request, but instead combine it with URL-specified representation.

Another solution is to override the Accept preference of the client by appending a quality attribute to the @Produces declaration. If you make qs larger than 1:

@Produces({MediaType.TEXT_HTML+";qs=1.1"}) 

the preferences of the browser clients will be overridden. Then you will have to make the qs value even larger for other content types on the client side for clients that require that. I don't know if this approach is good practice, but it's what I went with.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.