Mapping Controller Variables

Variables in the URL can be mapped into the controller arguments.

Path Variables

These are part of the URL. For example in the following one the name at the end would be a variable:

https://localhost:8080/users/John

Which can be mapped like this:

@GetMapping(path="/{name}")
public final Employee getEmployee()

Regular expressions can be used:

@GetMapping(path="/{name:[A-Za-z0-9]*}")
public final Employee getEmployee()

This can be applied to the controller mapping too:

@RestController
@RequestMapping(value = "/users/{name}/")
public class EmployeeController

Path Variables to Arguments

@GetMapping(path="/{name}", produces = MediaType.APPLICATION_JSON_VALUE)
public final Employee getEmployee(@PathVariable final String name)

Path Variables to Objects

This requires EmployeeName being a bean with a field sharing the path variable name:

Request Parameters

These come as a map added to the URL:

And can be mapped like this:

They can be optional:

Body Content

The content of a request can be mapped into an object:

Last updated

Was this helpful?