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

标签 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 。如何从接口(interface)获取参数query

最佳答案

你可以简单地做

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

"and want to set parameter query"

不太确定你的意思听到的,但如果你的意思是你想在运行时设置该值,我不太确定目的,也不太确定如何做到这一点。希望你们“得到”而不是“设置”:-)

关于java - 如何在 jersey WriterInterceptor 实现中获取 @interface 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30773160/

相关文章:

java - Cassandra 处理历史数据的最佳方式?

java - 在 finally block 中抛出异常

java - 自定义注释的使用,如何

java - 如何使用 Jersey 发布表单数据?

java - 警告 : The (sub)resource method contains empty path annotation

java - Java 的洗牌和数组问题

java - Spring 启动,maven : failing to run or package an application

java - 迭代所有具有特定注释的类

java - 在字节好友中生成参数注解

error-handling - 来自 Jersey 的Dropwizard错误消息