【问题标题】:Upgrading from Jersey 1.x to 2.x从 Jersey 1.x 升级到 2.x
【发布时间】:2015-09-28 14:02:02
【问题描述】:

请注意:有与此类似的问题,但没有什么可以说明这是一个准确的骗局。


我目前有以下 Gradle 依赖项:

dependencies {
    compile "com.sun.jersey.contribs:jersey-apache-client4:1.18.1"
    compile "com.sun.jersey:jersey-client:1.18.1"
    compile "com.sun.jersey:jersey-core:1.18.1"
    compile "com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:2.3.2"
    compile "org.apache.httpcomponents:httpclient:4.3.2"
    compile "org.slf4j:jcl-over-slf4j:1.7.7"
}

我想将这些升级到 Jersey 的 2.21 版本,但是:

  • 现在groupName好像是org.glassfish.jersey.core而不是com.sun.jersey;和
  • Jersey-Client 现在似乎不需要显式声明 Jersey-Core(Jersey-Core 似乎不再作为 JAR 存在)
  • Jersey Apache 4 客户端 (jersey-apache-client4) 似乎不存在于 2.x 领域

所以我问,我的所有 2.21 依赖项应该是什么?最近那个 Jersey/Apache 客户端藏在哪里?

【问题讨论】:

    标签: java maven jersey jax-rs pom.xml


    【解决方案1】:

    对于泽西岛客户,您只需要

    compile 'org.glassfish.jersey.core:jersey-client:2.21'
    

    jersey-core 不再存在,尽管它可以被认为类似于jersey-common。但是您不必担心这一点。 jersey-client 拉进来。

    对于 Apache,Jersey 2 使用连接器提供程序。见Client Transport Connectors。你想要的是 Apache 连接器

    compile 'org.glassfish.jersey.connectors:jersey-apache-connector:2.21'
    

    然后只需配置Client

    ClientConfig config = new ClientConfig();
    config.connectorProvider(new ApacheConnectorProvider());
    config.register(JacksonJsonProvider.class);
    Client client = ClientBuilder.newClient(config);
    

    您还应该摆脱当前的httpclient 依赖项。 jersey-apache-connector 拉进来。

    有关新客户端 API 的一般用法,请参阅Chapter 5. Client API


    您的问题标题非常笼统,尽管从您的依赖项的外观来看,您只是尝试实现客户端。但是对于其他想要实现服务器的人,他们可以查看

    【讨论】:

    • 谢谢你(一如既往)@peeskillet (+1) - 但是jersey-apache-connector 仍然给我带来麻烦。我在Maven Central repo 上看到它,但是当我将它添加到我的build.gradle 并运行./gradlew eclipse 时,它似乎没有下载jar。如果我进入 Eclipse 并刷新我的项目,我会收到“Missing required library”错误。果然,当我进入~/.gradle/caches/module-2/files-2.1 时,我看到一堆泽西文物,但没有jersey-apache-connector
    • 这个 jar/pom 是不是坏了?我的编译声明与您发布的完全一样...想法?再次感谢!
    • 我没有使用 Gradle 对其进行测试,但它在 Maven 中运行良好,并且由于它在 Maven 中心,它应该运行良好与 Gradle。不确定是什么问题。您是否尝试使用 Maven 将其添加到本地 maven 中,并在 Gradle 构建中将 mavenLocal() 添加到您的 repositories 中?
    • 现在我真的很困惑。你是对的——我的repositories 中没有定义mavenLocalmavenCentral,这令人费解,因为我已经完成了一半以上的开发,到目前为止我还没有遇到任何问题。所以是的,我现在正在删除jersey-apache-connector(谢谢!)。 但是,我认为我还没有走出困境......
    • WebTarget 上似乎不再有get/post/put/delete 方法,并且似乎不再有client.destroy() 方法。我没有找到任何关于正确使用WebTarget 的文档,也没有找到关于如何礼貌地关闭客户端的任何文档……有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 2023-04-08
    • 2014-06-02
    • 1970-01-01
    • 2013-10-11
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多