例如,我有一个界面
@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/