Dynamic provider registration in JAX-RS

The DynamicFeature class in JAX-RS (2.0) allows you to register providers

  • Dynamically i.e. without any pre-defined binding strategy (e.g. annotations)
  • Based on criteria i.e. to help decide which provider to bind to which JAX-RS resource(s)

The JAX-RS implementation detects and executes the DynamicFeature implementations at deployment time

Here is an example where we instruct the JAX-RS run time to dynamically bind the AuthenticationFilter (a JAX-RS post construct filter) to be applied when a PUT is invoked on UserResource (which is a JAX-RS resource class)

Criteria based

Criteria is provided by ResourceInfo and it’s based on 2 attributes

  • the resource class, and
  • the resource method

Dynamic

The dynamic registration is achieved using FeatureContext whose register method can be used to bind the provider

Note

  • @Provider usage is needed for automatic discovery by the JAX-RS run time
  • Applicable for – filters, interceptors and  any Feature
  • In case of filters, the following applicability criteria apply
    • Post matching (@PreMatching filters excluded)
    • Server side filters i.e. ClientRequestFilter and ClientResponseFilter cannot be registered using this method
  • Once used, it overrides other bindings (static or using @NameBinding)

Further reading

About Abhishek

Currently working as a Senior Product Manager in the Oracle Cloud Application Development team with a focus on Oracle Cloud PaaS portfolio. When not hovering in the clouds, I stay grounded with Java EE
This entry was posted in Java, Java EE and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s