1

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>
    .....
Community
  • 1
  • 1
dermoritz
  • 12,519
  • 25
  • 97
  • 185

2 Answers2

3

Sample project trying to solve this problem: https://github.com/miguelangelprogramacion/jersey-dependency-injection

I used the recommendations of peeskillet, with a modification in the final register process, as you can see in ResourceConfigApp.java

1

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"

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Gimme a minute to test. Just at a glance, that really looked like all that was wrong. What's with the `@ManagedBean` though? Also you are on what, Tomcat 6? I'm looking at your web.xml and it show servlet 2.5. I just want to get the environment right before I test. And the overriden `configure()` method is inside the `Binder` class right? – Paul Samsotha Mar 11 '15 at 08:49
  • Thanks for taking the time. Yeah the configure is in Binder class. I don't know if @ManagedBean is needed (i tried many things). – dermoritz Mar 11 '15 at 09:07
  • Ok, so on top the the above part of the answer, it should also be `bind(DataDbImpl.class).to(Data.class).in(Singleton.class);`. That works for me :-) – Paul Samsotha Mar 11 '15 at 09:13
  • now the error has changed: There was no object available for injection at SystemInjecteeImpl(requiredType=Connection,parent=DataDbImpl (don't know if this is better) (for me it is very strange to bind implementation to interface, normally it is the other way around?!) I came from guice an there it is bind.interface.to.impl. – dermoritz Mar 11 '15 at 09:17
  • I create a `Data` interface, and `DataDbImpl` that implements it. I created a `ExcelReport` class that takes `Data` as a constructor arg injecting it the `@Inject` – Paul Samsotha Mar 11 '15 at 09:18
  • As far as your concern, look at the [`to`](https://hk2.java.net/hk2-api/apidocs/org/glassfish/hk2/utilities/binding/ServiceBindingBuilder.html) method, it's argument is "named" `contract` – Paul Samsotha Mar 11 '15 at 09:19
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/72743/discussion-between-dermoritz-and-peeskillet). – dermoritz Mar 11 '15 at 09:21
  • 2
    I prefer having it be bind implementation to contract since a single implementation can be easily bound to multiple contracts (in java a class can implement many interfaces...) – jwells131313 Mar 11 '15 at 11:20