【问题标题】:Opening swagger UI gives 404 with Micronaut打开 swagger UI 给 404 和 Micronaut
【发布时间】:2020-05-09 11:29:23
【问题描述】:

按照这里的文档 - https://micronaut-projects.github.io/micronaut-openapi/1.3.4/guide/index.html

我将我的build.gradle 配置为包含用于 swagger yaml 生成的编译时任务,如下所示-

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'
}

这就是我的application.yaml 的样子-

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/**

就像文档说的,我也注释了Application.java,如下图-

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);
    }
}

完成这一切后,当我尝试打开 http://localhost:9090/swagger/api-backend-0.0.yaml 时,它会打开生成的开放 API 规范 yaml。但是,如果我尝试打开 http://localhost:9090/swagger-ui/,我会得到一个 404

谁能帮我找出问题或提出替代方案?

【问题讨论】:

  • 我想知道这个问题是否以某种方式解决了?我有同样的问题。我看到生成了 yaml 文件和 index.html 文件。但是在尝试访问 swagger-ui 时看到 401 错误
  • 我的问题没有解决,但是,401表示凭据问题。尝试将 swagger URL 模式列入白名单。
  • 感谢@Abhishek 的更新。由于该服务启用了安全性,我才知道这个问题。这是我问的另一个问题:stackoverflow.com/questions/62286864/…。如果您对这个问题有任何想法,请回答
  • 你最终解决了这个问题吗?

标签: java swagger swagger-ui micronaut micronaut-openapi


【解决方案1】:

您能否添加以下代码并检查,因为所有文件都在 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/**

【讨论】:

    【解决方案2】:

    所有步骤对我来说都是正确的。

    您能否在构建项目后检查是否在您的工件中的 META-INF/swagger/views/swagger-ui 下创建了 index.html 文件。

    从 maven 的角度来看,我们在运行 mvn compile 后在 target 文件夹下看到了这个: 目标/类/META_INF/swagger/views/swagger-ui

    【讨论】:

    • 抱歉延迟回复。我已经检查过了。 index.html 文件存在于您提到的文件夹中。
    【解决方案3】:

    配置看起来不错,swagger-ui 应该可以在浏览器中使用http://localhost:8080/swagger-ui

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-24
      • 2020-10-06
      • 2018-07-09
      • 1970-01-01
      • 2020-10-20
      • 1970-01-01
      • 1970-01-01
      • 2020-04-13
      相关资源
      最近更新 更多