DZone
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
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • Improving ui-select Control
  • Filtering Java Collections via Annotation-Driven Introspection
  • Iptables Basic Commands for Novice
  • Step-by-Step Guide to Use Anypoint MQ: Part 4

Trending

  • Build Real-Time Analytics Applications With AWS Kinesis and Amazon Redshift
  • Stop Prompt Hacking: How I Connected My AI Agent to Any API With MCP
  • What Is Plagiarism? How to Avoid It and Cite Sources
  • Top Load Balancing Algorithms: Choosing the Right Strategy

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.

By 
Abhishek Gupta user avatar
Abhishek Gupta
DZone Core CORE ·
Nov. 29, 16 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
11.1K 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.

Related

  • Improving ui-select Control
  • Filtering Java Collections via Annotation-Driven Introspection
  • Iptables Basic Commands for Novice
  • Step-by-Step Guide to Use Anypoint MQ: Part 4

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: