tags:

views:

42

answers:

1

Hi, 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 ?

A: 

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

Robert Wilson
Thank you very much. I resoved this problem.I removed @Path from class, and added @Path before each method, as this :@GET@Produces(MediaType.APPLICATION_XML)@Path("xml")public String getXml()@GET@Produces(MediaType.TEXT_HTML)@Path("html")public String getHtml()Now it runs well.
wonder garance
Glad to be of assistance. If the problem is solved you should accept an answer so others know you no longer require help.
Robert Wilson