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

Related

  • How to Verify Domain Ownership: A Technical Deep Dive
  • How to Identify the Underlying Causes of Connection Timeout Errors for MongoDB With Java
  • Utilizing Multiple Vectors and Advanced Search Data Model Design for City Data
  • How To Connect a Heroku Java App to a Cloud-Native Database

Trending

  • Retesting Best Practices for Agile Teams: A Quick Guide to Bug Fix Verification
  • Building Production-Grade GenAI on GCP with Vertex AI Agent Builder
  • Integrating AI-Driven Decision-Making in Agile Frameworks: A Deep Dive into Real-World Applications and Challenges
  • Spec-Driven Integration: Turning API Sprawl Into a Governed Capability Fleet for AI
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Using UriTemplate to Change Path Parameters in a URI

Using UriTemplate to Change Path Parameters in a URI

By 
Gerard Davison user avatar
Gerard Davison
·
Mar. 01, 13 · Interview
Likes (2)
Comment
Save
Tweet
Share
43.8K Views

Join the DZone community and get the full member experience.

Join For Free

I have been recently working on a client generator in the wadl.java.net project and I was solving a bug where I wanted to change path parameter on a URI. This turned out to be quite easy with existing classes that are part of the Jersey project.

First of all you need a instance of UriTemplate, depending on the very of Jersey this is in a slightly different package - you IDE's automatic import will do the work for you. 

String template = "http://example.com/name/{name}/age/{age}";
UriTemplate uriTemplate = new UriTemplate(template);
 Then you use match(...) to extract them:
  String uri = "http://example.com/name/Bob/age/47";
  Map<String, String> parameters = new HashMap<>();

  // Not this method returns false if the URI doesn't match, ignored
  // for the purposes of the this blog.
  uriTemplate.match(uri, parameters);
 Then you can replace the parameters at will and rebuild the URI:
  parameters.put("name","Arnold");

  UriBuilder builder = UriBuilder.fromPath(template);
  URI output = builder.build(parameters);



Uniform Resource Identifier

Published at DZone with permission of Gerard Davison. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How to Verify Domain Ownership: A Technical Deep Dive
  • How to Identify the Underlying Causes of Connection Timeout Errors for MongoDB With Java
  • Utilizing Multiple Vectors and Advanced Search Data Model Design for City Data
  • How To Connect a Heroku Java App to a Cloud-Native Database

Partner Resources

×

Comments

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

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook