如何获得球衣WriterInterceptor实施@interface参数 [英] How to get @interface parameter in jersey WriterInterceptor Implementation

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

问题描述

我有一个接口,例如:

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

我如何获得查询在拦截器实现参数?

  @Provider
@AutoLogged
公共类AutoLoggedInterceptor实现WriterInterceptor {
    @Context
    私人ResourceInfo中ResourceInfo中;    @覆盖
    公共无效aroundWriteTo(最终WriterInterceptorContext上下文)
            抛出IOException异常,WebApplicationException {
        尝试{
            最后弦乐方法名= this.resourceInfo.getResourceMethod()的getName()。
            BasicAutoLoggedProducer.makeCall(方法名);
        }赶上(最终例外五){
            e.printStackTrace(System.err的);
        } {最后
            context.proceed();
        }
    }
}

我不能context.getPropertyNames找到它。我见注释 AutoLogged 与getAnnotations方法。如何检索参数查询从接口?


解决方案

您可以简单地做

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


  

,并要设置参数查询


不完全相信你的意思听到的,但如果你的意思是你要在运行时设置的值,我真的不知道目的,真的不知道该怎么做。希望你男人的获取的而不是设置的: - )

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" :-)

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

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