Error to inject some dependency with kotlin + quarkus

浪尽此生 提交于 2019-12-20 02:34:45

问题


I want to include a dependency in my Kotlin Resource file. But I cant.

I made this tutorial: https://quarkus.io/guides/rest-client-guide But, to start the project, I've included extension "kotlin" in my project.

My codes below:

Country.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

The error:

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

Someone can help me?

Thanks!


回答1:


This problem results as a combination of how Kotlin handles annotations and the lack of the a @Target on the @RestClient annotation definition.

To fix your problem, simply use:

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


来源:https://stackoverflow.com/questions/55458873/error-to-inject-some-dependency-with-kotlin-quarkus

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!