【问题标题】:Helidon MP OpenAPI aren't generating a updated openapi endpoint responseHelidon MP OpenAPI 未生成更新的 openapi 端点响应
【发布时间】:2019-12-02 17:42:05
【问题描述】:

我目前正在按照 Oracle 自己的指南和教程构建基于 Helidon Microprofile 的微服务,但在使用注释时遇到了与“自动 OpenAPI 规范生成器”相关的问题。

我的 POM 包含一个 MP 包和集成,以使其与 Hibernate 提供的 JPA 一起使用。

即使在我的资源上设置了所有注释后,它也不会生成更新的规范。

POM

 <dependencies>
    <dependency>
        <groupId>io.helidon.microprofile.bundles</groupId>
        <artifactId>helidon-microprofile</artifactId>
        <version>1.4.0</version>
    </dependency>

    <dependency>
        <groupId>org.jboss</groupId>
        <artifactId>jandex</artifactId>
        <version>2.1.1.Final</version>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>io.helidon.integrations.cdi</groupId>
        <artifactId>helidon-integrations-cdi-datasource-hikaricp</artifactId>
        <version>1.4.0</version>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>io.helidon.integrations.cdi</groupId>
        <artifactId>helidon-integrations-cdi-jta-weld</artifactId>
        <version>1.4.0</version>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>io.helidon.integrations.cdi</groupId>
        <artifactId>helidon-integrations-cdi-hibernate</artifactId>
        <version>1.4.0</version>
    </dependency>

    <dependency>
        <groupId>javax.transaction</groupId>
        <artifactId>javax.transaction-api</artifactId>
        <version>1.3</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>jakarta.persistence</groupId>
        <artifactId>jakarta.persistence-api</artifactId>
        <version>2.2.3</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.mariadb.jdbc</groupId>
        <artifactId>mariadb-java-client</artifactId>
        <version>2.5.2</version>
    </dependency>

    <dependency>
        <groupId>com.googlecode.json-simple</groupId>
        <artifactId>json-simple</artifactId>
        <version>1.1.1</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.29.1</version>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>com.auth0</groupId>
        <artifactId>java-jwt</artifactId>
        <version>3.8.3</version>
    </dependency>

    <dependency>
        <groupId>com.auth0</groupId>
        <artifactId>jwks-rsa</artifactId>
        <version>0.9.0</version>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.5.2</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.5.2</version>
        <scope>test</scope>
    </dependency>
</dependencies>

我只使用指南中指定的注释和 @OpenAPIDefinition 来定义标题和许可证等内容。

资源

    @OpenAPIDefinition(
        info = @Info(
                title = "Newsletter Microservice",
                version = "1.0", description = "Microservice in charge of handling newsletter",
                license = @License(name = "Apache 2.0", url = "https://www.apache.org/licenses/LICENSE-2.0"),
                contact = @Contact(name = "Email", url = "mailto:email")
        ),
        tags = {
                @Tag(name = "public"), @Tag(name = "private")
        }
)

@Path("/newsletter")
@RequestScoped
public class NewsletterClientResource {

日志

Connected to the target VM, address: '127.0.0.1:0', transport: 'socket'
Java HotSpot(TM) 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended
2019.12.02 20:01:19 INFO org.jboss.weld.Version Thread[main,5,main]: WELD-000900: 3.1.1 (Final)
2019.12.02 20:01:20 INFO org.jboss.weld.Bootstrap Thread[main,5,main]: WELD-ENV-000020: Using jandex for bean discovery
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.jboss.weld.util.bytecode.ClassFileUtils$1 (file:/C:/Users/Brenno%20Fagundes/.m2/repository/org/jboss/weld/weld-core-impl/3.1.1.Final/weld-core-impl-3.1.1.Final.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int)
WARNING: Please consider reporting this to the maintainers of org.jboss.weld.util.bytecode.ClassFileUtils$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
2019.12.02 20:01:21 INFO org.jboss.weld.Event Thread[main,5,main]: WELD-000411: Observer method [BackedAnnotatedMethod] public org.glassfish.jersey.ext.cdi1x.internal.ProcessAllAnnotatedTypes.processAnnotatedType(@Observes ProcessAnnotatedType<?>, BeanManager) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.
2019.12.02 20:01:21 INFO org.jboss.weld.Event Thread[main,5,main]: WELD-000411: Observer method [BackedAnnotatedMethod] private io.helidon.microprofile.openapi.IndexBuilder.processAnnotatedType(@Observes ProcessAnnotatedType<X>) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.
2019.12.02 20:01:22 INFO org.jboss.weld.Bootstrap Thread[main,5,main]: WELD-ENV-002003: Weld SE container 404f642b-892f-4676-960e-8df848aee3a3 initialized
2019.12.02 20:01:22 INFO io.helidon.microprofile.security.SecurityMpService Thread[main,5,main]: Security extension for microprofile is enabled, yet security configuration is missing from config (requires providers configuration at key security.providers). Security will not have any valid provider.
2019.12.02 20:01:22 INFO io.smallrye.openapi.api.OpenApiDocument Thread[main,5,main]: OpenAPI document initialized: io.smallrye.openapi.api.models.OpenAPIImpl@7793ad58
2019.12.02 20:01:23 INFO io.helidon.webserver.NettyWebServer Thread[main,5,main]: Version: 1.4.0
2019.12.02 20:01:24 INFO io.helidon.webserver.NettyWebServer Thread[nioEventLoopGroup-2-1,10,main]: Channel '@default' started: [id: 0x4e1f119b, L:/0:0:0:0:0:0:0:0:7200]
2019.12.02 20:01:24 INFO io.helidon.microprofile.server.ServerImpl Thread[nioEventLoopGroup-2-1,10,main]: Server initialized on http://localhost:7200 (and all other host addresses) in 5254 milliseconds.

BUMP,另外,生成使用自定义过滤器和模型,META-INF 中的静态定义也可以。当前使用 JDK 13。


编辑:这是 Tim Quinn 建议修改后的我的应用程序类。

应用类

@ApplicationScoped
@ApplicationPath("/")
@OpenAPIDefinition(
        info = @Info(
                title = "Newsletter Microservice",
                version = "1.0", description = "Microservice in charge of handling newsletter",
                license = @License(name = "Apache 2.0", url = "https://www.apache.org/licenses/LICENSE-2.0"),
                contact = @Contact(name = "Email", url = "mailto:john.doe@gmail.com")
        ),
        tags = {
                @Tag(name = "public"), @Tag(name = "private")
        }
)
public class NewsletterApplication extends Application {
    @Override
    public Set<Class<?>> getClasses(){
        HashSet<Class<?>> classes = new HashSet<Class<?>>();
        classes.add(NewsletterClientResource.class);
        return classes;
    }
}

生成的文件

---
openapi: 3.0.1
info:
  title: Generated API
  version: "1.0"
paths: {}

【问题讨论】:

    标签: java openapi microprofile helidon


    【解决方案1】:

    布伦诺,

    您没有显示更新后的源代码,但我假设您在快速入门 GreetResource 类中添加了 @OpenAPIDefinition 注释,对吗?

    该注解的属性描述的是整个应用程序,而不是其资源的子集,因此请尝试将注解移至 GreetApplication 类:

    @ApplicationScoped
    @ApplicationPath("/")
    @OpenAPIDefinition(info = @Info(title = "QuickStart API", version = "1.1"))
    
    public class GreetApplication extends Application {...}
    
    

    应该可以。我刚刚在生成的 Helidon MP 快速入门示例源上尝试了这个,重新构建,服务器返回了预期的结果。

    这是我所做更改的差异:

    diff --git a/examples/quickstarts/helidon-quickstart-mp/src/main/java/io/helidon/examples/quickstart/mp/GreetApplication.java b/examples/quickstarts/helidon-quickstart-mp/src/main/java/io/helidon/examples/quickstart/mp/GreetApplication.java
    index fd140738..cca60da2 100644
    --- a/examples/quickstarts/helidon-quickstart-mp/src/main/java/io/helidon/examples/quickstart/mp/GreetApplication.java
    +++ b/examples/quickstarts/helidon-quickstart-mp/src/main/java/io/helidon/examples/quickstart/mp/GreetApplication.java
    @@ -23,12 +23,15 @@ import javax.ws.rs.ApplicationPath;
     import javax.ws.rs.core.Application;
    
     import io.helidon.common.CollectionsHelper;
    +import org.eclipse.microprofile.openapi.annotations.OpenAPIDefinition;
    +import org.eclipse.microprofile.openapi.annotations.info.Info;
    
     /**
      * Simple Application that produces a greeting message.
      */
     @ApplicationScoped
     @ApplicationPath("/")
    +@OpenAPIDefinition(info = @Info(title = "QuickStart API", version = "1.1"))
     public class GreetApplication extends Application {
    
         @Override
    

    这是从服务器返回的更新后 OpenAPI 文档的开头:

    ---
    openapi: 3.0.1
    info:
      title: QuickStart API
      version: "1.1"
    paths:
    

    正如预期的那样,标题和版本已更改。

    【讨论】:

    • 我在我的 Resource 类而不是 Application 类中定义了 @OpenAPIDefinition。我已将定义更改为正确的位置,但仍然无法正常工作,日志和我的资源标题也已添加到问题中。
    • 您添加到帖子中的代码示例在资源类上具有@OpenAPIDefinition,而不是应在的应用程序类。注释本身看起来不错,但如果您的代码就是这样,那么它仍然在错误的类上。正如我所提到的,当我将简单版本的注释添加到快速入门应用程序类时,它会被正确处理,因此您应该会看到相同的结果。
    • 我已将注释移至应用程序类,但没有新结果。
    • 你能分享你的来源吗?它应该工作。我已经更新了我的初始答案,以包括我所做的更改的差异。在更改之前,来自http://localhost:8080/openapi 的 OpenAPI 输出是默认值。更改后,输出的开头与新添加的注释一样。
    • 将整个应用程序类代码添加到原始帖子+生成的文件中。
    【解决方案2】:

    案例 1:您正在使用 jandex,而您的 /openapi 没有得到更新。

    如果您使用的是 jandex,您的 jandex.idx 很可能没有更新。你可以通过运行mvn process-classes来做到这一点

    案例 2:您没有使用 jandex,当您点击 /openapi 时,您会得到一些空白响应。

    这似乎是 Helidon 的问题。依赖项 helidon-integrations-cdi-jpa, helidon-integrations-cdi-jta, helidon-integrations-cdi-eclipselink etc... 包含 jandex.idx 并且 Helidon 现在认为 jandex 已启用并且它 只会从那些 jandex 文件中读取,跳过你的资源。所以暂时可以加入jandex插件来解决这个问题。

    【讨论】:

    • 事情实际上比这更微妙。 jandex 插件中的一个错误阻止它从多个 JAR 中将有关类的信息收集到索引中。这个问题更详细地解释了:github.com/oracle/helidon/issues/2729。 Helidon 对此无能为力。
    猜你喜欢
    • 1970-01-01
    • 2021-08-08
    • 1970-01-01
    • 2022-10-05
    • 2021-07-12
    • 2020-11-07
    • 2022-07-23
    • 2021-07-29
    • 1970-01-01
    相关资源
    最近更新 更多