java 使用 dropwizard + jersey 客户端时如何避免依赖冲突

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29994462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:15:31  来源:igfitidea点击:

How to avoid dependency conflict when using dropwizard + jersey client

javamaven-2dropwizardjersey-client

提问by Manish Patel

I have a DropWizard REST API written and works. One of the resource endpoints actually writes an email, however as soon as I add the following dependencies DropWizard starts to fail on start up

我编写了一个 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>

The DropWizard dependency is:

DropWizard 依赖项是:

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

The error on startup is really long, summarised below

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

    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
    ...

I'm guessing DropWizard is using another conflicting dependency so how do I deal with this?

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

Thanks in advance

提前致谢

采纳答案by Natan

Dropwizard 0.8.x uses Jersey 2.x which naturally conflicts with your Jersey 1.x dependencies.

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

I think it might be fine with jersey-clientas long as you don't have dropwizard-clientdependency but jersey-corewill conflict at many points with dropwizard's jersey which I don't think you can fix. And also I don't think you'd need jersey-corefor sending email anyway.

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

If you do need jersey for this, try using Jersey 2.16 instead which is the version dropwizard is using.

如果您确实需要球衣,请尝试使用 Jersey 2.16 代替 dropwizard 使用的版本。

回答by Clint Eastwood

I had the same problem (I'm using DW 0.9.2 and have unwanted transitive dependencies of both DW 0.7.2 and Jersey 1.x)

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

The solution was pretty simple: in the dependency that's bringing you those two family of libraries, declare an exclusionand it should work, for instance, this is how I did it in Gradle:

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

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

The syntax for Maven is pretty similar (more verbose actually ;-) )

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

Hope this helps someone.

希望这可以帮助某人。