Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

DZone's Guide to

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!

· Java Zone
Free Resource

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

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.

Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:
java ,java 8 ,java mvc

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

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}