gpt4 book ai didi

java - 配置 Jetty、Jersey 和 Guice

转载 作者:行者123 更新时间:2023-11-30 05:56:17 25 4
gpt4 key购买 nike

我正在重构旧版 Java 代码库,以向 Jersey 资源类提供 Guice 支持的依赖项注入(inject)。

这是一个精简的应用程序,它使用旧版 Jetty/Jersey 设置(请参阅 MainApplication )以及我尝试使用其 wiki article on servlets 连接 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'
}

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

}

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

}

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注入(inject)器的构造函数然后它就可以工作了。这告诉我 Jetty/Jersey 的内容已正确设置以服务资源,并且 Guice 能够正确构建其依赖关系树。我相信这意味着问题在于让 Jersey 在构建资源时使用 Guice。

最佳答案

正如评论中所指出的,在尝试连接 Guice 之前,我需要选择 Jersey 的版本 1 或 2。我选择了 Jersey 2。

然而,我最初的假设是正确的,Guice 和 Jersey(或者更确切地说 HK2)之间的联系需要建立。我使用 GuiceToHK2 类促进了这一点。我不想在两个地方定义 DI 绑定(bind),因此该解决方案循环遍历所有 Guice 绑定(bind),将它们过滤到特定的包(可选),然后将它们绑定(bind)到 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
}
}

}

这不是一个万无一失的解决方案,但它解决了我的问题。 它假设需要注入(inject)到我的资源中的所有内容都位于 org.arabellan.sandbox 包中,而不是 @Named

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

关于java - 配置 Jetty、Jersey 和 Guice,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53110625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com