开发者

MediaType of REST

开发者 https://www.devze.com 2023-01-02 07:11 出处:网络
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 :

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.开发者_JAVA百科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 ?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消