简体   繁体   English

REST的MediaType

[英]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 / htmlhttp:// 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.

相关问题 Rest - 响应中的 MediaType 与请求中的相同 - Rest - Same MediaType in response as in request Jersey REST客户端 - 将自定义MediaType视为MediaType.APPLICATION_JSON - Jersey REST Client - Treat Custom MediaType as MediaType.APPLICATION_JSON Rest api,添加另一个MediaType,默认响应 - Rest api, adding another MediaType, default response Rest资源文件中的两种方法,它们的@Path相同,但mediaType输出不同 - Two methods in Rest resource file with same @Path but different mediaType output 向其他客户端使用的 REST api 端点添加另一个 MediaType - Adding another MediaType to REST api endpoint that is used by other clients 其余可以与@Produces一起使用MediaType.MULTIPART_FORM_DATA - Rest can MediaType.MULTIPART_FORM_DATA be used with @Produces Spring Mvc和MediaType用于@RequestMapping以获取get rest请求 - Spring Mvc and MediaType for consumes in @RequestMapping for a get rest request 无法返回 rest api 中媒体类型“application/xml”的对象列表 - Unable to return List of objects for the mediatype “application/xml” in rest api 如何将@Consumes(MediaType.APPLICATION_OCTET_STREAM) 接收到 java rest(jersey) 中? - how to receive @Consumes(MediaType.APPLICATION_OCTET_STREAM) into java rest(jersey)? 是否有任何方法可以对使用MediaType.MULTIPART_FORM_DATA进行文件上传的休息服务进行junit测试? - Is there any way to junit test a rest service that takes a MediaType.MULTIPART_FORM_DATA for file upload?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM