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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Java
  4. Java EE 8 MVC: Working With Path Parameters (JSR-371)

Java EE 8 MVC: Working With Path Parameters (JSR-371)

Want to learn more about path parameters in Java EE 8 MVC? Here's a great guide with detailed path paramater examples!

Michael Scharhag user avatar by
Michael Scharhag
·
Feb. 08, 16 · Tutorial
Like (10)
Save
Tweet
Share
15.48K Views

Join the DZone community and get the full member experience.

Join For Free

In the previous post, we saw how to work with query parameters in Java EE MVC. This post continues with a very similar topic: path parameters.

Path parameters are a dynamic part of the request path and can be specified with the @Path annotation.

For example:

@Controller
@Path("path-params")
public class PathParamsController {

 @GET
 @Path("/date/{year}/{month}")
 public String pathParamDate(@PathParam("year") int year, @PathParam("month") int month) {
 ...
 }
}

Paths parameter are surrounded with curly brackets inside the @Path annotation. In this example two path parameters are defined: year and month.

With @PathParam path parameters can be mapped to method parameters.

We can call this method by sending a request to

/path-params/date/2016/01

In this case, 2016 and 1 will be passed as year and month arguments.

Type Conversion

Path parameters use the same type conversion rules as query parameters (explained in the previous blog post).

For example, we can convert a path parameter to an enum value like this:

public enum Role {
 admin, reporter, accountant
}
@Controller
@Path("path-params")
public class PathParamsController {

 @GET
 @Path("/roles/{role}")
 public String pathParamUsers(@PathParam("role") Role role) {
 ...
 }
}

If we now send a request to:

/path-params/roles/admin

the string admin gets converted to the corresponding enum constant.

Using @PathParam on Fields and Methods

Like @QueryParam the usage of @PathParam is not limited to method parameters. It is also possible to annotate fields or setters with @PathParam.

For example:

@Controller
@Path("path-params")
public class PathParamsController {

 @PathParam("category")
 private String category;

 @GET
 @Path("/categories/{category}")
 public String findByCategory() {
 // work with category
 }
}

Using Path Parameters with Patterns

It is possible to define a more specific pattern for a path variable. Therefore, a regular expression can be added after the name of the path variable.

For example:

@Controller
@Path("path-params")
public class PathParamsController {

 @GET
 @Path("/users/{id : \\d+}")
 public String findUserById(@PathParam("id") long id) {
 ...
 }

 @GET
 @Path("/users/{name : [a-zA-Z]+}")
 public String findUserByName(@PathParam("name") String name) {
 ...
 } 
}

Here we define two controller methods that listen on /users/{variable}:

  • findUserById() is only called if a numeric id is part of the request path
  • findUserByName() is used if the path parameter matches the regular expression [a-zA-Z]+.

So if we send a request to

/path-params/users/123

findUserById() will be called and 123 is passed as id.

Sending a request to

/path-params/users/john

calls findUserByName() and passes john as name.

Quick Summary

@PathParam can be used to extract path parameters defined with @Path. Like @QueryParam, @PathParam can be used on method arguments, instance fields and methods.

When defining path parameters with @Path, a regular expression can be used to define a specific path pattern.

You can find the source code for all shown examples on GitHub.

Java EE Java (programming language)

Published at DZone with permission of Michael Scharhag, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Shift-Left: A Developer's Pipe(line) Dream?
  • Stress Testing Tutorial: Comprehensive Guide With Best Practices
  • Public Key and Private Key Pairs: Know the Technical Difference
  • Unlock the Power of Terragrunt’s Hierarchy

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • 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: