繁体   English   中英

配置 Jetty、Jersey 和 Guice

[英]Configuring Jetty, Jersey, and Guice

我正在重构旧的 Java 代码库,以向 Jersey 资源类提供基于 Guice 的依赖注入。

这是一个精简的应用程序,它使用传统的 Jetty/Jersey 设置(请参阅Main & Application )以及我尝试使用他们关于 servletswiki 文章连接 Guice 的尝试:

构建.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'
}

主程序

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

}

应用程序.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;
    }

}

现有模块.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;
    }

}

美食岛

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。

正如评论中指出的那样,在尝试连接 Guice 之前,我需要确定 Jersey 的第 1 版或第 2 版。 我和泽西岛 2 一起去了。

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

构建.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'
}

应用程序.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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM