Reputation: 139
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?
Upvotes: 11
Views: 21124
Reputation: 1239
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();
}
Upvotes: 19
Reputation: 371
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
Upvotes: 7
Reputation: 2467
you can use Jersey filters.
public class HTTPFilter implements ContainerRequestFilter {
private static final Logger logger = LoggerFactory.getLogger(HTTPFilter.class);
@Override
public void filter(ContainerRequestContext containerRequestContext) throws IOException {
logger.info(containerRequestContext.getUriInfo().getPath() + " endpoint called...");
//logger.info(containerRequestContext.getUriInfo().getAbsolutePath() + " endpoint called...");
}
}
After that u must register it in http configuration file or just extend ResourceConfig class. This is how u can register it in http config class
public class HTTPServer {
public static final Logger logger = LoggerFactory.getLogger(HTTPServer.class);
public static void init() {
URI baseUri = UriBuilder.fromUri("http://localhost/").port(9191).build();
ResourceConfig config = new ResourceConfig(Endpoints.class, HTTPFilter.class);
HttpServer server = JdkHttpServerFactory.createHttpServer(baseUri, config);
logger.info("HTTP Server started");
}
}
Upvotes: 0
Reputation: 1829
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 + "&";
}
}
Upvotes: 4