2010-06-03 77 views
0

我是REST Web服務的初學者。REST的媒體類型

我編寫了一個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。

瀏覽器火狐總是excutes getHtml當URL或者是

http://localhost:8080/sources/htmlhttp://localhost:8080/sources/xml

但是IE總是excutes getXml()()。

如何在不同的瀏覽器中執行URL定義的正確方法?

+0

關於格式化的一點小心謹慎...... – skaffman 2010-06-03 08:59:12

回答

1

嘗試使用MediaType.APPLICATION_XML而不是TEXT_XML。這就是說,這不是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 
} 

將是一個更容易維護的方法。您也可以將JSP用於HTML。

查看http://java.dzone.com/articles/resteasy-spring是一個很好的例子(使用Spring)。

+0

非常感謝。我解決了這個問題。 我刪除@Path從類,並且每個方法前加入@Path,因爲這: @GET @Produces(MediaType.APPLICATION_XML) @Path( 「XML」) 公共字符串的getXML() @GET @Produces(MediaType.TEXT_HTML) @Path(「html」) public String getHtml() 現在它運行良好。 – 2010-06-03 13:26:34

+1

很高興爲您提供幫助。如果問題解決了,你應該接受一個答案,以便其他人知道你不再需要幫助。 – 2010-06-04 09:36:43

相關問題