问题描述
我想在Grizzly上使用Guice + Jersey 2.0.根据此如何在泽西岛2.0中使用guice-servlet?讨论目前没有针对Jersey2的直接Guice集成,但是可以使用HK2作为桥梁来实现.我还在Github中检查了示例项目 https://github.com/piersy/jersey2-guice-example-with-test .该项目是使用Jetty实施的.
I want to use Guice + Jersey 2.0 on Grizzly. According to this How to use guice-servlet with Jersey 2.0? discussion there is no direct Guice integration for Jersey2 at present but it can be achieved using HK2 as a bridge. I also checked the sample project in Github https://github.com/piersy/jersey2-guice-example-with-test . This project is implemented using Jetty.
但是我的问题是要在Grizzly中实现它.在码头上,它是这样使用的
But my problem is to implement it in Grizzly. On Jetty it is used like this
@Inject
public MyApplication(ServiceLocator serviceLocator) {
// Set package to look for resources in
packages("example.jersey");
System.out.println("Registering injectables...");
GuiceBridge.getGuiceBridge().initializeGuiceBridge(serviceLocator);
GuiceIntoHK2Bridge guiceBridge = serviceLocator.getService(GuiceIntoHK2Bridge.class);
guiceBridge.bridgeGuiceInjector(Main.injector);
}
我对灰熊的问题是,如何获取此serviceLocator对象?
My problem on grizzly is , how to get this serviceLocator object?
谢谢.
推荐答案
我在此处创建了示例 https://github.com/oleksiys/samples/tree /master/jersey2-guice-example-with-test
I have created the sample herehttps://github.com/oleksiys/samples/tree/master/jersey2-guice-example-with-test
Grizzly初始化代码如下:
The Grizzly initialization code looks like this:
final URI uri = UriBuilder.fromUri("http://127.0.0.1/")
.port(8080).build();
// Create HttpServer
final HttpServer serverLocal = GrizzlyHttpServerFactory.createHttpServer(uri, false);
// Create Web application context
final WebappContext context = new WebappContext("Guice Webapp sample", "");
context.addListener(example.jersey.Main.class);
// Initialize and register Jersey ServletContainer
final ServletRegistration servletRegistration =
context.addServlet("ServletContainer", ServletContainer.class);
servletRegistration.addMapping("/*");
servletRegistration.setInitParameter("javax.ws.rs.Application",
"example.jersey.MyApplication");
// Initialize and register GuiceFilter
final FilterRegistration registration =
context.addFilter("GuiceFilter", GuiceFilter.class);
registration.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), "/*");
context.deploy(serverLocal);
serverLocal.start();
这篇关于如何在Grizzly上使用球衣2.0 Guice的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!