16

Using Properties in JAX-RS Jersey Requests

 3 years ago
source link: https://nixmash.com/java/using-properties-in-jax-rs-jersey-requests/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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.

props0321a.png

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.

props0321b.png

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

props0321c.png

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.

props0321d.png

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.

props0321e.png

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

props0321f.png

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.


Recommend

  • 30
    • Reddit www.reddit.com 5 years ago
    • Cache

    Luminous by Jax Sheridon : ImaginaryMindscapes

    r/ImaginaryMindscapes: The Art of Imagination

  • 47

    While REST and Microservices are common terms these days, few people know JAX-RS, or do have a rather blurry vision of it. In fact, JAX-RS started a decade ago to provide a standard API for more sophisticated support in w...

  • 73
    • www.tuicool.com 5 years ago
    • Cache

    Pictures from W-JAX 2018

    This week I had the pleasure to attend W-JAX in Munich which is a conference for Java and enterprise developers with 1400 attendees. I’ve been to JAX and W-JAX several times and really like this confe...

  • 30
    • Github github.com 5 years ago
    • Cache

    GitHub - google/jax

  • 30
    • www.tuicool.com 5 years ago
    • Cache

    Pictures from JAX 2019

    This week I attended JAX in Mainz, Germany, which is a conference with 2000 enterprise developers. Below are some pictures. In the opening session the people responsible for the conten...

  • 10
    • nixmash.com 3 years ago
    • Cache

    JAX-RS @Context Options

    JAX-RS @Context Options – NixMashSkip to content

  • 7
    • nixmash.com 3 years ago
    • Cache

    Using the JAX-RS @BeanParam

    Using the JAX-RS @BeanParam There are several ways to pass multiple parameters to Controller Methods. One such option is using multiple @FormParam parameters like this example of a Login Submit @POST method.

  • 13

    JAX-RS Jersey Request Header and Entity Parameters We're going to look at three examples of passing parameters in JAX-RS Jersey on both the client and the server. We're going to look at: Objects sent as Request He...

  • 7

    JAX 是机器学习 (ML) 领域的新生力量,它有望使 ML 编程更加直观、结构化和简洁。在机器学习领域,大家可能对 TensorFlow 和 PyTorch 已经耳熟能详,但除了这两个框架,一些新生力量也不容小觑,它就是谷歌推出的 JAX。很对研...

  • 3

    JAX-WS. A Api “Coroa” do Java. Introdução ao Soap! Como sempre, vamos voltar um pouco às origens, mas antes porque decidi es...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK