Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RestEASY Interceptor Not Being Called

I've created a RestEASY Interceptor to allow me to set header values on the HTTP response after my webservice call has completed. My code looks like this...

@Provider
@ServerInterceptor
@Precedence("HEADER_DECORATORS")
public class MyHeaderInterceptor implements
        MessageBodyWriterInterceptor {

    @Override
    public void write(MessageBodyWriterContext context) throws IOException,
            WebApplicationException {

             ....do stuff here....
        }
}

When I make a call to my service, however, the interceptor is never called. I see the webservice call complete successfully, but none of the code in my interceptor is ever executed. Is there anything beyond this that I need to do to register my interceptor? Does it have to be declared anywhere else? Are there any special web.xml parameters that need to be included?

like image 349
Shadowman Avatar asked Mar 17 '11 19:03

Shadowman


2 Answers

You have to list the interceptor in the resteasy.providers context-param of your web.xml. Adding annotation to the Interceptor class is not enough.

<context-param>
      <param-name>resteasy.providers</param-name>
      <param-value>org.resteasy.test.ejb.exception.FooExceptionMapper</param-value>
</context-param>
like image 166
Luciano Fiandesio Avatar answered Nov 06 '22 22:11

Luciano Fiandesio


As for Resteasy 2.x you could also have it to automatically scan WEB-INF/lib jars and WEB-INF/classes directory for both @Provider and JAX-RS resource classes (@Path, @GET, @POST etc..) and register them:

<context-param>
    <param-name>resteasy.scan</param-name>
    <param-value>true</param-value>
</context-param>

Or can have Resteasy to Scan for @Provider classes and register them :

<context-param>
    <param-name>resteasy.scan.providers</param-name>
    <param-value>true</param-value>
</context-param>

In both cases you dont have to list the interceptors explicitly into web.xml.

Otherwise if both context-params 'resteasy.scan' and 'resteasy.scan.providers' are not enabled (and they are disabled by default) you may want to specify a comma delimited list of fully qualified @Provider class names you want to register inside the 'resteasy.providers' element:

<context-param>
    <param-name>resteasy.providers</param-name>
    <param-value>com.test.Interceptor1,com.test.Interceptor2</param-value>
</context-param>

That's taken from the doc : http://docs.jboss.org/resteasy/docs/2.3.3.Final/userguide/html_single/index.html#d0e72

like image 35
Dmytro Pavliuchenko Avatar answered Nov 06 '22 22:11

Dmytro Pavliuchenko