Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic dependency injection with jersey from business layer

I am working on a project with RESTful services. I have modules as web layer, business layer and so. I added basic api layer (using jersey) and I get basic response for get request. Now I must connect it to business layer. I was googling but I am not sure how to implement each solutions to my project.

This is my resource class for trip:

@Path("trip")
public class TripResource {

    @Context
    private UriInfo context;
    @Inject
    private AdminService adminService;

    public TripResource() {
    }

    @GET
    @Produces("text/plain")
    public List<TripDTO> getText() {
        return adminService.listAllTrips();
    }

}

and this I use for adding resources classes:

@javax.ws.rs.ApplicationPath("api")
public class ApplicationConfig extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> resources = new java.util.HashSet<Class<?>>();
        addRestResourceClasses(resources);
        return resources;
    }
    private void addRestResourceClasses(Set<Class<?>> resources) {
        resources.add(cz.infi.javatravelagency.ServiceResource.class);
        resources.add(cz.infi.javatravelagency.TripResource.class);
    }
}

My pom.xml:

<name>JavaTravelAgency - Api module</name>
    <dependencies>
         <dependency>
            <groupId>cz.infi</groupId>
            <artifactId>javatravelagency-business</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.4.1</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
            <version>2.4.1</version>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <type>jar</type>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <!-- Java language version -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>6</source>
                    <target>6</target>
                </configuration>
            </plugin>
            <!-- Servlet 3.0 without web.xml -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>

I tried to follow answer in this link. And I just added:

public class MyApplicationBinder extends AbstractBinder {
    @Override
    protected void configure() {
        bind(AdminServiceImpl.class).to(AdminService.class);
    }
}

and now I am stuck.

How can I add this binder to my config class? What's the easiest implementation without using any other technology?

like image 976
Libor Zapletal Avatar asked Dec 03 '13 18:12

Libor Zapletal


1 Answers

this also cost me a lot of time.

Try the following:

  1. Add a HK2 Binder to your project as described here: https://jersey.java.net/documentation/latest/migration.html

Here you have to add the binding to your business logic. You have this already (just added for completeness).

e. g.

public class MyBinder extends AbstractBinder {

    @Override
    protected void configure() {
        // request scope binding
        bind(MyInjectablePerRequest.class)
                .to(MyInjectablePerRequest.class)
                .in(RequestScope.class);
        // singleton binding
        bind(MyInjectableSingleton.class).in(Singleton.class);
        // singleton instance binding
        bind(new MyInjectableSingleton()).to(MyInjectableSingleton.class);
    }
}

Then add a "ResourceConfig" class to your project and register your binder like here: http://sleeplessinslc.blogspot.de/2012/10/jax-rs-20-jersey-20-preview-example.html

In your case you could simply extend your ApplicationConfig from ResourceConfig instead of ApplicationConfig (this is what I did). All classes registered in "getClasses()" should then be like described below.

e. g.

/**
 * Application config
 */
public class ApplicationConfig extends ResourceConfig {

    public ApplicationConfig() {
        register(SomeResources.class, SomeProviders.class);

        // Register different Binders
        addBinders(new MyBinder());
    }
}

At least ensure that your web.xml uses the config. This depends on your setup (glassfish, servlet v1 / v2, etc.)

As you're already using the ApplicationConfig class, chances are good, that you're using the correct settings already.

Again here is an example:

<servlet>
    <servlet-name>om.example.package.to.your.config.ApplicationConfig</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>com.example.package.to.your.config.ApplicationConfig</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

Hope this will help ;)

Regards Ben


Found a similar post right now: Dependency injection with Jersey 2.0

like image 95
Ben Avatar answered Oct 19 '22 19:10

Ben