Using Properties in JAX-RS Jersey Requests

There isn't much out there on the subject of using properties with JAX-RS Requests, so I want to send this post out into the void. If you're reading this then you probably already know that by properties I'm not referring to parameters of any kind, but rather properties added to the Jersey Client Request Context as in this example.

Our property is added to the Request Context. It will NOT appear in any Request Endpoint @Context object like Request or ServletContext. For properties to be available at the Endpoint they must be set in a Container Request Filter, which we'll see in a minute.

The examples we'll cover in the post are found in this GitGub Gist. We're using Bootique Jersey to create our tests.

Context Property Behavior

Here are general rules in using Request Properties, which we'll see play out in the code examples.

  1. Properties WILL be passed to a Filter which implements ClientRequestFilter.
  2. Properties WILL NOT be passed to a Filter which implements ContainerRequestFilter.
  3. Properties set in a ClientRequestFilter WILL NOT be passed to the Request Endpoint
  4. Properties set in a ContainerRequestFilter WILL be passed to the Endpoint in the @Context ContainerRequestContext object.

Stepping Through the Process with a Client Request Filter

We'll start with a ClientRequestFilter property handling example. Our client registers the filter and adds an INCLIENT property in the Request. As the property value states, the property will be available to the Filter, but not at the Endpoint.

The ClientRequestFilter Asserts tell the tale of Property Availability. We're good for this step.

But that's the end for our property as we see at the /send_properties_request Endpoint. Not yet discussed, we set an INFILTER property in a ContainerRequestFilter, which IS passed to our Endpoint. Our INCLIENT property is no longer available.

Using the ContainerRequestFilter

If we want to use properties with our JAX-RS Request we must create them in a ContainerRequestFilter, then use the @Context ContainerRequestContext if we want to use those properties on the other side.

Asserts complete the story in our Resource method where the INFILTER property set in our Filter is available and the INCLIENT property is null.

Like I said at the top, there doesn't seem to be much documentation on using Request Properties in Jersey, so if you have any additions to make to this post, please do so on the Gist Comments.