Dependency Injection in Grizzly 4.0

I’m creating a simple “self-hosted” rest application with jersey and jakarta.

Main Method:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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>
<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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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>
<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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@Contract
public interface VersionService {
String getAppVersion();
}
</code>
<code>@Contract public interface VersionService { String getAppVersion(); } </code>
@Contract
public interface VersionService {
    String getAppVersion();
}

Service Implementation:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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>
<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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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>
<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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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>
<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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật