Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error to inject some dependency with kotlin + quarkus

Tags:

quarkus

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!

like image 928
Renan Vaz Avatar asked Apr 01 '19 15:04

Renan Vaz


People also ask

Does kotlin support Quarkus?

Quarkus provides support for live reloading changes made to source code. This support is also available to Kotlin, meaning that developers can update their Kotlin source code and immediately see their changes reflected.

How do you inject with Kotlin?

The building block of kotlin-inject is a component which you declare with an @Component annotation on an abstract class. An implementation of this component will be generated for you. For external dependencies, you can declare a function or read-only property in the component to create an instance for a certain type.

What does @inject mean in Kotlin?

@Inject is a Java annotation for describing the dependencies of a class that is part of Java EE (now called Jakarta EE). It is part of CDI (Contexts and Dependency Injection) which is a standard dependency injection framework included in Java EE 6 and higher.

What is dependency injection in Kotlin?

Dependency injection (DI) is a technique widely used in programming and well suited to Android development. By following the principles of DI, you lay the groundwork for good app architecture. Implementing dependency injection provides you with the following advantages: Reusability of code. Ease of refactoring.


1 Answers

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
like image 171
geoand Avatar answered Sep 30 '22 16:09

geoand