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 > 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 · Java Zone · Tutorial
Like (10)
Save
Tweet
15.28K 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

  • Screen Sharing in Java
  • MACH Architecture Explained
  • A Brief History of the DMCA
  • Refactor Switch to a One-Liner

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