Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java jersey get full URL

Tags:

java

url

jersey

I need to do a proxy API service with Jersey. I need to have full request URL in jersey method. I don't want to specify all possible parameters.

For example:

@GET
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Path("/media.json")
public  String getMedia( ){
    // here I want to get the full request URL like /media.json?param1=value1&param2=value2
}

How can I do it?

like image 550
Denys Romaniuk Avatar asked Oct 25 '13 12:10

Denys Romaniuk


3 Answers

In Jersey 2.x (note that it uses HttpServletRequest object):

@GET
@Path("/test")
public Response test(@Context HttpServletRequest request) {
    String url = request.getRequestURL().toString();
    String query = request.getQueryString();
    String reqString = url + "?" + query;
    return Response.status(Status.OK).entity(reqString).build();
}
like image 114
Alexmelyon Avatar answered Oct 19 '22 05:10

Alexmelyon


try UriInfo as follow ,

    @POST
    @Consumes({ MediaType.APPLICATION_JSON})
    @Produces({ MediaType.APPLICATION_JSON})
    @Path("add")
    public Response addSuggestionAndFeedback(@Context UriInfo uriInfo, Student student) {

            System.out.println(uriInfo.getAbsolutePath());

      .........
    }

OUT PUT:- https://localhost:9091/api/suggestionandfeedback/add

you can try following options also

enter image description here

like image 35
Buddhika Alwis Avatar answered Oct 19 '22 07:10

Buddhika Alwis


If you need a smart proxy, you can get parameters, filter them and create a new url.

@GET
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Path("/media.json")
public  String getMedia(@Context HttpServletRequest hsr){
    Enumeration parameters = hsr.getParameterNames();
    while (parameters.hasMoreElements()) {
        String key = (String) parameters.nextElement();
        String value = hsr.getParameter(key);
    //Here you can add values to a new string: key + "=" + value + "&"; 
    }

}
like image 26
Azee Avatar answered Oct 19 '22 06:10

Azee