【问题标题】:How to avoid dependency conflict when using dropwizard + jersey client使用dropwizard + jersey客户端时如何避免依赖冲突
【发布时间】:2015-07-11 17:36:04
【问题描述】:

我编写了一个 DropWizard REST API,并且可以正常工作。其中一个资源端点实际上会写一封电子邮件,但是一旦我添加以下依赖项,DropWizard 就会在启动时开始失败

<dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-client</artifactId>
        <version>1.18.1</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-core</artifactId>
        <version>1.18.1</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey.contribs</groupId>
        <artifactId>jersey-multipart</artifactId>
        <version>1.18.1</version>
    </dependency>

DropWizard 依赖项是:

<dependency>
        <groupId>io.dropwizard</groupId>
        <artifactId>dropwizard-core</artifactId>
        <version>0.8.1</version>
    </dependency>

启动时的错误真的很长,总结如下

    WARN  [2015-05-01 20:06:08,887] org.glassfish.jersey.internal.Errors: The following warnings have been detected: WARNING: Unknown HK2 failure detected:
MultiException stack 1 of 2
java.lang.NullPointerException
    at com.sun.jersey.core.provider.jaxb.AbstractJAXBProvider.setConfiguration(AbstractJAXBProvider.java:113)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    ....
    MultiException stack 2 of 2
java.lang.IllegalStateException: Unable to perform operation: method inject on com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$App
    at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:395)
    ....
    MultiException stack 3 of 3
java.lang.IllegalStateException: Unable to perform operation: create on org.glassfish.jersey.message.internal.MessageBodyFactory
    ...

我猜 DropWizard 正在使用另一个冲突的依赖项,那么我该如何处理呢?

提前致谢

【问题讨论】:

  • 从 java11 切换回 java8 为我修复了奇怪的错误(检测到未知 HK2 故障)。

标签: java maven-2 dropwizard jersey-client


【解决方案1】:

Dropwizard 0.8.x 使用 Jersey 2.x,这自然会与您的 Jersey 1.x 依赖项发生冲突。

我认为 jersey-client 可能没问题,只要您没有 dropwizard-client 依赖项,但 jersey-core 会在很多方面与 dropwizard 的球衣发生冲突,我认为您无法修复。而且我认为您无论如何都不需要jersey-core 来发送电子邮件。

如果您确实需要球衣,请尝试使用 Jersey 2.16,而不是 dropwizard 正在使用的版本。

【讨论】:

  • 好吧,我想我刚刚意识到一些痛苦的事情......电子邮件的代码是为(旧的?)泽西客户端编写的,而新泽西在 glassfish 依赖项中 - 看起来完全不同。否则我上面的那个只能升级到 1.19 版
  • 我有同样的问题,如果你的父 maven 项目是 dropwizard,你可以在你的依赖项中省略 jersey 版本,一切都很好。我的父母不是 dropwizard,所以 mvn dependency:tree 将显示所需的球衣版本(对于 DW 0.9.3 it.s 2.22.1)
【解决方案2】:

我遇到了同样的问题(我使用的是 DW 0.9.2,并且对 DW 0.7.2 和 Jersey 1.x 都有不想要的传递依赖)

解决方案非常简单:在为您带来这两个库系列的依赖项中,声明一个exclusion,它应该可以工作,例如,我在 Gradle 中就是这样做的:

compile("com.example.culprit:culprit:1.2.3") {
    exclude group: 'io.dropwizard'
    exclude group: 'com.sun.jersey'
}

Maven 的语法非常相似(实际上更冗长;-))

希望这对某人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-24
    • 2015-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多