gpt4 book ai didi

java - 如何将 Guice 与 Jersey JdkHttpServerFactory 一起使用?

转载 作者:可可西里 更新时间:2023-11-01 17:07:24 24 4
gpt4 key购买 nike

这是一个 Java SE 应用程序。我想专门使用 Guice 进行注入(inject)。我读过 HK2 Binder ,我宁愿只使用 Guice。我熟悉 Guice 并经常使用它,但我不知道从哪里开始,几乎我所有的 Google-fu 都指向基于容器的 Jersey 服务器。我对 Grizzly 也不感兴趣。

那么我该如何实现它,以便我可以使用 Guice 来满足我的注入(inject)需求?

import java.net.URI;
import javax.ws.rs.core.UriBuilder;

import org.glassfish.jersey.jdkhttp.JdkHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.roycat.rnotes.ws.guice.MainModule;
import com.roycat.rnotes.ws.server.CustomResourceConfig;

public class StartRestServer {

public static void main(String[] args) {

String base = "localhost";
Integer port = 8090;

if (args.length == 1) {
base = args[0];
} else if (args.length == 2) {
base = args[0];
port = Integer.parseInt(args[1]);
}

URI baseUri = UriBuilder.fromUri("http://" + base
+ "/").port(port).build();
ResourceConfig config = new CustomResourceConfig();
JdkHttpServerFactory.createHttpServer(baseUri, config);

System.out.println("Server running on " + baseUri.toString()
+ "...");
}
}

最佳答案

您可以使用 HK2 guice-bridge将 Guice 服务桥接到 HK2。

<dependency>
<groupId>org.glassfish.hk2</groupId>
<artifactId>guice-bridge</artifactId>
<version>2.4.0-b31</version>
</dependency>

注意 <version>应该与您的 Jersey 版本引入的 HK2 版本相匹配。看看Jersey拉进来的HK2 jar,应该是有版本的。就我而言,我使用的是 Jersey 2.22.1,它引入了 HK2 2.4.0-b31

然后您需要对桥进行编码。我刚刚在 Feature 中做到了

@Priority(1)
public class GuiceFeature implements Feature {

@Override
public boolean configure(FeatureContext context) {
ServiceLocator locator = ServiceLocatorProvider.getServiceLocator(context);
GuiceBridge.getGuiceBridge().initializeGuiceBridge(locator);
Injector injector = Guice.createInjector(new MyGuiceModule());
GuiceIntoHK2Bridge guiceBridge = locator.getService(GuiceIntoHK2Bridge.class);
guiceBridge.bridgeGuiceInjector(injector);
return true;
}
}

然后使用您的 ResourceConfig 注册该功能

config.register(GuiceFeature.class);

现在您应该能够将 Guice 服务注入(inject)到您的 Jersey 组件中。


更新

这是一个完整的工作测试

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.sun.net.httpserver.HttpServer;
import java.net.URI;
import java.util.logging.Logger;
import javax.annotation.Priority;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Feature;
import javax.ws.rs.core.FeatureContext;
import javax.ws.rs.core.Response;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.jersey.ServiceLocatorProvider;
import org.glassfish.jersey.filter.LoggingFilter;
import org.glassfish.jersey.jdkhttp.JdkHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.jvnet.hk2.guice.bridge.api.GuiceBridge;
import org.jvnet.hk2.guice.bridge.api.GuiceIntoHK2Bridge;
import static org.junit.Assert.assertEquals;

public class JdkHttpGuiceTest {

@Priority(1)
public static class GuiceFeature implements Feature {
@Override
public boolean configure(FeatureContext context) {
ServiceLocator locator = ServiceLocatorProvider.getServiceLocator(context);
GuiceBridge.getGuiceBridge().initializeGuiceBridge(locator);
Injector injector = Guice.createInjector(new EncoderModule());
GuiceIntoHK2Bridge guiceBridge = locator.getService(GuiceIntoHK2Bridge.class);
guiceBridge.bridgeGuiceInjector(injector);
return true;
}
}

public static interface Encoder {
String encode(String value);
}

public static class UppercaseEncoder implements Encoder {
@Override
public String encode(String value) {
return value.toUpperCase();
}
}

static class EncoderModule extends AbstractModule {
@Override
protected void configure() {
bind(Encoder.class).to(UppercaseEncoder.class);
}
}

@Path("test")
public static class TestResource {
@Inject
private Encoder encoder;

@GET
public String get(@QueryParam("s") String s) {
return encoder.encode(s);
}
}

public static class AppConfig extends ResourceConfig {
public AppConfig() {
register(TestResource.class);
register(GuiceFeature.class);
register(new LoggingFilter(Logger.getAnonymousLogger(), true));
}
}

private HttpServer server;

@Before
public void setUp() {
server = JdkHttpServerFactory.createHttpServer(
URI.create("http://localhost:8080/"), new AppConfig());
}

@After
public void tearDown() {
server.stop(1);
server = null;
}

@Test
public void injectionShouldWork() {
final Response response = ClientBuilder.newClient().target("http://localhost:8080/test")
.queryParam("s", "hello")
.request().get();
assertEquals(200, response.getStatus());
assertEquals("HELLO", response.readEntity(String.class));
}
}

使用的依赖

<dependency>
<groupId>org.glassfish.hk2</groupId>
<artifactId>guice-bridge</artifactId>
<version>2.4.0-b31</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jdk-http</artifactId>
<version>2.22.2</version>
</dependency>

关于java - 如何将 Guice 与 Jersey JdkHttpServerFactory 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36140567/

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