The following example demonstrates how to version an API:

    Versioning an API

    Versioning an API

    1. import io.micronaut.http.annotation.Controller
    2. import io.micronaut.http.annotation.Get
    3. @Controller("/versioned")
    4. class VersionedController {
    5. @Version("1") (1)
    6. @Get("/hello")
    7. String helloV1() {
    8. "helloV1"
    9. }
    10. @Version("2") (2)
    11. @Get("/hello")
    12. String helloV2() {
    13. "helloV2"
    14. }

    Versioning an API

    1. import io.micronaut.core.version.annotation.Version
    2. import io.micronaut.http.annotation.Controller
    3. import io.micronaut.http.annotation.Get
    4. @Controller("/versioned")
    5. internal class VersionedController {
    6. @Version("1") (1)
    7. @Get("/hello")
    8. fun helloV1(): String {
    9. return "helloV1"
    10. }
    11. @Version("2") (2)
    12. @Get("/hello")
    13. fun helloV2(): String {
    14. return "helloV2"
    15. }

    You should then enabling versioning by setting micronaut.router.versioning.enabled to true in application.yml:

    Enabling Versioning

    By default Micronaut has 2 out-of-the-box strategies for resolving the version that are based on an HTTP header named X-API-VERSION or a request parameter named api-version, however this is configurable. A full configuration example can be seen below:

    Configuring Versioning

    1. micronaut:
    2. router:
    3. versioning:
    4. enabled: true (1)
    5. parameter:
    6. enabled: false (2)
    7. names: 'v,api-version' (3)
    8. header:
    9. enabled: true (4)
    10. names: (5)
    11. - 'X-API-VERSION'
    12. - 'Accept-Version'

    If this is not enough you can also implement the interface which receives the HttpRequest and can implement any strategy you choose.

    It is possible to supply a default version through configuration.

    1. micronaut:
    2. router:
    3. versioning:
    4. enabled: true
    5. default-version: 3 (1)

    A route will not be matched if the following conditions are met:

    • The default version is configured

    • No version is found in the request

    • The route defines a version

    • The route version does not match the default version

    If the incoming request specifies a version then the default version has no effect.

    Versioning Client Requests

    Micronaut’s Declarative HTTP client also supports automatic versioning of outgoing requests via the annotation.

    By default if you annotate a client interface with @Version the value supplied to the annotation will be included using the X-API-VERSION header.

    For example:

    1. import io.micronaut.core.version.annotation.Version
    2. import io.micronaut.http.annotation.Get
    3. import io.micronaut.http.client.annotation.Client
    4. import io.reactivex.Single
    5. @Client("/hello")
    6. interface HelloClient {
    7. String sayHello(String name)
    8. @Version("2")
    9. @Get("/greeting/{name}")
    10. Single<String> sayHelloTwo(String name) (2)
    11. }
    1. import io.micronaut.core.version.annotation.Version
    2. import io.micronaut.http.annotation.Get
    3. import io.micronaut.http.client.annotation.Client
    4. import io.reactivex.Single
    5. @Client("/hello")
    6. @Version("1") (1)
    7. interface HelloClient {
    8. @Get("/greeting/{name}")
    9. fun sayHello(name : String) : String
    10. @Version("2")
    11. @Get("/greeting/{name}")
    12. fun sayHelloTwo(name : String) : Single<String> (2)
    13. }

    The default behaviour for how the version is sent for each call can be configured with :

    For example to use Accept-Version as the header name:

    Configuring Client Versioning

    The default key is used to refer to the default configuration. You can specify client specific configuration by using the value passed to @Client (typically the service ID). For example:

    Configuring Versioning

    1. micronaut:
    2. http:
    3. client:
    4. versioning:
    5. greeting-service:
    6. headers:
    7. - 'Accept-Version'
    8. - 'X-API-VERSION'