Swagger Annotations: Openapi
Swagger Annotations: Openapi
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:
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 io.micronaut.runtime.Micronaut;
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.info.Contact;
import io.swagger.v3.oas.annotations.info.Info;
import io.swagger.v3.oas.annotations.info.License;
@OpenAPIDefinition(
info = @Info(
title = "Hello World",
version = "0.0",
description = "My API",
license = @License(name = "Apache 2.0", url =
"https://foo.bar"),
contact = @Contact(url = "https://gigantic-server.com", name =
"Fred", email = "[email protected]")
)
)
public class Application {
For Java build/classes/java/main/META-INF/swagger/hello-world-0.0.yml
For Kotlin build/tmp/kapt3/classes/main/META-INF/swagger/hello-world-
0.0.yml
The previously defined annotations will produce YAML like the following:
The path from where the swagger specification will be served by the http server defaults to swagger. You can change
it via the mapping.path property.