是否可以从另一个调用一个jax-rs方法? [英] is it possible to call one jax-rs method from another?
问题描述
假设我有一些jax-rs资源类:
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class ResourceA {
@GET
public Something get(@Context UriInfo uriInfo) {
if (...) {
//how to get to ResourceB ?
}
}
}
我想有条件地将调用重定向到其他jax-rs资源:
public class ResourceB {
@GET
@Path("{identifier}")
public Other get(@PathParam("identifier")String someArg) {
}
}
我该怎么做? 请注意,我不希望客户端看到此消息(因此不进行http重定向),通常,我要重定向以不共享相同签名的资源方法(它们可能具有路径参数等,如我给出的示例).>
在apache tomcat下运行im jersey 2.6(如果有帮助的话,它是一个春季应用程序)
编辑-我正在寻找与解决方案
您可以使用ResourceContext如下获得它:
@Context
ResourceContext resourceContext;
这会将ResourceContext注入到您的Resource中.然后,您可以使用以下资源:
ResourceB b = resourceContext.getResource(ResourceB.class);
ResourceContext的Javadoc在这里一个>.您可以找到类似的问题这里
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
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
这篇关于是否可以从另一个调用一个jax-rs方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!