繁体   English   中英

RestEASY拦截器未被调用

[英]RestEASY Interceptor Not Being Called

我创建了一个RestEASY拦截器,允许我在webservice调用完成后在HTTP响应上设置标头值。 我的代码看起来像这样......

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

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

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

但是,当我调用我的服务时,永远不会调用拦截器。 我看到webservice调用成功完成,但我的拦截器中的代码都没有被执行。 除此之外我还需要注册我的拦截器吗? 是否必须在其他任何地方宣布? 是否需要包含任何特殊的web.xml参数?

您必须在web.xml的resteasy.providers context-param中列出拦截器。 向Interceptor类添加注释是不够的。

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

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

<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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM