Multiple URIs

Each of the routing annotations support multiple URI templates. For each template, a route will be created. This feature is useful for example to change the path of the API and leaving 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. @Controller("/hello")
  2. class BackwardCompatibleController {
  3. @Get(uris = ["/{name}", "/person/{name}"]) (1)
  4. fun hello(name: String): String { (2)
  5. return "Hello, $name"
  6. }
  7. }
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, then that variable is considered optional since it may not exist for every execution of the method.