java - Jersey : Do I need to have maven dependency for JSR as well? 为什么?

标签 java spring maven spring-mvc

我的 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/

相关文章:

java - 图片加载速度非常慢

Swagger 的 Spring Boot 骨架

java - SPRING Tomcat 请求的资源不可用

java - IncationTargetExceptionMessage 是 : It is impossible to create a new session because 'createSession' error

java - 如何使 Spring AspectJ 注释在 Java 6/7 中工作

java - 使用jquery调用web服务

java - JPA 强制转换异常

java - 类实例化实例,静态是类特定的,那么为什么实例不共享静态属性?

Spring Batch - 读取多行日志消息

java - 每次修改代码时都必须 mvn clean install