Get method with path variables

Let's shift our attention to path variables. Path variables are used to bind values from the URI to a variable on the controller method. In the following example, we want to parameterize the name so that we can customize the welcome message with a name:

    @GetMapping("/welcome-with-parameter/name/{name}")    fun welcomeWithParameter(@PathVariable name: String) =     WelcomeBean("Hello World, $name")

The following are a few important things to note:

  • @GetMapping("/welcome-with-parameter/name/{name}"): {name} indicates that this value will be the variable. We can have multiple variable templates in a URI.
  • welcomeWithParameter(@PathVariable String name): @PathVariable ensures that the variable value from the URI is bound ...

Get Mastering Spring 5.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.