1 Introduction
Improve this doc
Micronaut includes support for producing OpenAPI (Swagger) YAML at compilation time. Micronaut will at compile
time produce a OpenAPI 3.x compliant YAML file just based on the regular Micronaut annotations and the javadoc
comments within your code.
You can customize the generated Swagger using the standard Swagger Annotations.
To create a project with OpenAPI/Swagger support using the Micronaut CLI, supply the openapi feature to
the features flag. For example:
$ mn create-app my-openapi-app --features openapi
This will create a project with the minimum necessary configuration for OpenAPI.
If you have already created a Micronaut project and will like to add Swagger support, you can simply follow
instructions in subsequent sections.
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@OpenAPIDefinition(
info = @Info(
title = "Hello World",
version = "0.0",
description = "My API",
license = @License(name = "Apache 2.0", url =
"[Link]
contact = @Contact(url = "[Link] name =
"Fred", email = "Fred@[Link]")
)
)
public class Application {
public static void main(String[] args) {
[Link]([Link]);
}
}
Copy to Clipboard
With that in place, you compile your project and a OpenAPI YAML file will be generated to
the META-INF/swagger directory of your project’s class output. For example, the above configuration generates:
For Java build/classes/java/main/META-INF/swagger/[Link]
For Kotlin build/tmp/kapt3/classes/main/META-INF/swagger/hello-world-
[Link]
The previously defined annotations will produce YAML like the following:
Generated OpenAPI YAML
openapi: 3.0.1
info:
title: the title
description: My API
contact:
name: Fred
url: [Link]
email: Fred@[Link]
license:
name: Apache 2.0
url: [Link]
version: "0.0"
The path from where the swagger specification will be served by the http server defaults to swagger. You can change
it via the [Link] property.
Thus, by default, the views expect to find the yaml under /swagger.
If you change this mapping to something else:
Exposing Swagger YAML
micronaut:
router:
static-resources:
swagger:
paths: classpath:META-INF/swagger
mapping: /swaggerYAML/