【问题标题】:Using enums as query params in Resteasy Reactive / Quarkus在 Resteasy Reactive / Quarkus 中使用枚举作为查询参数
【发布时间】:2021-04-12 18:06:22
【问题描述】:

我试图在我的休息控制器中使用 Kotlin 枚举作为查询参数,但没有任何成功。

我受影响的代码如下所示:

enum class FieldExclusion {
    AUTHOR_FIELD,
    TIME_FIELD
}

...

 @GET
    fun get(
        @RestPath id: UUID,
        @RestQuery excludedFields: List<FieldExclusion>?,
        @Context httpRequest: HttpServerRequest
    ): Uni<Response> {

...

包含此代码后,mvn 无法构建我的项目并抛出此错误:

2021-04-12 20:49:30,553 ERROR [io.qua.dep.dev.IsolatedDevModeMain] (main) Failed to start quarkus: java.lang.RuntimeException: io.quarkus.builder.BuildException: Build failure: Build failed due to errors
    [error]: Build step io.quarkus.resteasy.reactive.server.deployment.ResteasyReactiveProcessor#setupEndpoints threw an exception: java.lang.RuntimeException: java.lang.RuntimeException: Failed to process method com.kzman.zukus.user.controller.UserController#io.smallrye.mutiny.Uni<javax.ws.rs.core.Response> get(java.util.UUID id, java.util.List<? extends com.kzman.zukus.common.request.model.FieldExclusion> excludedFields, io.vertx.core.http.HttpServerRequest httpRequest)
    at org.jboss.resteasy.reactive.common.processor.EndpointIndexer.createEndpoints(EndpointIndexer.java:238)
    at io.quarkus.resteasy.reactive.server.deployment.ResteasyReactiveProcessor.setupEndpoints(ResteasyReactiveProcessor.java:402)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at io.quarkus.deployment.ExtensionLoader$2.execute(ExtensionLoader.java:920)
    at io.quarkus.builder.BuildContext.run(BuildContext.java:277)
    at org.jboss.threads.EnhancedQueueExecutor$Task.run(EnhancedQueueExecutor.java:2415)
    at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1452)
    at java.base/java.lang.Thread.run(Thread.java:832)
    at org.jboss.threads.JBossThread.run(JBossThread.java:501)
Caused by: java.lang.RuntimeException: Failed to process method com.kzman.zukus.user.controller.UserController#io.smallrye.mutiny.Uni<javax.ws.rs.core.Response> get(java.util.UUID id, java.util.List<? extends com.kzman.zukus.common.request.model.FieldExclusion> excludedFields, io.vertx.core.http.HttpServerRequest httpRequest)
    at org.jboss.resteasy.reactive.common.processor.EndpointIndexer.createResourceMethod(EndpointIndexer.java:526)
    at org.jboss.resteasy.reactive.common.processor.EndpointIndexer.createEndpoints(EndpointIndexer.java:285)
    at org.jboss.resteasy.reactive.common.processor.EndpointIndexer.createEndpoints(EndpointIndexer.java:209)
    ... 11 more
Caused by: java.lang.RuntimeException: Unknown parameter type ? extends com.kzman.zukus.common.request.model.FieldExclusion
    at org.jboss.resteasy.reactive.common.processor.EndpointIndexer.toClassName(EndpointIndexer.java:701)
    at org.jboss.resteasy.reactive.common.processor.EndpointIndexer.extractParameterInfo(EndpointIndexer.java:889)
    at org.jboss.resteasy.reactive.common.processor.EndpointIndexer.createResourceMethod(EndpointIndexer.java:407)
    ... 13 more

    at io.quarkus.runner.bootstrap.AugmentActionImpl.runAugment(AugmentActionImpl.java:396)
    at io.quarkus.runner.bootstrap.AugmentActionImpl.createInitialRuntimeApplication(AugmentActionImpl.java:269)
    at io.quarkus.runner.bootstrap.AugmentActionImpl.createInitialRuntimeApplication(AugmentActionImpl.java:66)
    at io.quarkus.deployment.dev.IsolatedDevModeMain.firstStart(IsolatedDevModeMain.java:79)
    at io.quarkus.deployment.dev.IsolatedDevModeMain.accept(IsolatedDevModeMain.java:378)
    at io.quarkus.deployment.dev.IsolatedDevModeMain.accept(IsolatedDevModeMain.java:56)
    at io.quarkus.bootstrap.app.CuratedApplication.runInCl(CuratedApplication.java:127)
    at io.quarkus.bootstrap.app.CuratedApplication.runInAugmentClassLoader(CuratedApplication.java:84)
    at io.quarkus.deployment.dev.DevModeMain.start(DevModeMain.java:144)
    at io.quarkus.deployment.dev.DevModeMain.main(DevModeMain.java:63)
Caused by: io.quarkus.builder.BuildException: Build failure: Build failed due to errors
    [error]: Build step io.quarkus.resteasy.reactive.server.deployment.ResteasyReactiveProcessor#setupEndpoints threw an exception: java.lang.RuntimeException: java.lang.RuntimeException: Failed to process method com.kzman.zukus.user.controller.UserController#io.smallrye.mutiny.Uni<javax.ws.rs.core.Response> get(java.util.UUID id, java.util.List<? extends com.kzman.zukus.common.request.model.FieldExclusion> excludedFields, io.vertx.core.http.HttpServerRequest httpRequest)
    at org.jboss.resteasy.reactive.common.processor.EndpointIndexer.createEndpoints(EndpointIndexer.java:238)
    at io.quarkus.resteasy.reactive.server.deployment.ResteasyReactiveProcessor.setupEndpoints(ResteasyReactiveProcessor.java:402)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at io.quarkus.deployment.ExtensionLoader$2.execute(ExtensionLoader.java:920)
    at io.quarkus.builder.BuildContext.run(BuildContext.java:277)
    at org.jboss.threads.EnhancedQueueExecutor$Task.run(EnhancedQueueExecutor.java:2415)
    at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1452)
    at java.base/java.lang.Thread.run(Thread.java:832)
    at org.jboss.threads.JBossThread.run(JBossThread.java:501)
Caused by: java.lang.RuntimeException: Failed to process method com.kzman.zukus.user.controller.UserController#io.smallrye.mutiny.Uni<javax.ws.rs.core.Response> get(java.util.UUID id, java.util.List<? extends com.kzman.zukus.common.request.model.FieldExclusion> excludedFields, io.vertx.core.http.HttpServerRequest httpRequest)
    at org.jboss.resteasy.reactive.common.processor.EndpointIndexer.createResourceMethod(EndpointIndexer.java:526)
    at org.jboss.resteasy.reactive.common.processor.EndpointIndexer.createEndpoints(EndpointIndexer.java:285)
    at org.jboss.resteasy.reactive.common.processor.EndpointIndexer.createEndpoints(EndpointIndexer.java:209)
    ... 11 more
Caused by: java.lang.RuntimeException: Unknown parameter type ? extends com.kzman.zukus.common.request.model.FieldExclusion
    at org.jboss.resteasy.reactive.common.processor.EndpointIndexer.toClassName(EndpointIndexer.java:701)
    at org.jboss.resteasy.reactive.common.processor.EndpointIndexer.extractParameterInfo(EndpointIndexer.java:889)
    at org.jboss.resteasy.reactive.common.processor.EndpointIndexer.createResourceMethod(EndpointIndexer.java:407)
    ... 13 more

    at io.quarkus.builder.Execution.run(Execution.java:116)
    at io.quarkus.builder.BuildExecutionBuilder.execute(BuildExecutionBuilder.java:79)
    at io.quarkus.deployment.QuarkusAugmentor.run(QuarkusAugmentor.java:153)
    at io.quarkus.runner.bootstrap.AugmentActionImpl.runAugment(AugmentActionImpl.java:394)
    ... 9 more
Caused by: java.lang.RuntimeException: java.lang.RuntimeException: Failed to process method com.kzman.zukus.user.controller.UserController#io.smallrye.mutiny.Uni<javax.ws.rs.core.Response> get(java.util.UUID id, java.util.List<? extends com.kzman.zukus.common.request.model.FieldExclusion> excludedFields, io.vertx.core.http.HttpServerRequest httpRequest)
    at org.jboss.resteasy.reactive.common.processor.EndpointIndexer.createEndpoints(EndpointIndexer.java:238)
    at io.quarkus.resteasy.reactive.server.deployment.ResteasyReactiveProcessor.setupEndpoints(ResteasyReactiveProcessor.java:402)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at io.quarkus.deployment.ExtensionLoader$2.execute(ExtensionLoader.java:920)
    at io.quarkus.builder.BuildContext.run(BuildContext.java:277)
    at org.jboss.threads.EnhancedQueueExecutor$Task.run(EnhancedQueueExecutor.java:2415)
    at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1452)
    at java.base/java.lang.Thread.run(Thread.java:832)
    at org.jboss.threads.JBossThread.run(JBossThread.java:501)
Caused by: java.lang.RuntimeException: Failed to process method com.kzman.zukus.user.controller.UserController#io.smallrye.mutiny.Uni<javax.ws.rs.core.Response> get(java.util.UUID id, java.util.List<? extends com.kzman.zukus.common.request.model.FieldExclusion> excludedFields, io.vertx.core.http.HttpServerRequest httpRequest)
    at org.jboss.resteasy.reactive.common.processor.EndpointIndexer.createResourceMethod(EndpointIndexer.java:526)
    at org.jboss.resteasy.reactive.common.processor.EndpointIndexer.createEndpoints(EndpointIndexer.java:285)
    at org.jboss.resteasy.reactive.common.processor.EndpointIndexer.createEndpoints(EndpointIndexer.java:209)
    ... 11 more
Caused by: java.lang.RuntimeException: Unknown parameter type ? extends com.kzman.zukus.common.request.model.FieldExclusion
    at org.jboss.resteasy.reactive.common.processor.EndpointIndexer.toClassName(EndpointIndexer.java:701)
    at org.jboss.resteasy.reactive.common.processor.EndpointIndexer.extractParameterInfo(EndpointIndexer.java:889)
    at org.jboss.resteasy.reactive.common.processor.EndpointIndexer.createResourceMethod(EndpointIndexer.java:407)
    ... 13 more

2021-04-12 20:49:30,553 INFO  [io.qua.dep.dev.IsolatedDevModeMain] (main) Attempting to start hot replacement endpoint to recover from previous Quarkus startup failure

创立者documentation表示也支持枚举类。

具有名为 valueOf 或 fromString 的静态方法的类型,该方法带有返回该类型实例的单个 String 参数。如果这两种方法都存在,则将使用 valueOf,除非类型是枚举,在这种情况下将使用 fromString。

由于在 Kotlin 中我无法将静态函数(伴奏)添加到枚举中,有没有办法解决它而不是使用纯字符串并手动转换它们?我还尝试向主构造函数添加一个值,但也失败了。

enum class FieldExclusion(val value : String) {
    AUTHOR_FIELD("AUTHOR_FIELD"),
    TIME_FIELD("TIME_FIELD")
}

非常感谢您的帮助!

【问题讨论】:

    标签: java kotlin resteasy quarkus


    【解决方案1】:

    我不认为这是 kotlin 的问题。 Kotlin 枚举具有预先生成的静态 valueOf,所以这不是问题。

    你也可以像这样使用 kotlin 为 jvm 添加静态方法:

    companion object {
        @JvmStatic
        fun fromString(text: String): A {
            //TODO: do your thing
        }
    }
    

    我没有看到该异常的任何部分,这表明这是该枚举的问题。我错过了什么吗?

    【讨论】:

    • 您好,谢谢您的回答!我不知道你也可以用分号将它添加到枚举类中。但不幸的是,它也无济于事。我已经用完整的堆栈跟踪更新了我的第一篇文章。我也尝试过使用 java 类,但它也失败了。所以你可能是对的,但我看不出任何其他原因。
    【解决方案2】:

    这听起来像是一个错误。

    请在 Quarkus GitHub 问题跟踪器上打开一个问题,并提供一个我们可以用来重现问题的项目。

    【讨论】:

      猜你喜欢
      • 2023-03-28
      • 2019-06-06
      • 2015-02-27
      • 2013-12-13
      • 1970-01-01
      • 1970-01-01
      • 2021-11-15
      • 2015-11-22
      • 1970-01-01
      相关资源
      最近更新 更多