使用 kotlin + quarkus 注入一些依赖项时出错

Posted

技术标签:

【中文标题】使用 kotlin + quarkus 注入一些依赖项时出错【英文标题】:Error to inject some dependency with kotlin + quarkus 【发布时间】:2019-08-22 20:11:00 【问题描述】:

我想在我的 Kotlin 资源文件中包含一个依赖项。但我不能。

我制作了这个教程:https://quarkus.io/guides/rest-client-guide 但是,为了启动这个项目,我在我的项目中包含了扩展“kotlin”。

我的代码如下:

国家.kt

package org.acme.restclient

class Country 
  var name:String? = null
  var alpha2Code:String? = null
  var capital:String? = null
  var currencies:List<Currency>? = null
  class Currency 
    var code:String? = null
    var name:String?= null
    var symbol:String? = null
  

CountriesService.kt

package org.acme.restclient

import org.eclipse.microprofile.rest.client.inject.RegisterRestClient
import javax.ws.rs.GET
import javax.ws.rs.Path
import javax.ws.rs.PathParam
import javax.ws.rs.Produces

@Path("/v2")
@RegisterRestClient
interface CountriesService 
    @GET
    @Path("/name/name")
    @Produces("application/json")
    fun getByName(@PathParam("name") name: String): Set<Country>

CountriesResource.kt

package org.acme.restclient

import org.eclipse.microprofile.rest.client.inject.RestClient
import javax.inject.Inject
import javax.ws.rs.GET
import javax.ws.rs.Path
import javax.ws.rs.PathParam
import javax.ws.rs.Produces
import javax.ws.rs.core.MediaType

@Path("/country")
class CountriesResource 
    @Inject
    @RestClient
    lateinit internal var countriesService: CountriesService

    @GET
    @Path("/name/name")
    @Produces(MediaType.APPLICATION_JSON)
    fun name(@PathParam("name") name: String): Set<Country> 
        return countriesService.getByName(name)
    

application.properties

org.acme.restclient.CountriesService/mp-rest/url=https://restcountries.eu/rest

错误:

12:23:55,340 ERROR [io.qua.dev.DevModeMain] Failed to start quarkus: java.lang.RuntimeException: org.jboss.builder.BuildException: Build failure: Build failed due to errors
    [error]: Build step io.quarkus.arc.deployment.ArcAnnotationProcessor#build threw an exception: javax.enterprise.inject.spi.DeploymentException: javax.enterprise.inject.UnsatisfiedResolutionException: Unsatisfied dependency for type org.acme.restclient.CountriesService and qualifiers [@Default]
    - java member: org.acme.restclient.CountriesResource#countriesService
    - declared on CLASS bean [types=[org.acme.restclient.CountriesResource, java.lang.Object], qualifiers=[@Default, @Any], target=org.acme.restclient.CountriesResource]
    at io.quarkus.runner.RuntimeRunner.run(RuntimeRunner.java:134)
    at io.quarkus.dev.DevModeMain.doStart(DevModeMain.java:105)
    at io.quarkus.dev.DevModeMain.main(DevModeMain.java:66)
Caused by: org.jboss.builder.BuildException: Build failure: Build failed due to errors
    [error]: Build step io.quarkus.arc.deployment.ArcAnnotationProcessor#build threw an exception: javax.enterprise.inject.spi.DeploymentException: javax.enterprise.inject.UnsatisfiedResolutionException: Unsatisfied dependency for type org.acme.restclient.CountriesService and qualifiers [@Default]
    - java member: org.acme.restclient.CountriesResource#countriesService
    - declared on CLASS bean [types=[org.acme.restclient.CountriesResource, java.lang.Object], qualifiers=[@Default, @Any], target=org.acme.restclient.CountriesResource]
    at org.jboss.builder.Execution.run(Execution.java:123)
    at org.jboss.builder.BuildExecutionBuilder.execute(BuildExecutionBuilder.java:136)
    at io.quarkus.deployment.QuarkusAugmentor.run(QuarkusAugmentor.java:110)
    at io.quarkus.runner.RuntimeRunner.run(RuntimeRunner.java:99)
    ... 2 more
Caused by: javax.enterprise.inject.spi.DeploymentException: javax.enterprise.inject.UnsatisfiedResolutionException: Unsatisfied dependency for type org.acme.restclient.CountriesService and qualifiers [@Default]
    - java member: org.acme.restclient.CountriesResource#countriesService
    - declared on CLASS bean [types=[org.acme.restclient.CountriesResource, java.lang.Object], qualifiers=[@Default, @Any], target=org.acme.restclient.CountriesResource]
    at io.quarkus.arc.processor.BeanDeployment.processErrors(BeanDeployment.java:740)
    at io.quarkus.arc.processor.BeanDeployment.init(BeanDeployment.java:276)
    at io.quarkus.arc.processor.BeanProcessor.process(BeanProcessor.java:153)
    at io.quarkus.arc.deployment.ArcAnnotationProcessor.build(ArcAnnotationProcessor.java:237)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at io.quarkus.deployment.ExtensionLoader$1.execute(ExtensionLoader.java:506)
    at org.jboss.builder.BuildContext.run(BuildContext.java:413)
    at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
    at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1998)
    at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1525)
    at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1416)
    at java.base/java.lang.Thread.run(Thread.java:834)
    at org.jboss.threads.JBossThread.run(JBossThread.java:479)
Caused by: javax.enterprise.inject.UnsatisfiedResolutionException: Unsatisfied dependency for type org.acme.restclient.CountriesService and qualifiers [@Default]
    - java member: org.acme.restclient.CountriesResource#countriesService
    - declared on CLASS bean [types=[org.acme.restclient.CountriesResource, java.lang.Object], qualifiers=[@Default, @Any], target=org.acme.restclient.CountriesResource]
    at io.quarkus.arc.processor.Beans.resolveInjectionPoint(Beans.java:326)
    at io.quarkus.arc.processor.BeanInfo.init(BeanInfo.java:365)
    at io.quarkus.arc.processor.BeanDeployment.init(BeanDeployment.java:268)
    ... 14 more

有人可以帮助我吗?

谢谢!

【问题讨论】:

未来的读者可能想看看这个:***.com/questions/55513502/… 和这个:byteslounge.com/tutorials/… 【参考方案1】:

这个问题是由 Kotlin 处理注解的方式和 @RestClient 注解定义中缺少 @Target 共同导致的。

要解决您的问题,只需使用:

   @Inject
   @field: RestClient
   lateinit internal var countriesService: CountriesService

【讨论】:

很高兴听到这个消息!请接受答案,以便未来的读者立即知道这是正确的解决方案,而无需阅读 cmets,谢谢:)

以上是关于使用 kotlin + quarkus 注入一些依赖项时出错的主要内容,如果未能解决你的问题,请参考以下文章

quarkus依赖注入之十三:其他重要知识点大串讲(终篇)

quarkus依赖注入之十三:其他重要知识点大串讲(终篇)

Testcontainer 不适用于 Kotlin 和 Quarkus - 连接到服务器 mongo:27017 时监视器线程中出现异常

为啥 Quarkus 会警告我关于私有字段的注入?

quarkus依赖注入之一:创建bean

quarkus依赖注入之十一:拦截器高级特性上篇(属性设置和重复使用)