Guice-Jersey-Servlet绑定 [英] Guice - Jersey - Servlet binding

查看:112
本文介绍了Guice-Jersey-Servlet绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近切换到两阶段注入,这在我的servlet绑定中创建了一个错误.我目前正在两种错误模式之间切换,不确定是否最好选择哪个方向.

I recently switched to two phase injection and this has created an error in my servlet binding. I am currently toggling between two error modes and not sure which direction is best to pursue.

我遇到的第一个错误是:

The first error I encountered was:

com.sun.jersey.api.container.ContainerException:ResourceConfig 实例不包含任何根资源类.

com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes.

我的servlet模块如下:

My servlet module looked like this:

public class MyServletModule extends JerseyServletModule {
    @Override
    protected void configureServlets() {
        bind(MyServlet.class).asEagerSingleton();

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

我能够通过显式提供com.sun.jersey.config.property.packages参数来消除此错误.

I was able to remove this error by explicitly providing the com.sun.jersey.config.property.packages parameter.

public class MyServletModule extends JerseyServletModule {

    @Override
    protected void configureServlets() {
        bind(MyServlet.class).asEagerSingleton();

        Map<String,String> parameters = new HashMap<String, String>();
        parameters.put(PackagesResourceConfig.PROPERTY_PACKAGES, MyServlet.class.getPackage().getName());
        serve("/*").with(GuiceContainer.class, parameters);
    }
}

但是,当我这样做时,Guice尝试进行一次即时绑定,该绑定不遵守我的servlet构造函数上的@Inject.

But when I do this, Guice attempts a Just in Time binding which does not respect the @Inject on my servlet constructor.

com.google.inject.ConfigurationException:Guice配置错误:

com.google.inject.ConfigurationException: Guice configuration errors:

1)无法为MyServlet创建绑定.已经配置好了 在一个或多个子注入器或专用模块上 如果绑定到MyServletModule.configureServlets(MyServletModule.java:44) 在PrivateModule中,您是否忘了公开绑定?尽管 找到MyServlet

1) Unable to create binding for MyServlet. It was already configured on one or more child injectors or private modules bound at MyServletModule.configureServlets(MyServletModule.java:44) If it was in a PrivateModule, did you forget to expose the binding? while locating MyServlet

1个错误 com.google.inject.internal.InjectorImpl.getBinding(InjectorImpl.java:150)

1 error at com.google.inject.internal.InjectorImpl.getBinding(InjectorImpl.java:150)

我的servlet有一个@Inject构造函数,该构造函数的参数不能及时绑定.调试完InjectorImpl后,我相信这就是我使用PROPERTY_PACKAGES时失败的原因.

My servlet has an @Inject constructor who's arguments cannot be bound just in time. After debugging into InjectorImpl, I believe this is the reason that things fail when I use PROPERTY_PACKAGES.

我只是不确定使用PROPERTY_PACKAGES是否正确,是否需要修复一些绑定?或者,如果这是错误的方向,而我需要以其他方式解决原始的ResourceConfig错误.

I'm just not sure if using PROPERTY_PACKAGES is correct and I need to fix some bindings? Or if that is the wrong direction and I need to fix the original ResourceConfig error in a different way.

感谢您的帮助或向正确方向的推动.

Help or a push in the right direction is appreciated.

推荐答案

我无需使用bind-parameters(无需显式提供com.sun.jersey.config.property.packages参数)就可以将Jersey资源绑定到Guice. ,通过分别绑定资源

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);
    }
}

资源如下

@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.

这篇关于Guice-Jersey-Servlet绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆