[英]MediaType of REST
I am beginner in REST web services. 我是REST Web服务的初学者。
I wrote a program of REST to display the HTML or XML. 我编写了一个REST程序来显示HTML或XML。 The @Path annotation's value is
@Path("{typeDocument}")
. @Path批注的值为
@Path("{typeDocument}")
。 There are two methods for GET : GET有两种方法:
@GET
@Produces(MediaType.TEXT_XML)
public String getXml(@PathParam("typeDocument") String typeDocument)
to display XML file, and 显示XML文件,以及
@GET
@Produces(MediaType.TEXT_HTML)
public String getHtml(@PathParam("typeDocument") String typeDocument)
to display HTML. 显示HTML。
The browser Firefox always excutes getHtml() when URL is either 当URL为或时,浏览器Firefox总是会执行getHtml()
http://localhost:8080/sources/html or http://localhost:8080/sources/xml http:// localhost:8080 / sources / html或http:// localhost:8080 / sources / xml
But IE always excutes getXml()
. 但是IE总是使用
getXml()
。
How to excute the correct method, as defined by URL, in different browser ? 如何在不同的浏览器中执行URL定义的正确方法?
try using MediaType.APPLICATION_XML instead of TEXT_XML. 尝试使用MediaType.APPLICATION_XML而不是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. 话虽如此,这并不是JAX-RS的最佳用法-特别是如果您正在使用RestEASY或任何其他具有JAXB支持的实现。
@GET
@Produces(MediaType.APPLICATION_XML)
@Path("/{typeDocument}")
public MyObject getXml(@PathParam("typeDocument") String typeDocument) {
myObjectService.get(typeDocument);
}
@XmlRootElement(name="myObject")
public class MyObject {
// Some properties
}
would be a much easier method to maintain. 将是一个更容易维护的方法。 You can also use JSPs for the HTML.
您还可以将JSP用于HTML。
See http://java.dzone.com/articles/resteasy-spring for a good example (using Spring). 参见http://java.dzone.com/articles/resteasy-spring作为一个很好的例子(使用Spring)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.