Dieser Inhalt ist in der von Ihnen ausgewählten Sprache nicht verfügbar.
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:
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.
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 Link kopierenLink in die Zwischenablage kopiert!
Link kopierenLink in die Zwischenablage kopiert!
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 {...}
@Provider
@ServerInterceptor
@Precedence("MY_CUSTOM_PRECEDENCE")
public class MyCustomInterceptor implements MessageBodyWriterInterceptor {...}
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 {}
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Precedence("MY_CUSTOM_PRECEDENCE")
public @interface MyCustomPrecedence {}
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
resteasy.append.interceptor.precedence
resteasy.interceptor.before.precedence
resteasy.interceptor.after.precedence
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:
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: