Multiple URIs

Each of the routing annotations supports multiple URI templates. For each template, a route is created. This feature is useful for example to change the path of the API and leave the existing path as is for backwards compatibility. For example:

Multiple URIs

  1. import io.micronaut.http.annotation.Controller;
  2. import io.micronaut.http.annotation.Get;
  3. @Controller("/hello")
  4. public class BackwardCompatibleController {
  5. @Get(uris = {"/{name}", "/person/{name}"}) (1)
  6. public String hello(String name) { (2)
  7. return "Hello, " + name;
  8. }
  9. }

Multiple URIs

  1. import io.micronaut.http.annotation.Controller
  2. import io.micronaut.http.annotation.Get
  3. @Controller("/hello")
  4. class BackwardCompatibleController {
  5. @Get(uris = ["/{name}", "/person/{name}"]) (1)
  6. String hello(String name) { (2)
  7. "Hello, $name"
  8. }
  9. }

Multiple URIs

  1. import io.micronaut.http.annotation.Controller
  2. import io.micronaut.http.annotation.Get
  3. @Controller("/hello")
  4. class BackwardCompatibleController {
  5. @Get(uris = ["/{name}", "/person/{name}"]) (1)
  6. fun hello(name: String): String { (2)
  7. return "Hello, $name"
  8. }
  9. }
1Specify multiple templates
2Bind to the template arguments as normal
Route validation is more complicated with multiple templates. If a variable that would normally be required does not exist in all templates, that variable is considered optional since it may not exist for every execution of the method.