Create cool API specifications with Micronaut + Swagger UI

1. Add dependency </ b>

build.gradle


dependencies {
    annotationProcessor "io.micronaut.configuration:micronaut-openapi" 
    implementation "io.swagger.core.v3:swagger-annotations" 
}

A yaml file that conforms to the OpenAPI Specification will be generated at build time. For annotations that can be used, refer to Swagger's Wiki. https://github.com/swagger-api/swagger-core/wiki/Swagger-2.X---Annotations

If you want to handle it as static content, add as follows to application.yml. You can handle the generated HTML file described later by the same procedure.

application.yml


micronaut:
    router:
        static-resources:
            swagger:
                paths: classpath:META-INF/swagger
                mapping: /swagger/**

Handling static content See the Micronaut manual for details https://docs.micronaut.io/1.1.0/guide/index.html#staticResources

2. Make it look good in HTML from the yaml file </ b> Use Swagger UI. If you want to make it look like Swagger UI, you can copy the whole file under / dist at https://github.com/swagger-api/swagger-ui. The rest is in index.html

index.html


url: "https://petstore.swagger.io/v2/swagger.json"

Just rewrite this to the URL of the yml file.

By the way, if you think that you can't find @ApiModel or @ApiModelProperty, it seems that all schema definition annotations have been integrated into @Schema. http://docs.swagger.io/swagger-core/v2.1.1/apidocs/io/swagger/v3/oas/annotations/media/Schema.html

Recommended Posts