0% found this document useful (0 votes)
70 views2 pages

Swagger Annotations: Openapi

Micronaut includes support for producing OpenAPI (Swagger) YAML at compilation time based on annotations and javadoc comments. The document provides instructions for generating a project with OpenAPI support using the Micronaut CLI and adding support to an existing project. It includes an example of annotations that will produce OpenAPI YAML defining title, description, license, and contact details.

Uploaded by

Srinu S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views2 pages

Swagger Annotations: Openapi

Micronaut includes support for producing OpenAPI (Swagger) YAML at compilation time based on annotations and javadoc comments. The document provides instructions for generating a project with OpenAPI support using the Micronaut CLI and adding support to an existing project. It includes an example of annotations that will produce OpenAPI YAML defining title, description, license, and contact details.

Uploaded by

Srinu S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

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 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 {

public static void main(String[] args) {


Micronaut.run(Application.class);
}
}
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/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:

Generated OpenAPI YAML


openapi: 3.0.1
info:
title: the title
description: My API
contact:
name: Fred
url: https://gigantic-server.com
email: [email protected]
license:
name: Apache 2.0
url: https://foo.bar
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 mapping.path 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/

You might also like