Guice - Jersey - Servlet binding

大兔子大兔子 提交于 2019-12-04 10:27:02

I was able to bind Jersey resources to Guice without using the bind-parameters (without explicitly providing the com.sun.jersey.config.property.packages parameter), by binding resources separately

public class BindJerseyResources extends ServletModule {

    @Override
    protected void configureServlets() {
        // excplictly bind GuiceContainer before binding Jersey resources
        // otherwise resource won't be available for GuiceContainer
        // when using two-phased injection
        bind(GuiceContainer.class);

        // bind Jersey resources
        PackagesResourceConfig resourceConfig = new PackagesResourceConfig("jersey.resources.package");
        for (Class<?> resource : resourceConfig.getClasses()) {
            bind(resource);
        }

        // Serve resources with Jerseys GuiceContainer
        serve("/*").with(GuiceContainer.class);
    }
}

The resources are like following

@Path("/")
@RequestScoped
public class Resource {

    private Storage storage;

    @Inject
    public Resource(Storage storage) {
        this.storage = storage;
    }

    @GET
    @Path("get/{name}")
    @Produces(MediaType.TEXT_PLAIN)
    public String getGuid(@PathParam("name") String name) {
        return storage.get(name);
    }
}

Maybe this helps you to avoid the latter problematic binding.


Updated the answer to work with two-phased injection.

Java Part

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Singleton;
import com.google.inject.persist.jpa.JpaPersistModule;
import com.google.inject.servlet.GuiceServletContextListener;
import com.sun.jersey.guice.JerseyServletModule;
import com.sun.jersey.guice.spi.container.servlet.GuiceContainer;
import com.sun.jersey.spi.container.servlet.ServletContainer;
import com.thjug.apipublic.Echo;

public class ServletContextListener extends GuiceServletContextListener {

    @Override
    protected Injector getInjector() {
        final Injector injector = Guice.createInjector(new JerseyServletModule() {
            @Override
            protected void configureServlets() {
                bind(Echo.class);
                bind(ServletContainer.class).in(Singleton.class);
                serve("/*").with(GuiceContainer.class);
            }
        }, new JpaPersistModule("dbUnit"), new LoggingModule());

        injector.getInstance(JPAInitializer.class);
        return injector;
    }
}

web.xml

    <distributable />

    <display-name>API</display-name>

    <filter>
            <filter-name>guiceFilter</filter-name>
            <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
    </filter>
    <filter-mapping>
            <filter-name>guiceFilter</filter-name>
            <url-pattern>/*</url-pattern>
    </filter-mapping>

    <listener>
            <description>Guice Initiate</description>
            <listener-class>com.thjug.base.ServletContextListener</listener-class>
    </listener>

Below is my keynote about How to make REST with Guice http://www.slideshare.net/nuboat/lightweight-javaee

Here's an article on how to do the binding (including full source code): implementing-distributed-counter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!