如何仅在特定方法中使用RESTEasy PreProcessInterceptor? [英] How to use RESTEasy PreProcessInterceptor only in specific methods?

查看:177
本文介绍了如何仅在特定方法中使用RESTEasy PreProcessInterceptor?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用RestEasy 2.3.4.Final编写REST API. 我知道拦截器将拦截我的所有请求,并且PreProcessInterceptor将是第一个(在所有操作之前)被调用.我想知道如何仅在调用特定方法时才能调用此Interceptor.

I'm writing a REST API, making use of RestEasy 2.3.4.Final. I know that a Interceptor will intercept all of my requests, and that a PreProcessInterceptor will be the first (before everything) to be called. I would like to know how can I make this Interceptor to be called just when specific methods are called.

我尝试同时使用PreProcessInterceptor和AcceptedByMethod,但无法读取所需的参数. 例如,仅在调用此方法时才需要运行Interceptor:

I tried to use both PreProcessInterceptor and AcceptedByMethod, but I was not able to read the parameters I need. For example, I need to run my Interceptor only when this method is called:

@GET
@Produces("application/json;charset=UTF8")
@Interceptors(MyInterceptor.class)
public List<City> listByName(@QueryParam("name") String name) {...}

更具体地说,我需要在所有具有@QueryParam("name")

To be more specific, I need to run my Interceptor in all methods whose have a @QueryParam("name")

在其签名上,以便我可以抓住这个名称并在做所有事情之前先做些事情.

on its signature, so that I can grab the name and do something before everything.

有可能吗?我试图在拦截器中捕获名称"参数,但我无法做到这一点.

Is it possible? I tried to catch the "name" parameter inside the Interceptor, but I was not able to do that.

有人可以帮我吗?

推荐答案

您可以按照创建一个同时实现PreProcessInterceptorAcceptedByMethod的类.在accept方法中,可以检查该方法是否具有带有@QueryParam("name")注释的参数.如果该方法具有该注释,请从accept方法返回true.

Create a class that implement both PreProcessInterceptor and AcceptedByMethod. In the accept-method, you can check if the method has a parameter with annotated with @QueryParam("name"). If the method has that annotation, return true from the accept-method.

preProcess方法中,您可以从request.getUri().getQueryParameters().getFirst("name")获取查询参数.

In the preProcess-method, you can get the query parameter from request.getUri().getQueryParameters().getFirst("name").

这里是一个例子:

public class InterceptorTest  {

    @Path("/")
    public static class MyService {

        @GET
        public String listByName(@QueryParam("name") String name){
            return "not-intercepted-" + name;
        }
    }

    public static class MyInterceptor implements PreProcessInterceptor, AcceptedByMethod {

        @Override
        public boolean accept(Class declaring, Method method) {
            for (Annotation[] annotations : method.getParameterAnnotations()) {
                for (Annotation annotation : annotations) {
                    if(annotation.annotationType() == QueryParam.class){
                        QueryParam queryParam = (QueryParam) annotation;
                        return queryParam.value().equals("name");
                    }
                }
            }
            return false;
        }

        @Override
        public ServerResponse preProcess(HttpRequest request, ResourceMethod method)
                throws Failure, WebApplicationException {

            String responseText = "intercepted-" + request.getUri().getQueryParameters().getFirst("name");
            return new ServerResponse(responseText, 200, new Headers<Object>());
        }
    }

    @Test
    public void test() throws Exception {
        Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
        dispatcher.getProviderFactory().getServerPreProcessInterceptorRegistry().register(new MyInterceptor());
        dispatcher.getRegistry().addSingletonResource(new MyService());

        MockHttpRequest request = MockHttpRequest.get("/?name=xxx");
        MockHttpResponse response = new MockHttpResponse();

        dispatcher.invoke(request, response);

        assertEquals("intercepted-xxx", response.getContentAsString());
    }
}

这篇关于如何仅在特定方法中使用RESTEasy PreProcessInterceptor?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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