是否可以从另一个调用一个 jax-rs 方法?方法、jax、rs

2023-09-06 11:44:42 作者:○會飛の煤氣罐﹏

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) {
   }
}
Serverless现状与未来 剑入佳境,无招胜有招

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

解决方案

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