我正在重构遗留的Java代码库,以提供Guice支持的依赖注入到Jersey资源类。
这是一个精简的应用程序,它使用传统的Jetty / Jersey设置(请参阅Main
和Application
)以及我尝试使用其wiki article on servlets连接Guice的尝试:
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();
}
}
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;
}
}
package org.arabellan.sandbox;
import com.google.inject.AbstractModule;
public class ExistingModule extends AbstractModule {
protected void configure() {
bind(FooDao.class).to(DynamoDBFooDao.class);
}
}
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;
}
}
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();
}
}
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。
答案 0 :(得分:1)
正如评论中指出的那样,在连接Guice之前,我需要选择Jersey的1版或2版。我选择了泽西岛2。
但是,我最初的假设是正确的,因此必须设置Guice和Jersey(或HK2)之间的联系。我通过GuiceToHK2
类对此进行了简化。我不想在两个地方定义DI绑定,所以此解决方案循环遍历所有Guice绑定,将它们过滤到特定的程序包(可选),然后将其绑定到HK2中。
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'
}
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;
}
}
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
更新:通过消除假设使解决方案更通用。
答案 1 :(得分:0)
hmmn对我来说,您似乎执行了以下URL之一:
,因此此函数的字符串参数“ id”:“ public Response getById(@PathParam(“ id”)字符串id)“为空。这会导致您的错误。
这只是一个假设。请问我是否正确,请