开发者

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,
            WebApplicationExcep开发者_如何学编程tion {

             ....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?


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>


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜