java - How to use RESTEasy PreProcessInterceptor only in specific methods? -


i'm writing rest api, making use of resteasy 2.3.4.final. know interceptor intercept of requests, , preprocessinterceptor first (before everything) called. know how can make interceptor called when specific methods called.

i tried use both preprocessinterceptor , acceptedbymethod, not able read parameters need. example, need run interceptor when method called:

@get @produces("application/json;charset=utf8") @interceptors(myinterceptor.class) public list<city> listbyname(@queryparam("name") string name) {...} 

to more specific, need run interceptor in methods have @queryparam("name")

on signature, can grab name , before everything.

is possible? tried catch "name" parameter inside interceptor, not able that.

could me, please?

you can use acceptedbymethod explained in resteasy documentation

create class implement both preprocessinterceptor , acceptedbymethod. in accept-method, can check if method has parameter annotated @queryparam("name"). if method has annotation, return true accept-method.

in preprocess-method, can query parameter request.geturi().getqueryparameters().getfirst("name").

edit:

here 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) { (annotation[] annotations : method.getparameterannotations()) { (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()); } } 

Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -