Annotation Interface ServerRequestFilter


@Retention(RUNTIME) @Target(METHOD) public @interface ServerRequestFilter
When used on a method, then an implementation of ContainerRequestFilter is generated that calls the annotated method with the proper arguments

The idea behind using this is to make it much to write a ServerRequestFilter as all the necessary information is passed as arguments to the method instead of forcing the author to use a mix of @Context and programmatic CDI look-ups.

An example filter could look like this:

 public class CustomContainerRequestFilter {

     private final SomeBean someBean;

     // SomeBean will be automatically injected by CDI as long as SomeBean is a bean itself
     public CustomContainerRequestFilter(SomeBean someBean) {
         this.someBean = someBean;
     }

     @ServerRequestFilter
     public void whatever(UriInfo uriInfo, HttpHeaders httpHeaders) {
         // do something
     }
 }
 
Methods annotated with ServerRequestFilter can declare any of the following parameters (in any order) The return type of the method must be either be of type void, Response, RestResponse, Optional<Response>, Optional<RestResponse>, Uni<Void>, Uni<Response> or Uni<RestResponse>.
  • void should be used when filtering does not need to perform any blocking operations and the filter cannot abort processing.
  • Response or RestResponse should be used when filtering does not need to perform any blocking operations and the filter cannot abort processing - in this case the processing will be aborted if the response is not null.
  • Optional<Response> or Optional<RestResponse> should be used when filtering does not need to perform any blocking operations but the filter might abort processing - in this case processing is aborted when the Optional contains a Response payload.
  • Uni<Void> should be used when filtering needs to perform a non-blocking operation but the filter cannot abort processing. Note that Uni<Void> can easily be produced using: Uni.createFrom().nullItem()
  • Uni<Response> or Uni<RestResponse> should be used when filtering needs to perform a non-blocking operation and the filter might abort processing - in this case processing is aborted when the Uni contains a Response payload.
Another important thing to note is that if ContainerRequestContext is used as a request parameter, calling abortWith is not allowed. You should use the proper response type if aborting processing is necessary.
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    boolean
    Normally ContainerRequestFilter classes are run by RESTEasy Reactive on the same thread as the Resource Method - this means than when a Resource Method is annotated with Blocking, the filters will also be run on a worker thread.
    boolean
    Whether the filter is a pre-matching filter
    int
    The priority with which this request filter will be executed
    boolean
    Deprecated.
    use WithFormRead on your filter to force reading the form values before your filter is invoked.
  • Element Details

    • priority

      int priority
      The priority with which this request filter will be executed
      Default:
      5000
    • preMatching

      boolean preMatching
      Whether the filter is a pre-matching filter

      Note that this setting and readBody() cannot be both set to true.

      Default:
      false
    • nonBlocking

      boolean nonBlocking
      Normally ContainerRequestFilter classes are run by RESTEasy Reactive on the same thread as the Resource Method - this means than when a Resource Method is annotated with Blocking, the filters will also be run on a worker thread. This is meant to be set to true if a filter should be run on the event-loop even if the target Resource method is going to be run on the worker thread. For this to work, this filter must be run before any of the filters when non-blocking is not required.
      Default:
      false
    • readBody

      @Deprecated boolean readBody
      Deprecated.
      use WithFormRead on your filter to force reading the form values before your filter is invoked.
      If set to true, the filter will be run after the body has been fully read but before any deserialization takes place.

      Note that this change only affects Resource Methods that do result in reading the message body. For all other Resource Methods that the filter applies to, it will be executed in normal fashion.

      Also note that this setting and preMatching() cannot be both set to true.

      Default:
      false