簡體   English   中英

如何在Jersey WriterInterceptor實現中獲取@interface參數

[英]How to get @interface parameter in jersey WriterInterceptor Implementation

我有一個界面,例如

@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

你可以簡單地做

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

“並且要設置參數query

不能完全確定您想聽到的內容,但是如果您想在運行時設置值,則我不確定目標是什么,也不確定如何做到。 希望你們能“得到”而不是“設定” :-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM