我有以下示例球衣应用程序,其中我使用HK2拦截具有自定义注释的休息端点以验证标头。 但似乎REST端点永远不会被拦截
调试时我可以看到MyInterceptionService类没有扫描端点类。
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomInterceptor {
}
@Path("/")
public class RestEndpoint{
@GET
@Path("intercepted")
@Produces({MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_JSON})
@ManagedAsync
@CustomInterceptor
public String getIntercepted() {
return "ServerResource: Intercepted method invoked\n";
}
}
@ApplicationPath("/")
public class Application extends ResourceConfig {
public Application() {
packages("com.example");
register(new MyInterceptionBinder());
}
}
public class MyInterceptionBinder extends AbstractBinder {
@Override
protected void configure() {
bind(MyInterceptionService.class)
.to(org.glassfish.hk2.api.InterceptionService.class)
.in(Singleton.class);
}
}
public class MyInterceptionService implements InterceptionService {
private final List<MethodInterceptor> interceptors = Collections.singletonList(new MyInterceptor());
@Override
public Filter getDescriptorFilter() {
return BuilderHelper.allFilter();
}
@Override
public List<MethodInterceptor> getMethodInterceptors(Method method) {
System.err.println("----"+method.getDeclaringClass());
if(method.isAnnotationPresent(CustomInterceptor.class)) {
return interceptors;
}
}
@Override
public List<ConstructorInterceptor> getConstructorInterceptors(Constructor<?> constructor) {
return null;
}
}
public class MyInterceptor implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
return invocation.proceed();
}
}
感谢任何建议。