java 如何仅在特定方法中使用 RESTEasy PreProcessInterceptor?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11370107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:48:08  来源:igfitidea点击:

How to use RESTEasy PreProcessInterceptor only in specific methods?

javarestresteasyinterceptor

提问by pulu

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.

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

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:

我尝试同时使用 PreProcessInterceptor 和 AcceptedByMethod,但无法读取所需的参数。例如,我只需要在调用此方法时运行我的拦截器:

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

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

更具体地说,我需要在所有具有 @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.

是否可以?我试图在拦截器中捕获“名称”参数,但我无法做到。

Could someone help me, please?

有人可以帮我吗?

采纳答案by eiden

You can use AcceptedByMethodas explained in the RESTEasy documentation

您可以AcceptedByMethod按照RESTEasy 文档中的说明使用

Create a class that implement both PreProcessInterceptorand 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.

创建一个同时实现PreProcessInterceptor和的类AcceptedByMethod。在accept-method 中,您可以检查该方法是否有带注释的参数@QueryParam("name")。如果该方法具有该注释,则从accept-method返回 true 。

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

preProcess- 方法中,您可以从request.getUri().getQueryParameters().getFirst("name").

EDIT:

编辑:

Here is an example:

下面是一个例子:

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

回答by maiello

if you return return new ServerResponse(responseText, 200, new Headers<Object>());you will lose the end-point. you need to return nullif you still want the message to be delivered to the final point.

如果您返回,return new ServerResponse(responseText, 200, new Headers<Object>());您将失去终点。null如果您仍然希望将消息传递到最后一点,则需要返回。