我的 Maven 模块 pom.xml
看起来像
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
<version>2.8</version>
</dependency>
<dependency>
<groupId>com.sun.jersey.test.framework</groupId>
<artifactId>jersey-test-framework</artifactId>
<version>1.0.3.1</version>
<scope>test</scope>
</dependency>
</dependencies>
资源类为
@Service
@Path("/hello")
public class HelloResource {
@GET
public Response hello() {
return Response.ok("Hello World!").build();
}
}
一切正常。
问题
- 我刚刚看到这个post ,其中表示
You need both the JSR and the implementation. The annotations are in the JSR, the implementation provides supporting classes
我可以知道为什么需要这个吗?
最佳答案
其中的依赖项也将通过 Maven 传递依赖机制检索 JSR API jar。因此,如果您问为什么它运行良好,那是因为 jax.ws.rs-api 2.0 将通过 jersey-server -> jersey-common -> jax.ws.rs-api 路径提供。
如果你问为什么两者都需要(即使是通过 Maven 传递依赖机制),那么我认为原因是 API 和实现之间的分离。您的代码在编译时依赖于 JSR API,但在运行时它也需要实现。因此,从理论上讲,可以将一种实现替换为另一种实现,而不会影响代码。
关于java - Jersey : Do I need to have maven dependency for JSR as well? 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23446184/