REST 的媒体类型

发布于 2024-09-04 00:54:58 字数 745 浏览 2 评论 0原文

我是 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/htmlhttp://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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的往事 2024-09-11 00:54:58

尝试使用 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)。

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.

@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.

See http://java.dzone.com/articles/resteasy-spring for a good example (using Spring).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文