![](/img/trans.png)
[英]How can I get resource annotations in a Jersey ContainerResponseFilter
[英]How can I get resource annotations in a Jersey 2.4 filter?
我的問題與此問題基本相同: 我如何在Jersey ContainerResponseFilter中獲取資源注釋 。
但是我使用Java Jersey 2.4並且找不到ResourceFilterFactory或ResourceFilter類的任何標志。 文檔也沒有提到它們。 他們被棄用了還是真的被隱藏了? 如果他們已被棄用,我可以使用什么呢? 現在有一種方法可以使用Jersey 2.4和2.5從ContainerRequestFilter獲取資源注釋嗎?
謝謝
如果要根據資源方法/類上可用的注釋修改請求的處理,那么我建議使用JAX-RS 2.0中的DynamicFeature 。 使用DynamicFeature
您可以為可用資源方法的子集分配特定提供程序。 例如,考慮我有一個類似的資源類:
@Path("helloworld")
public class HelloWorldResource {
@GET
@Produces("text/plain")
public String getHello() {
return "Hello World!";
}
}
我想為它分配一個ContainerRequestFilter 。 我會創建:
@Provider
public class MyDynamicFeature implements DynamicFeature {
@Override
public void configure(final ResourceInfo resourceInfo, final FeatureContext context) {
if ("HelloWorldResource".equals(resourceInfo.getResourceClass().getSimpleName())
&& "getHello".equals(resourceInfo.getResourceMethod().getName())) {
context.register(MyContainerRequestFilter.class);
}
}
}
注冊后(如果你正在使用包掃描,那么你不需要注冊它以防它上面有@Provider
注釋) MyContainerRequestFilter
將與你的資源方法相關聯。
另一方面,您始終可以在過濾器中注入ResourceInfo (無法使用@PreMatching
進行注釋)並從中獲取注釋:
@Provider
public class MyContainerRequestFilter implements ContainerRequestFilter {
@Context
private ResourceInfo resourceInfo;
@Override
public void filter(final ContainerRequestContext requestContext) throws IOException {
resourceInfo.getResourceMethod().getDeclaredAnnotations();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.