15

suppose i have some jax-rs resource class:

@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class ResourceA {
   @GET
   public Something get(@Context UriInfo uriInfo) {
      if (...) {
         //how to get to ResourceB ?
      }
   }
}

and i want to conditionally redirect the call to some other jax-rs resource:

public class ResourceB {
   @GET
   @Path("{identifier}")
   public Other get(@PathParam("identifier")String someArg) {
   }
}

how do i do this? note that i dont want this to be visible to the client (so no http redirects) and generally the resource methods i want to redirect to dont share the same signature (they may have path params etc as in the example i gave).

im running jersey 2.6 under apache tomcat (its a spring app, if thats any help)

EDIT - im looking for a jax-rs equivalent of servlet forward. i dont want to do an extra http hop or worry abour instantiating resource classes myself

2 Answers 2

15

You can get it using ResourceContext as follows:

@Context 
ResourceContext resourceContext;

This will inject the ResourceContext into your Resource. You then get the resource you want using:

ResourceB b = resourceContext.getResource(ResourceB.class);

The Javadoc for ResourceContext is here. You can find a similar question here

6

I'm not aware of any possibility to do this from a resource method, but if it fits your use case, what you could do is implement your redirect logic in a pre matching request filter, for example like so:

@Provider
@PreMatching
public class RedirectFilter implements ContainerRequestFilter {

    @Override
    public void filter(ContainerRequestContext requestContext) {
        UriInfo uriInfo = requestContext.getUriInfo();
        String prefix = "/redirect";
        String path = uriInfo.getRequestUri().getPath();
        if (path.startsWith(prefix)) {
            String newPath = path.substring(prefix.length());
            URI newRequestURI = uriInfo.getBaseUriBuilder().path(newPath).build();
            requestContext.setRequestUri(newRequestURI);
        }
    }
}

This will redirect every request to /redirect/some/resource to /some/resource (or whatever you pass to requestContext.setRequestUri()) internally, before the resource method has been matched to the request and is executed and without http redirects or an additional internal http request.

3
  • nice suggestion, but unfortunately for me my routing logic depends on values calculated inside the resource method (from arguments/request body values etc) so i cant perform the redirection before.
    – radai
    Mar 10, 2014 at 14:51
  • @radai Gotcha, too bad. Note that you can access path and query parameters in a filter via UriInfo, but not form parameters/the request body (without consuming the body afaik).
    – jkovacs
    Mar 10, 2014 at 15:42
  • With CXF note that you need to use resource URI. ie setRequestUri(new Uri("/path/to/other")) (found in @Path("/path/to/other")) not the absolute URI. For example the above code in this answer don't work with CXF (2.7.18).
    – bric3
    Dec 7, 2015 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.