【问题标题】:Configuring Jetty, Jersey, and Guice配置 Jetty、Jersey 和 Guice
【发布时间】:2018-11-01 23:08:57
【问题描述】:

我正在重构一个遗留的 Java 代码库,以便为 Jersey 资源类提供 Guice 支持的依赖注入。

这是一个精简的应用程序,它使用旧的 Jetty/Jersey 设置(请参阅 MainApplication)以及我尝试使用他们的 wiki article on servlets 连接 Guice:

build.gradle

plugins {
    id 'java'
}

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.projectlombok:lombok:1.16.18'
    compile 'com.google.inject:guice:4.1.0'
    compile 'com.google.inject.extensions:guice-servlet:4.1.0'
    compile 'com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:2.9.3'
    compile 'org.eclipse.jetty:jetty-server:9.4.8.v20171121'
    compile 'org.eclipse.jetty:jetty-servlet:9.4.8.v20171121'
    compile 'org.glassfish.jersey.media:jersey-media-sse:2.26'
    compile 'com.sun.jersey:jersey-servlet:1.19.4'
}

Main.java

package org.arabellan.sandbox;

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.servlet.ServletModule;

import java.util.ArrayList;
import java.util.List;

public class Main {

    static Injector injector;

    public static void main(String[] args) throws Exception {
        List<AbstractModule> modules = new ArrayList<>();
        modules.add(new ExistingModule());
        modules.add(new ServletModule());
        injector = Guice.createInjector(modules);
        injector.getInstance(Application.class).run();
    }

}

Application.java

package org.arabellan.sandbox;

import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
import com.google.inject.servlet.GuiceFilter;
import com.sun.jersey.spi.container.servlet.ServletContainer;
import org.glassfish.jersey.message.DeflateEncoder;
import org.glassfish.jersey.message.GZipEncoder;
import org.glassfish.jersey.server.ResourceConfig;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.glassfish.jersey.server.filter.EncodingFilter;

class Application {

    void run() throws Exception {
        Server jettyServer = new Server(8080);
        ServletContextHandler httpContext = new ServletContextHandler(jettyServer, "/");
        httpContext.addEventListener(new GuiceServletConfig());
        httpContext.addFilter(GuiceFilter.class, "/*", null);
        httpContext.addServlet(new ServletHolder(new ServletContainer(buildResourceConfig())), "/*");
        jettyServer.setHandler(httpContext);
        jettyServer.start();
    }

    private ResourceConfig buildResourceConfig() {
        ResourceConfig config = new ResourceConfig();
        config.register(JacksonJsonProvider.class);
        config.registerClasses(EncodingFilter.class, GZipEncoder.class, DeflateEncoder.class);
        config.packages("org.arabellan.sandbox");
        return config;
    }

}

ExistingModule.java

package org.arabellan.sandbox;

import com.google.inject.AbstractModule;

public class ExistingModule extends AbstractModule {

    protected void configure() {
        bind(FooDao.class).to(DynamoDBFooDao.class);
    }

}

GuiceServletConfig.java

package org.arabellan.sandbox;

import com.google.inject.Injector;
import com.google.inject.servlet.GuiceServletContextListener;

public class GuiceServletConfig extends GuiceServletContextListener {

    @Override
    protected Injector getInjector() {
        return Main.injector;
    }

}

FooResource.java

package org.arabellan.sandbox;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

@Path("/foo")
public class FooResource {

    private final FooDao dao;

    @Inject
    public FooResource(FooDao dao) {
        this.dao = dao;
    }

    @GET
    @Path("/{id}")
    public Response getById(@PathParam("id") String id) {
        return Response.ok(dao.getById(id)).build();
    }

}

DynamoDBFooDao.java

package org.arabellan.sandbox;

import javax.inject.Singleton;

@Singleton
public class DynamoDBFooDao implements FooDao {

    public String getById(String id) {
        return id;
    }

}

FooDao.java

package org.arabellan.sandbox;

interface FooDao {

    String getById(String id);

}

我无法理解各种组件以及它们如何协同工作。因此,我不断收到以下错误:

SEVERE: The following errors and warnings have been detected with resource and/or provider classes:
  SEVERE: Missing dependency for constructor public org.arabellan.sandbox.FooResource(org.arabellan.sandbox.FooDao) at parameter index 0

如果我直接在FooResource 的构造函数中访问 Guice 注入器,那么它就可以工作。这告诉我 Jetty/Jersey 的东西已正确设置以提供资源,并且 Guice 能够正确构建它的依赖关系树。我相信这意味着问题在于让 Jersey 在构建资源时使用 Guice。

【问题讨论】:

  • 第一个错误是试图混合 Jersey 1.x 和 Jersey 2.x。你需要先弄清楚你要使用哪个。然后从那里工作。您集成 Guice 的方式将与 Jersey 1.x 完全不同。和泽西岛 2.x.
  • 这是有道理的。在将 Guice 引入泽西岛之前,我将努力重构代码以使用其中一种。
  • Jetty 9.4.8 有几个与之关联的 CVE,请考虑升级。 - eclipse.org/lists/jetty-announce/msg00123.html

标签: java jersey jetty guice


【解决方案1】:

正如 cmets 中所指出的,在尝试连接 Guice 之前,我需要确定 Jersey 的版本 1 或 2。我和泽西 2 一起去了。

然而,我最初的假设是正确的,需要设置 Guice 和 Jersey(或者更确切地说是 HK2)之间的联系。我通过GuiceToHK2 类促进了这一点。我不想在两个地方定义 DI 绑定,所以这个解决方案循环遍历所有 Guice 绑定,将它们过滤到特定的包(可选),然后在 HK2 中绑定它们。

build.gradle

plugins {
    id 'java'
}

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.projectlombok:lombok:1.16.18'
    compile 'com.google.inject:guice:4.1.0'
    compile 'com.google.inject.extensions:guice-servlet:4.1.0'
    compile 'com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:2.9.3'
    compile 'org.eclipse.jetty:jetty-server:9.4.8.v20171121'
    compile 'org.eclipse.jetty:jetty-servlet:9.4.8.v20171121'
    compile 'org.glassfish.jersey.containers:jersey-container-jetty-servlet:2.26'
    compile 'org.glassfish.jersey.media:jersey-media-sse:2.26'
    compile 'org.glassfish.jersey.inject:jersey-hk2:2.26'
}

Application.java

package org.arabellan.sandbox;

import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.glassfish.jersey.message.DeflateEncoder;
import org.glassfish.jersey.message.GZipEncoder;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.filter.EncodingFilter;
import org.glassfish.jersey.servlet.ServletContainer;

class Application {

    void run() throws Exception {
        ServletContextHandler httpContext = new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
        ServletContainer container = new ServletContainer(buildResourceConfig());
        ServletHolder holder = new ServletHolder(container);
        httpContext.setContextPath("/");
        httpContext.addServlet(holder, "/*");

        Server jettyServer = new Server(8080);
        jettyServer.setHandler(httpContext);
        jettyServer.start();
    }

    private ResourceConfig buildResourceConfig() {
        ResourceConfig config = new ResourceConfig();
        config.register(new GuiceToHK2(Main.injector));
        config.register(JacksonJsonProvider.class);
        config.registerClasses(EncodingFilter.class, GZipEncoder.class, DeflateEncoder.class);
        config.packages("org.arabellan.sandbox");
        return config;
    }

}

GuiceToHK2.java

package com.flightstats.hub.app;

import com.google.inject.Injector;
import com.google.inject.Key;
import lombok.extern.slf4j.Slf4j;
import org.glassfish.hk2.api.Factory;
import org.glassfish.hk2.utilities.binding.AbstractBinder;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

@Slf4j
class GuiceToHK2 extends AbstractBinder {

    private final Injector injector;

    GuiceToHK2(Injector injector) {
        this.injector = injector;
    }

    @Override
    protected void configure() {
        injector.getBindings().forEach((key, value) -> {
            if (isNamedBinding(key)) {
                bindNamedClass(key);
            } else {
                bindClass(key);
            }
        });
    }

    private boolean isNamedBinding(Key<?> key) {
        return key.getAnnotationType() != null && key.getAnnotationType().getSimpleName().equals("Named");
    }

    private void bindClass(Key<?> key) {
        try {
            String typeName = key.getTypeLiteral().getType().getTypeName();
            log.info("mapping guice to hk2: {}", typeName);
            Class boundClass = Class.forName(typeName);
            bindFactory(new ServiceFactory<>(boundClass)).to(boundClass);
        } catch (ClassNotFoundException e) {
            log.warn("unable to bind {}", key);
        }
    }

    private void bindNamedClass(Key<?> key) {
        try {
            String typeName = key.getTypeLiteral().getType().getTypeName();
            Method value = key.getAnnotationType().getDeclaredMethod("value");
            String name = (String) value.invoke(key.getAnnotation());
            log.info("mapping guice to hk2: {} (named: {})", typeName, name);
            Class boundClass = Class.forName(typeName);
            bindFactory(new ServiceFactory<>(boundClass)).to(boundClass).named(name);
        } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            log.warn("unable to bind {}", key);
        }
    }

    private class ServiceFactory<T> implements Factory<T> {

        private final Class<T> serviceClass;

        ServiceFactory(Class<T> serviceClass) {
            this.serviceClass = serviceClass;
        }

        public T provide() {
            return injector.getInstance(serviceClass);
        }

        public void dispose(T versionResource) {
            // do nothing
        }
    }

}

这不是一个防弹的解决方案,但它解决了我的问题。 它假定需要注入我的资源的所有东西都在org.arabellan.sandbox 包中,而不是@Named

更新:通过删除假设使解决方案更加通用。

【讨论】:

  • 你可能会从this得到一些想法
  • 我认为使用上面评论中建议的 hk2-guice 网桥是可行的方法
【解决方案2】:

嗯,对我来说,您似乎执行了以下 URL 之一:

因此该函数的字符串参数“id”:“public Response getById(@PathParam("id") String id)”为空。这会导致您的错误。

这只是一个假设。如果我是对的,请您检查一下,请

【讨论】:

  • 如果我不依赖 Jersey 向构造函数提供依赖项,那么该资源就可以正常工作。我得到的错误是指资源类的构造函数,而不是getById 方法调用。
猜你喜欢
  • 2012-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-13
  • 2016-04-11
  • 2021-04-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多