Search code examples
kotlinswaggermicronaut

How to enable swagger UI on micronaut?


I'm following the micronaut doc for openapi at https://micronaut-projects.github.io/micronaut-openapi/latest/guide/ and the UI is not being generated, every time when I try to access /swagger, /swagger-ui I got this error:

{
  "message": "Not Found",
  "_links": {
    "self": {
      "href": "/swagger",
      "templated": false
    }
  },
  "_embedded": {
    "errors": [
      {
        "message": "Page Not Found"
      }
    ]
  }
}

The dependencies are installed:

implementation("io.swagger.core.v3:swagger-annotations")
annotationProcessor("io.micronaut.openapi:micronaut-openapi:4.5.2")

The router on application.yml is defined:

micronaut:
  application:
    name: myapp
  router:
    static-resources:
      default:
        enabled: true
      swagger:
        enabled: true
        paths: classpath:META-INF/swagger
        mapping: /swagger/**

and the file openapi.properties on the root folder is created:

swagger-ui.enabled=true
micronaut.openapi.views.spec=apidoc.enabled=true,swagger-ui.enabled=true,swagger-ui.theme=flattop
micronaut.openapi.expand.api.version=v0.1
micronaut.openapi.expand.openapi.description=myapp

according to the docs a .yml file should be generated at;

For Kotlin build/tmp/kapt3/classes/main/META-INF/swagger/myapp-0.1.yml

On my controllers I have the Operation and ApiResponses annotation as well, but when I run the application the file yml is not being generated, how can I properly enable swagger on my micronaut project?

Code sample: https://github.com/rafa-acioly/micronaut


Solution

  • You defined only swagger in your application.yml. You should add also add a section for swagger-ui with a similar definition:

    micronaut:
      application:
        name: openapitest
      router:
        static-resources:
          swagger:
            paths: classpath:META-INF/swagger
            mapping: /swagger/**
          swagger-ui:
            paths: classpath:META-INF/swagger/views/swagger-ui
            mapping: /swagger-ui/**
    

    Also, I used the default openapi.properties file:

    swagger-ui.enabled=true
    redoc.enabled=false
    rapidoc.enabled=false
    rapidoc.bg-color=#14191f
    rapidoc.text-color=#aec2e0
    rapidoc.sort-endpoints-by=method
    

    And did you try to clean and build your project with gradlew?

    I create a sample repository on GitHub and you can check it.

    =========UPDATE========

    Ok, I've found what is incorrect! You are using annotation-processing :

    annotationProcessor("io.micronaut.openapi:micronaut-openapi:4.5.2")
    

    But if you are using Kotlin, you should use kapt - Kotlin Annotation Processing Tool. Here is a few articles - link1, link2.

    So you should change to:

    kapt("io.micronaut.openapi:micronaut-openapi:4.5.2")