REST 的媒体类型
我是 REST Web 服务的初学者。
我编写了一个 REST 程序来显示 HTML 或 XML。 @Path 注释的值为 @Path("{typeDocument}")
。 GET 有两种方法:
@GET
@Produces(MediaType.TEXT_XML)
public String getXml(@PathParam("typeDocument") String typeDocument)
显示 XML 文件, 并
@GET
@Produces(MediaType.TEXT_HTML)
public String getHtml(@PathParam("typeDocument") String typeDocument)
显示 HTML。
当 URL 为
http://localhost:8080/sources/html 或 http://localhost:8080/sources/xml
但 IE 总是执行 getXml( )
。
如何在不同的浏览器中执行 URL 定义的正确方法?
I am beginner in REST web services.
I wrote a program of REST to display the HTML or XML. The @Path annotation's value is @Path("{typeDocument}")
. There are two methods for GET :
@GET
@Produces(MediaType.TEXT_XML)
public String getXml(@PathParam("typeDocument") String typeDocument)
to display XML file,
and
@GET
@Produces(MediaType.TEXT_HTML)
public String getHtml(@PathParam("typeDocument") String typeDocument)
to display HTML.
The browser Firefox always excutes getHtml() when URL is either
http://localhost:8080/sources/html or http://localhost:8080/sources/xml
But IE always excutes getXml()
.
How to excute the correct method, as defined by URL, in different browser ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用 MediaType.APPLICATION_XML 而不是 TEXT_XML。
话虽如此,这并不是 JAX-RS 的最佳用途 - 特别是如果您使用 RestEASY 或任何其他支持 JAXB 的实现。
将是一种更容易维护的方法。您还可以将 JSP 用于 HTML。
请参阅 http://java.dzone.com/articles/resteasy-spring 了解很好的例子(使用Spring)。
try using MediaType.APPLICATION_XML instead of TEXT_XML.
That being said, this isn't the best use of JAX-RS - especially if you're using RestEASY or any other implementation with JAXB support.
would be a much easier method to maintain. You can also use JSPs for the HTML.
See http://java.dzone.com/articles/resteasy-spring for a good example (using Spring).