使用Jersey2.0的依赖注入从零开始,在没有任何先前Jersey1.x知识的情况下,我很难理解如何在Jersey2.0项目中设置依赖注入。我也知道在泽西岛2.0版中可以使用HK2,但我似乎找不到有助于泽西岛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@ManagedBeanpublic class MyService {
void serviceCall() {
System.out.print("Service calls");
}}柚木<properties>
<jersey.version>2.0-rc1</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies></dependencyManagement><dependencies>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jax-rs-ri</artifactId>
</dependency></dependencies>我的入门项目可在GitHub获得:https://github.com/donaldjarmstrong/jaxrs
添加回答
举报
0/150
提交
取消