'Java/Quarkus: How to Inject different instances which return the same data type
I am in a bit tricky situation where I have 2 methods in my Quarkus RESTApplication both of which return the same data type. However, I would like to inject them into 2 different variables.
If there is only one method with one variable then everything works fine but since there are 2 methods and both of which return the same type the Quarkus is unable to identify which to inject and runs into the error:
Ambiguous dependencies for type String and qualifiers [@Default]
Following is my code: RestApplication.class, this has the @Produces which will provide the required dependency:
@ApplicationPath("/")
public class RESTApplication extends Application {
@Produces
public String firstMethod() {
System.out.println("firstMethod : returns Hello One");
return "HELLO ONE";
}
@Produces
public String secondMethod() {
System.out.println("secondMethod : returns Hello TWO");
return "HELLO TWO";
}
}
Following is the class where I am injecting the returned value:
@Path("/api")
public class RestAPI {
@Inject
String one;
@Inject
String two;
@Path("/one")
@POST
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
public String myMethodOne(final String input) {
System.out.println("One : " + one)
return one;
}
@Path("/two")
@POST
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
public String myMethodOne(final String input) {
System.out.println("Two : " + two)
return two;
}
}
As you can see both methods are returning the same type so Quarkus cannot understand which one to inject. Is there a way to differentiate this?
If I remove the second method in RestApplication then everything works perfectly. In my project, I need to return different values of the same type based on some calculation so I need to use something like this.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
