相关疑难解决方法(0)

使用Jersey 2.0进行依赖注入

从头开始没有任何以前的Jersey 1.x知识,我很难理解如何在我的Jersey 2.0项目中设置依赖注入.

我也明白HK2可用于Jersey 2.0,但我似乎无法找到有助于Jersey 2.0集成的文档.

@ManagedBean
@Path("myresource")
public class MyResource {

    @Inject
    MyService myService;

    /**
     * Method handling HTTP GET requests. The returned object will be sent
     * to the client as "text/plain" media type.
     *
     * @return String that will be returned as a text/plain response.
     */
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/getit")
    public String getIt() {
        return "Got it {" + myService + "}";
    }
}

@Resource
@ManagedBean
public class MyService {
    void serviceCall() {
        System.out.print("Service calls");
    }
}
Run Code Online (Sandbox Code Playgroud)

的pom.xml …

java dependency-injection jersey jersey-2.0 hk2

106
推荐指数
4
解决办法
9万
查看次数

如何在灰熊上使用球衣2.0 guice

我想在Grizzly上使用Guice + Jersey 2.0.根据这个如何使用泽西2.0的guice-servlet?讨论目前没有针对Jersey2的直接Guice集成,但可以使用HK2作为桥梁来实现.我还检查了Github中的示例项目https://github.com/piersy/jersey2-guice-example-with-test.该项目使用Jetty实现.

但我的问题是在Grizzly中实现它.在Jetty上它就像这样使用

  @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);

}
Run Code Online (Sandbox Code Playgroud)

关于grizzly的问题是,如何获取这个serviceLocator对象?

谢谢.

jersey guice grizzly

6
推荐指数
1
解决办法
3717
查看次数

标签 统计

jersey ×2

dependency-injection ×1

grizzly ×1

guice ×1

hk2 ×1

java ×1

jersey-2.0 ×1