Swagger UI在使用Micronaut多模块项目的Gradle构建时无法访问。

huangapple go评论119阅读模式
英文:

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

问题

我有一个如下的项目结构

项目产品和API网关共享一个名为common的公共项目。由于在父项目的settings.Gradle中,我已经按照以下方式包含了该项目

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

在API网关的build.gradle中,我已经包含了以下依赖

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

在产品的build.gradle中,我已经包含了以下依赖

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

当我运行命令 $ gradle build 时,我可以看到视图被生成了

Swagger暴露了端点

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

但是当我访问URL

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

我可以看到下面的消息,并且我没有启用任何安全性

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

在调试io.micronaut.web.router.resource.StaticResourceResolver时,public URL findResource(String name) 在BuiltinClassLoader.java中返回null。

英文:

I have a below project structure

Swagger UI在使用Micronaut多模块项目的Gradle构建时无法访问。

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

Swagger UI在使用Micronaut多模块项目的Gradle构建时无法访问。

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

Swagger UI在使用Micronaut多模块项目的Gradle构建时无法访问。

答案1

得分: 1

不确定你是否这样做了,但你可能需要将生成的API文档公开为静态资源,并使用类似于以下在Micronaut OpenAPI文档中所述的YML配置:

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

然后,你应该能够通过类似于以下路由访问它们:http://localhost:8080/swagger/views/swagger-ui/index.html

如果你启用了安全模块,你可能需要使用“拦截URL映射”为该URL指定安全规则,否则你可能无法访问它:

micronaut:
  security:
    intercept-url-map:
      - pattern: /swagger/**
        http-method: GET
        access:
          - isAnonymous()
英文:

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()

答案2

得分: 0

解决方案是在每个项目中包含setting.gradle文件,但是在IntelliJ中,如果你导航到Gradle任务并运行构建,它会给你一个错误;或者如果你运行gradle run,它会给你一个错误。

英文:

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

答案3

得分: 0

文档指出 enabled 标志的默认值为true,但由于某种原因,我不得不明确地设置这个值以显示 Swagger UI。

micronaut:
  router:
    static-resources:
      swagger:
        enabled: true  # 文档中表示这已经是默认值了
        paths: classpath:META-INF/swagger
        mapping: /swagger/**
      swagger-ui:
        enabled: true  # 但只有在明确设置时才起作用
        paths: classpath:META-INF/swagger/views/swagger-ui
        mapping: /swagger-ui/**
英文:

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

huangapple
  • 本文由 发表于 2020年10月18日 19:54:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/64412985.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定