views:

29

answers:

1

Hello!

How can I get XML and/or URL (String) in JAX-RS service?

For example in GET method URL

@GET
@Produces("application/xml; charset=UTF-8")
public JaxrsPriceWrapper getPrice(@QueryParam("firstId"), @QueryParam("materialId"),...) {
    //here I would like to get whole URL
}

and in POST method XML

@POST
public JaxrsOrderWrapper insertOrder(OrderJaxrsVO jaxrsVO) {
    //here the XML
}
+3  A: 

This works for me using Jersey. Add a variable;

@Context private UriInfo uriInfo;

.. to your resource class. This will be made available to the resource methods. You can then call

uriInfo.getRequestURI().

Example;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;

@Path("/jerseytest")
public class Server
{
    @Context private UriInfo uriInfo;

    @GET
    @Produces(MediaType.APPLICATION_XML)
    public String get()
    {
        System.out.println("jerseytest called: URI = " + uriInfo.getRequestUri());

        return "<response>hello world</response>";
    }
}

Edit: You probably need to annotate your POST method with @Consumes(MediaType.APPLICATION_XML) to get the data posted.

Qwerky