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¶m2=value2
}
How can I do it?
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();
}
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
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 + "&";
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With