RestEASY 拦截器未被调用

发布于 2024-10-24 01:42:21 字数 541 浏览 6 评论 0原文

我创建了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

誰認得朕 2024-10-31 01:42:22

您必须在 web.xml 的resteasy.providers 上下文参数中列出拦截器。向 Interceptor 类添加注释是不够的。

<context-param>
      <param-name>resteasy.providers</param-name>
      <param-value>org.resteasy.test.ejb.exception.FooExceptionMapper</param-value>
</context-param>

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>
朕就是辣么酷 2024-10-31 01:42:22

至于 Resteasy 2.x,您还可以让它自动扫描 WEB-INF/lib jar 和 WEB-INF/classes 目录中的 @Provider 和 JAX-RS 资源类(@Path、@GET、@POST 等...)并注册它们:

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

或者可以让 Resteasy 扫描 @Provider 类并注册它们:

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

在这两种情况下,您都不必将拦截器显式列出到 web.xml 中。

否则,如果 context-params 'resteasy.scan' 和 'resteasy.scan.providers' 均未启用(并且默认情况下禁用),您可能需要指定要注册的完全限定 @Provider 类名的逗号分隔列表在“resteasy.providers”元素内:

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

取自文档: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:

<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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文