如何在jersey WriterInterceptor实现中获取@interface参数

时间:2015-06-11 06:24:11

标签: java annotations jersey jax-rs interceptor

我有一个界面,例如

@NameBinding
@Retention(RetentionPolicy.RUNTIME)
public @interface AutoLogged {
    boolean query() default false;
}

如何在拦截器实现中获取query参数?

@Provider
@AutoLogged
public class AutoLoggedInterceptor implements WriterInterceptor {
    @Context
    private ResourceInfo resourceInfo;

    @Override
    public void aroundWriteTo(final WriterInterceptorContext context)
            throws IOException, WebApplicationException {
        try {
            final String methodName = this.resourceInfo.getResourceMethod().getName();
            BasicAutoLoggedProducer.makeCall(methodName);
        } catch (final Exception e) {
            e.printStackTrace(System.err);
        } finally {
            context.proceed();
        }
    }
}

我在context.getPropertyNames中找不到它。我看到带有getAnnotations方法的注释AutoLogged。如何从界面中检索参数query

1 个答案:

答案 0 :(得分:1)

你可以简单地做

AutoLogged annotation = resourceInfo.getResourceMethod().getAnnotation(AutoLogged.class);
if (annotation != null) {
    boolean query = annotation.query();
}
  

“并想要设置参数query

不完全确定你的意思,但如果你的意思是你想在运行时设置值,我不是很确定目的而不确定如何去做。希望你的男人“得到”而不是“set”: - )