mscharhag, Programming and Stuff;

A blog about programming and software development topics, mostly focused on Java technologies including Java EE, Spring and Grails.

Sunday, 31 January, 2016

Java EE 8 MVC: Working with Path Parameters

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.

Leave a reply