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.
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) {...}
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.
Could someone help me, please?
You can use
AcceptedByMethod
as explained in the RESTEasy documentationCreate a class that implement both
PreProcessInterceptor
andAcceptedByMethod
. In theaccept
-method, you can check if the method has a parameter with annotated with@QueryParam("name")
. If the method has that annotation, return true from theaccept
-method.In the
preProcess
-method, you can get the query parameter fromrequest.getUri().getQueryParameters().getFirst("name")
.EDIT:
Here is an example:
if you return
return new ServerResponse(responseText, 200, new Headers<Object>());
you will lose the end-point. you need to returnnull
if you still want the message to be delivered to the final point.