为什么我的Web服务为大多数移动浏览器而不是HTML结果生成XML结果? [英] Why does my web service produce an XML result for most mobile browsers instead of an HTML result?

查看:138
本文介绍了为什么我的Web服务为大多数移动浏览器而不是HTML结果生成XML结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个宁静的Web服务资源,我已经定义了XML和HTML方法。对于桌面浏览器,该服务可以准确地生成HTML,而对于Android客户端,我已经准确地生成了XML。

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.

但是,对于移动浏览器,该服务会生成XML,它应该生成HTML。

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

方法注释为

@Produces({MediaType.TEXT_HTML})

另一个

@Produces({MediaType.APPLICATION_XML})

我是否需要查看多于Accept标头以获得所需的结果?

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/xml

INFO:application / xhtml + xml

INFO: application/xhtml+xml

INFO:image / png

INFO: image/png

INFO:text / html; q = 0.9

INFO: text/html; q=0.9

INFO:text / plain; q = 0.8

INFO: text/plain; q=0.8

INFO: / ; q = 0.5

INFO: /; q=0.5

推荐答案

这篇帖子解释了基于webkit的浏览器(大多数移动浏览器),以及IE显然也使用接受标头来优先考虑XML而不是HTML。基于此和那么问题,似乎最好不要仅依赖请求的接受标头,而是将其与URL指定的表示结合起来。

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.

另一种解决方案是覆盖通过将质量属性附加到@Produces声明来接受客户端的首选项。如果你使qs大于1:

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"})

将覆盖浏览器客户端的首选项。然后,对于需要的客户端,您必须为客户端的其他内容类型设置更大的qs值。我不知道这种方法是不是很好的做法,但这就是我的用法。

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.

这篇关于为什么我的Web服务为大多数移动浏览器而不是HTML结果生成XML结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆