I am trying hard to get some kind of (c)di work for my little web application. I gave up using Weld and now i am trying hk2 (it is already used by jersey 2). Here is my service that tries to inject something:
@ManagedBean
@Path("excelExport")
public class ExcelExportService {
@Inject
ExcelReport excelReport;
/**
*
* @param idString
* crucial to fetch the report
* @return excel document (xlsx) to be downloaded
*/
@GET
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public final Response getExcel(@QueryParam("id") final String idString) {
Long id;
try {
id = Long.valueOf(idString);
} catch (NumberFormatException e) {
return Response.status(Status.BAD_REQUEST).build();
}
return Response.ok(excelReport.getCompleteExcel(id), MediaType.APPLICATION_OCTET_STREAM)
.header("Content-Disposition", "attachment; filename=\"" + "report-" + id + ".xlsx" + "\"")
.build();
}
}
I am not sure if @ManagedBean
is necessary. The Problem is:
1. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=ExcelReport,parent=ExcelExportService,qualifiers={},position=-1,optional=false,self=false,unqualified=null,1464117566)
So the inject is simply not working. I set up DI as it is described here (see accepted answer). In my case the binder look like that:
@Override
protected void configure() {
bind(ExcelReport.class).to(ExcelReport.class);
bind(Data.class).to(DataDbImpl.class).in(Singleton.class);
}
I think i wouldn't need the bind of ExcelReport because it has a Constructor that injects Data - right? But it isn't working either way.
My "main" is this
public class WebApp extends ResourceConfig{
public WebApp(){
register(Binder.class);
packages("true", "com.prodyna.reportExport.service");
}
}
and web.xml:
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>reportExport</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.prodyna.WebApp</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>reportExport</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
</web-app>
So how to setup DI properly (my third try will be Guice).
UPDATE I am also unsure about the dependencies needed. currently i included this (updated based on hint):
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>2.16</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
</dependency>
.....
It should not be register(Binder.class);
, but instead register(new Binder());
UPDATE
Ok, so on top the the above part of the answer, it should also be
bind(DataDbImpl.class).to(Data.class).in(Singleton.class);
The binding basically reads "bind service to contract"