此内容没有您所选择的语言版本。

28.7. Interceptor Ordering and Precedence


Some interceptors are sensitive to the order in which they are invoked. For example, your security interceptor should always be invoked first. Other interceptors' behavior can be triggered by an interceptor that adds a header. By default, you have no control over the order in which registered interceptors are invoked, but you can specify interceptor precedence.
Interceptor precedence is not specified by listing interceptor classes. Instead, a particular interceptor class is associated with a precedence family with the @org.jboss.resteasy.annotations.interception.Precedence annotation. Specifying precedence through a family structure protects the built-in interceptors that are sensitive to ordering and simplifies configuration.
The families are listed here in execution order:
    SECURITY
    HEADER_DECORATOR
    ENCODER
    REDIRECT
    DECODER
Copy to Clipboard Toggle word wrap
Any interceptor not associated with a precedence family will be invoked last. SECURITY usually includes PreProcessInterceptors. These should be invoked first so that as little as possible occurs prior to authorization. HEADER_DECORATORs are interceptors that add headers to a response or an outgoing request. These are next in precedence because the added headers may affect the behavior of other interceptors. ENCODER interceptors change the OutputStream. For example, the GZIP interceptor creates a GZIPOutputStream to wrap the real OutputStream for compression. REDIRECT interceptors are usually used in PreProcessInterceptors because they can reroute the request and bypass the JAX-RS method. DECODER interceptors wrap the InputStream. For example, the GZIP interceptor decoder wraps the InputStream in a GzipInputStream instance.
To associate your custom interceptors with a particular family, annotate it with @org.jboss.resteasy.annotations.interception.Precendence annotation.
@Provider
@ServerInterceptor
@ClientInterceptor
@Precedence("ENCODER")
public class MyCompressionInterceptor implements MessageBodyWriterInterceptor {...}
Copy to Clipboard Toggle word wrap
There are convenience annotations in the org.jboss.resteasy.annotations.interception package to provide complete type safety: @DecoredPrecedence, @EncoderPrecedence, @HeaderDecoratorPrecedence, @RedirectPrecedence, and @SecurityPrecedence. Use these instead of the @Precedence annotation

28.7.1. Custom Precedence

You can define your own precedence families and apply them with the @Precedence annotation.
 @Provider
 @ServerInterceptor
 @Precedence("MY_CUSTOM_PRECEDENCE")
 public class MyCustomInterceptor implements MessageBodyWriterInterceptor {...}
Copy to Clipboard Toggle word wrap
You can create your own convenience annotation by using @Precedence as a meta-annotation.
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Precedence("MY_CUSTOM_PRECEDENCE")
public @interface MyCustomPrecedence {}
Copy to Clipboard Toggle word wrap
You must register your custom precedence, or RESTEasy will show an error at deployment time. You can register your custom precendence with the context parameters:
resteasy.append.interceptor.precedence
resteasy.interceptor.before.precedence
resteasy.interceptor.after.precedence
Copy to Clipboard Toggle word wrap
resteasy.append.interceptor.precedence appends the precedence family to the list. resteasy.interceptor.before.precedence lets you specify a family for your precendence to fall ahead of. resteasy.interceptor.after.precedence lets you specify a family for your precedence to follow after. For example:
<web-app>
    <display-name>Archetype RestEasy Web Application</display-name>

    <!-- testing configuration -->
    <context-param>
        <param-name>resteasy.append.interceptor.precedence</param-name>
        <param-value>END</param-value>
    </context-param>
    <context-param>
        <param-name>resteasy.interceptor.before.precedence</param-name>
        <param-value>ENCODER : BEFORE_ENCODER</param-value>
    </context-param>

    <context-param>
        <param-name>resteasy.interceptor.after.precedence</param-name>
        <param-value>ENCODER : AFTER_ENCODER</param-value>
    </context-param>

    <context-param>
        <param-name>resteasy.servlet.mapping.prefix</param-name>
        <param-value>/test</param-value>
    </context-param>

    <listener>
        <listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
    </listener>

    <servlet>
        <servlet-name>Resteasy</servlet-name>
        <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>Resteasy</servlet-name>
        <url-pattern>/test/*</url-pattern>
    </servlet-mapping>

</web-app>
Copy to Clipboard Toggle word wrap
In this web.xml file, we have defined three new precedence families: END, BEFORE_ENCODER, and AFTER_ENCODER. With this configuration, the family order would look like this:
SECURITY
HEADER_DECORATOR
BEFORE_ENCODER
ENCODER
AFTER_ENCODER
REDIRECT
DECODER
END
Copy to Clipboard Toggle word wrap
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat