swagger ui not accessible with micronaut multi-module project gradle build

1.2k views Asked by At

I have a below project structure

enter image description here

Project product and API gateway share the common project as common. Since in the parent project settings.Gradle I have included the project as below

rootProject.name = 'src'
include 'common', 'fetebird-apigateway', 'fete-bird-product'

In the API gateway build.gradle I have included the below dependency

    dependencies {
        annotationProcessor("io.micronaut.openapi:micronaut-openapi:2.1.1")
        implementation("io.swagger.core.v3:swagger-annotations")
        implementation project(':common')
        }

In the product build.gradle I have include the below dependency

dependencies {
         annotationProcessor("io.micronaut.openapi:micronaut-openapi:2.1.1") 
         implementation("io.swagger.core.v3:swagger-annotations")
         implementation project(':common')

        }

When I run the command $ gradle build I can see the view is generated

enter image description here

Swagger expose end point

micronaut:
  application:
    name: feteBirdProduct
  server:
    port: 8083
  router:
    versioning:
      enabled: true
    static-resources:
      swagger:
        paths: classpath:META-INF/swagger
        mapping: /swagger/**
      swagger-ui:
        paths: classpath:META-INF/swagger/views/swagger-ui
        mapping: /swagger-ui/**

But when I access the URL

http://localhost:8084/swagger-ui/index.html

I can see the below message and I don't have any security enable

{"message":"Page Not Found","_links":{"self":{"href":"/swagger-ui/index.html","templated":false}}}

While debugging io.micronaut.web.router.resource.StaticResourceResolver. The public URL findResource(String name) return null in the BuiltinClassLoader.java

enter image description here

3

There are 3 answers

7
tcrespog On

Not sure if you did, but you might need to expose the generated API docs as static resources with a YML config such as the following stated in the Micronaut OpenAPI docs):

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

Then you should be able to access them using a route such as: http://localhost:8080/swagger/views/swagger-ui/index.html.

In case you enable the security module, you might need to specify security rules for that URL using "intercept URL map", otherwise you might not be able to reach it:

micronaut:
  security:
    intercept-url-map:
      - pattern: /swagger/**
        http-method: GET
        access:
          - isAnonymous()
0
xvronny On

Documentation says the default value for enabled flag is true but for some reason I had to set this value explicitly to display the swagger ui.

micronaut:
  router:
    static-resources:
      swagger:
        enabled: true  # docs says this is already the default value
        paths: classpath:META-INF/swagger
        mapping: /swagger/**
      swagger-ui:
        enabled: true  # but this only works if it is explicitly set
        paths: classpath:META-INF/swagger/views/swagger-ui
        mapping: /swagger-ui/**
0
San Jaisy On

The solution was to include the setting.gradle file of each project, however on Intellj if you navigate to the Gradle task and run the build, it will give you an error or if you run gradle run it give you an error