DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Dynamic Provider Registration in JAX-RS

Dynamic Provider Registration in JAX-RS

Wondering if it's possible to register providers dynamically in JEE? The answer is yes, it is possible! Read on to find out how it's done.

Abhishek Gupta user avatar by
Abhishek Gupta
CORE ·
Nov. 29, 16 · Java Zone · Tutorial
Like (5)
Save
Tweet
9.71K Views

Join the DZone community and get the full member experience.

Join For Free

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 runtime 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):

@Provider
public class DynamicFilter implements DynamicFeature {
  @Override
  public void configure(ResourceInfo resInfo, FeatureContext ctx) {
    if (UserResource.class.equals(resInfo.getResourceClass()) &&
      resInfo.getResourceMethod().getName().contains("PUT")) {
      ctx.register(AuthenticationFilter.class);
    }
  }
}


Criteria-Based

Criteria are provided by ResourceInfo, and they're based on two attributes:

  • The resource class
  • The resource method

Dynamic

Dynamic registration is achieved using FeatureContext, whose register method can be used to bind the provider.

Note

  • @Provider is needed for automatic discovery by the JAX-RS runtime.
  • 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).
Filter (software) POST (HTTP) Binding (linguistics) Implementation Discovery (law) Attribute (computing) Construct (game engine)

Published at DZone with permission of Abhishek Gupta, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Writing Beautiful, Optimized, and Better .NET Code With NDepend Static Analysis
  • How to Make Git Forget a Tracked File Now in .gitignore
  • How to Build Security for Your SaaS User Communications
  • How Java Apps Litter Beyond the Heap

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo