Opening swagger UI gives 404 with Micronaut

2.7k views Asked by At

Following the documentation over here - https://micronaut-projects.github.io/micronaut-openapi/1.3.4/guide/index.html

I configured my build.gradle to include compile time tasks for swagger yaml generation as follows-

tasks.withType(JavaCompile){
    options.encoding = "UTF-8"
    options.compilerArgs.add('-parameters')
    options.fork = true
    options.forkOptions.jvmArgs << '-Dmicronaut.openapi.views.spec=rapidoc.enabled=true,swagger-ui.enabled=true,swagger-ui.theme=flattop'
}

This is how my application.yaml looks like-

micronaut:
  server:
    port: 9090
    cors:
      enabled: true
  router:
    static-resources:
      swagger:
        paths: classpath:META-INF/swagger
        mapping: /swagger/**
      redoc:
        paths: classpath:META-INF/swagger/views/redoc
        mapping: /redoc/**
      rapidoc:
        paths: classpath:META-INF/swagger/views/rapidoc
        mapping: /rapidoc/**
      swagger-ui:
        paths: classpath:META-INF/swagger/views/swagger-ui
        mapping: /swagger-ui/**

Like the doc said, I also annotated by Application.java as shown below-

package com.api.backend;

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;

@OpenAPIDefinition(
    info = @Info(
            title = "API Backend",
            version = "0.0",
            description = "API backend"
    )
)
public class Application {

    public static void main(String[] args) {
        Micronaut.run(Application.class);
    }
}

After doing all this, when I try to open http://localhost:9090/swagger/api-backend-0.0.yaml it opens the generated open API spec yaml. However, if I try opening http://localhost:9090/swagger-ui/, I get a 404.

Can anyone help me figure out the problem or suggest an alternative?

3

There are 3 answers

0
Jimmy Obonyo Abor On

The configs looks ok , swagger-ui should be available in browser at http://localhost:8080/swagger-ui

0
San Jaisy On

Can you add the below code and check, since all the files are inside the swagger-ui

  router:
    static-resources:
      default:
        enabled: true
        mapping: /**
        paths: classpath:swagger
      swagger:
        paths: classpath:META-INF/swagger
        mapping: /swagger/**
      swagger-ui:
        paths: classpath:META-INF/swagger/views/swagger-ui
        mapping: /swagger-ui/**
1
Mohit Gupta On

All the steps looks correct to me.

Could you please check that after you build your project, is index.html file is created under META-INF/swagger/views/swagger-ui in your artifact.

From maven perspective, we see this in under target folder after running mvn compile as: target/classes/META_INF/swagger/views/swagger-ui