RestEASY 拦截器未被调用
我创建了一个 RestEASY 拦截器,允许我在 Web 服务调用完成后在 HTTP 响应上设置标头值。我的代码如下所示...
@Provider
@ServerInterceptor
@Precedence("HEADER_DECORATORS")
public class MyHeaderInterceptor implements
MessageBodyWriterInterceptor {
@Override
public void write(MessageBodyWriterContext context) throws IOException,
WebApplicationException {
....do stuff here....
}
}
但是,当我调用我的服务时,永远不会调用拦截器。我看到 Web 服务调用成功完成,但拦截器中的任何代码都没有执行。除此之外,我还需要做些什么来注册我的拦截器吗?是否必须在其他地方声明?是否需要包含任何特殊的 web.xml 参数?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须在 web.xml 的resteasy.providers 上下文参数中列出拦截器。向 Interceptor 类添加注释是不够的。
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.
至于 Resteasy 2.x,您还可以让它自动扫描 WEB-INF/lib jar 和 WEB-INF/classes 目录中的 @Provider 和 JAX-RS 资源类(@Path、@GET、@POST 等...)并注册它们:
或者可以让 Resteasy 扫描 @Provider 类并注册它们:
在这两种情况下,您都不必将拦截器显式列出到 web.xml 中。
否则,如果 context-params 'resteasy.scan' 和 'resteasy.scan.providers' 均未启用(并且默认情况下禁用),您可能需要指定要注册的完全限定 @Provider 类名的逗号分隔列表在“resteasy.providers”元素内:
取自文档:http://docs.jboss.org/resteasy/docs/2.3.3.Final/userguide/html_single/index.html#d0e72
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:
Or can have Resteasy to Scan for @Provider classes and register them :
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:
That's taken from the doc : http://docs.jboss.org/resteasy/docs/2.3.3.Final/userguide/html_single/index.html#d0e72