相关疑难解决方法(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万
查看次数

标签 统计

dependency-injection ×1

hk2 ×1

java ×1

jersey ×1

jersey-2.0 ×1