如何在 jersey WriterInterceptor 实现中获取@interface 参数 [英] How to get @interface parameter in jersey WriterInterceptor Implementation

查看:24
本文介绍了如何在 jersey WriterInterceptor 实现中获取@interface 参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个接口,例如

@NameBinding@Retention(RetentionPolicy.RUNTIME)公共@interface AutoLogged {布尔查询()默认为假;}

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

@Provider@AutoLogged公共类 AutoLoggedInterceptor 实现了 WriterInterceptor {@语境私有 ResourceInfo 资源信息;@覆盖public void aroundWriteTo(final WriterInterceptorContext context)抛出 IOException,WebApplicationException {尝试 {final String methodName = this.resourceInfo.getResourceMethod().getName();BasicAutoLoggedProducer.makeCall(methodName);} 捕获(最终异常 e){e.printStackTrace(System.err);} 最后 {context.proceed();}}}

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

解决方案

你可以简单地做

AutoLogged annotation = resourceInfo.getResourceMethod().getAnnotation(AutoLogged.class);如果(注释!= null){布尔查询 = annotation.query();}

<块引用>

"并想设置参数query"

不完全确定你的意思是什么,但如果你的意思是你想在运行时设置值,我不太确定目的,也不确定如何去做.希望你们男人 "get" 而不是 "set" :-)

I have an interface, for example

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

How can I get query parameter in interceptor implementation?

@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();
        }
    }
}

I can not find it in context.getPropertyNames. I see annotation AutoLogged with getAnnotations method. How to retrieve parameter query from interface?

解决方案

You can simply do

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

"and want to set parameter query"

Not exactly sure what you mean hear, but if you mean you want to set the value at runtime, I'm not really sure the purpose and not really sure how to do it. Hopefully you men "get" instead of "set" :-)

这篇关于如何在 jersey WriterInterceptor 实现中获取@interface 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆