Why does my web service produce an XML result for most mobile browsers instead of an HTML result?

本小妞迷上赌 提交于 2019-12-11 08:41:41

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/8878291/why-does-my-web-service-produce-an-xml-result-for-most-mobile-browsers-instead-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!