I’m creating a simple “self-hosted” rest application with jersey and jakarta.
Main Method:
<code>@ApplicationPath("/")
public class Program extends ResourceConfig {
public static void main(String[] args) throws IOException, InterruptedException {
final ResourceConfig resourceConfig = new Program();
resourceConfig.register(VersionController.class);
resourceConfig.register(VersionService.class);
resourceConfig.register(VersionServiceImpl.class);
final HttpServer server = HttpServer.createSimpleServer(null, 8080);
server.getServerConfiguration().addHttpHandler(RuntimeDelegate.getInstance().createEndpoint(resourceConfig, HttpHandler.class));
Thread.currentThread().join();
<code>@ApplicationPath("/")
public class Program extends ResourceConfig {
public static void main(String[] args) throws IOException, InterruptedException {
final ResourceConfig resourceConfig = new Program();
resourceConfig.register(VersionController.class);
resourceConfig.register(VersionService.class);
resourceConfig.register(VersionServiceImpl.class);
final HttpServer server = HttpServer.createSimpleServer(null, 8080);
server.getServerConfiguration().addHttpHandler(RuntimeDelegate.getInstance().createEndpoint(resourceConfig, HttpHandler.class));
server.start();
Thread.currentThread().join();
}
}
</code>
@ApplicationPath("/")
public class Program extends ResourceConfig {
public static void main(String[] args) throws IOException, InterruptedException {
final ResourceConfig resourceConfig = new Program();
resourceConfig.register(VersionController.class);
resourceConfig.register(VersionService.class);
resourceConfig.register(VersionServiceImpl.class);
final HttpServer server = HttpServer.createSimpleServer(null, 8080);
server.getServerConfiguration().addHttpHandler(RuntimeDelegate.getInstance().createEndpoint(resourceConfig, HttpHandler.class));
server.start();
Thread.currentThread().join();
}
}
Controller:
<code>@jakarta.ws.rs.Path("/version")
public class VersionController {
@jakarta.ws.rs.core.Context
private ResourceContext resourceContext; // This injection works
private VersionService service; // Fails:
@jakarta.ws.rs.Produces(MediaType.TEXT_PLAIN)
public String getVersion() {
//return "Hello From Service Controller"; /* works when @Inject is removed */
return service.getAppVersion();
<code>@jakarta.ws.rs.Path("/version")
public class VersionController {
@jakarta.ws.rs.core.Context
private ResourceContext resourceContext; // This injection works
@jakarta.inject.Inject
private VersionService service; // Fails:
@jakarta.ws.rs.GET
@jakarta.ws.rs.Produces(MediaType.TEXT_PLAIN)
public String getVersion() {
//return "Hello From Service Controller"; /* works when @Inject is removed */
return service.getAppVersion();
}
}
</code>
@jakarta.ws.rs.Path("/version")
public class VersionController {
@jakarta.ws.rs.core.Context
private ResourceContext resourceContext; // This injection works
@jakarta.inject.Inject
private VersionService service; // Fails:
@jakarta.ws.rs.GET
@jakarta.ws.rs.Produces(MediaType.TEXT_PLAIN)
public String getVersion() {
//return "Hello From Service Controller"; /* works when @Inject is removed */
return service.getAppVersion();
}
}
Service Interface:
public interface VersionService {
<code>@Contract
public interface VersionService {
String getAppVersion();
}
</code>
@Contract
public interface VersionService {
String getAppVersion();
}
Service Implementation:
public class VersionServiceImpl implements VersionService {
public String getAppVersion() {
String version = getClass().getPackage().getImplementationVersion();
if(version==null || version.isBlank()) {
<code>@Service
@RequestScoped
public class VersionServiceImpl implements VersionService {
@Override
public String getAppVersion() {
String version = getClass().getPackage().getImplementationVersion();
if(version==null || version.isBlank()) {
return "Unknown";
} else {
return version;
}
}
}
</code>
@Service
@RequestScoped
public class VersionServiceImpl implements VersionService {
@Override
public String getAppVersion() {
String version = getClass().getPackage().getImplementationVersion();
if(version==null || version.isBlank()) {
return "Unknown";
} else {
return version;
}
}
}
My controller works perfectly fine if I remove @Inject private VersionService service;
. With @Inject I get the following error:
<code>WARNING: An exception mapping did not successfully produce and processed a response. Logging the exception propagated to the default exception mapper.
org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available in __HK2_Generated_0 for injection at SystemInjecteeImpl(requiredType=VersionService,parent=VersionController,qualifiers={},position=-1,optional=false,self=false,unqualified=null,70775954)
java.lang.IllegalArgumentException: While attempting to resolve the dependencies of com.example.controller.rest.VersionController errors were found
java.lang.IllegalStateException: Unable to perform operation: resolve on com.example.controller.rest.VersionController
<code>WARNING: An exception mapping did not successfully produce and processed a response. Logging the exception propagated to the default exception mapper.
org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available in __HK2_Generated_0 for injection at SystemInjecteeImpl(requiredType=VersionService,parent=VersionController,qualifiers={},position=-1,optional=false,self=false,unqualified=null,70775954)
java.lang.IllegalArgumentException: While attempting to resolve the dependencies of com.example.controller.rest.VersionController errors were found
java.lang.IllegalStateException: Unable to perform operation: resolve on com.example.controller.rest.VersionController
</code>
WARNING: An exception mapping did not successfully produce and processed a response. Logging the exception propagated to the default exception mapper.
org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available in __HK2_Generated_0 for injection at SystemInjecteeImpl(requiredType=VersionService,parent=VersionController,qualifiers={},position=-1,optional=false,self=false,unqualified=null,70775954)
java.lang.IllegalArgumentException: While attempting to resolve the dependencies of com.example.controller.rest.VersionController errors were found
java.lang.IllegalStateException: Unable to perform operation: resolve on com.example.controller.rest.VersionController
What am I missing? In my pom.xml I have latest versions of jersey-container-grizzly2-http
and jersey-hk2
(version 4.0.0-M1) running on JDK-21. I gave older versions a try, but resulted in same error.
I also get the following warnings at the start of the application:
<code>WARNING: A class jakarta.activation.DataSource for a default provider MessageBodyWriter<jakarta.activation.DataSource> was not found. The provider is not available.
WARNING: JAX-B API not found . WADL feature is disabled.
WARNING: A provider com.example.operation.ix.VersionService registered in SERVER runtime does not implement any provider interfaces applicable in the SERVER runtime. Due to constraint configuration problems the provider com.example.operation.ix.VersionService will be ignored.
WARNING: A provider com.example.operation.VersionServiceImpl registered in SERVER runtime does not implement any provider interfaces applicable in the SERVER runtime. Due to constraint configuration problems the provider com.example.operation.VersionServiceImpl will be ignored.
<code>WARNING: A class jakarta.activation.DataSource for a default provider MessageBodyWriter<jakarta.activation.DataSource> was not found. The provider is not available.
WARNING: JAX-B API not found . WADL feature is disabled.
WARNING: A provider com.example.operation.ix.VersionService registered in SERVER runtime does not implement any provider interfaces applicable in the SERVER runtime. Due to constraint configuration problems the provider com.example.operation.ix.VersionService will be ignored.
WARNING: A provider com.example.operation.VersionServiceImpl registered in SERVER runtime does not implement any provider interfaces applicable in the SERVER runtime. Due to constraint configuration problems the provider com.example.operation.VersionServiceImpl will be ignored.
</code>
WARNING: A class jakarta.activation.DataSource for a default provider MessageBodyWriter<jakarta.activation.DataSource> was not found. The provider is not available.
WARNING: JAX-B API not found . WADL feature is disabled.
WARNING: A provider com.example.operation.ix.VersionService registered in SERVER runtime does not implement any provider interfaces applicable in the SERVER runtime. Due to constraint configuration problems the provider com.example.operation.ix.VersionService will be ignored.
WARNING: A provider com.example.operation.VersionServiceImpl registered in SERVER runtime does not implement any provider interfaces applicable in the SERVER runtime. Due to constraint configuration problems the provider com.example.operation.VersionServiceImpl will be ignored.
I feel that the problem is as simple as like I’m missing a dependency. I tried adding jakarta.jakartaee-api
, jakarta.inject-api
, jaxb-runtime
and a few others, but the problem persisted. Some other things that I already tried without success are: adding ServiceLocator
and ServiceLocatorProvider
, using ServiceLocatorUtilities.createAndPopulateServiceLocator()
, etc.