Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible to call one jax-rs method from another?

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

like image 502
radai Avatar asked Mar 09 '14 08:03

radai


People also ask

Is Jax and Jersey RS the same?

JAX-RS is an specification (just a definition) and Jersey is a JAX-RS implementation. Jersey framework is more than the JAX-RS Reference Implementation. Jersey provides its own API that extend the JAX-RS toolkit with additional features and utilities to further simplify RESTful service and client development.

What can a JAX-RS method return?

If the URI path template variable cannot be cast to the specified type, the JAX-RS runtime returns an HTTP 400 (“Bad Request”) error to the client. If the @PathParam annotation cannot be cast to the specified type, the JAX-RS runtime returns an HTTP 404 (“Not Found”) error to the client.

Is Jax a Rs servlet?

Simply put, YES, the JAX-RS specification is built on top of Servlets, and any other deployment method (such as mentioned by @Jilles van Gurp) is implementation specific.

What is a JAX-RS resource?

JAX-RS is a Java programming language API designed to make it easy to develop applications that use the REST architecture. The JAX-RS API uses Java programming language annotations to simplify the development of RESTful web services.


2 Answers

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

like image 197
Harry Avatar answered Oct 26 '22 09:10

Harry


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.

like image 37
jkovacs Avatar answered Oct 26 '22 08:10

jkovacs