How to use Jersey with a newer version of jackson

后端 未结 1 1210
无人共我
无人共我 2021-01-13 19:53

I am using Jersey (2.23.1) with jersey-media-json-jackson. But that is linked against Jackson 2.5.4. But I need to use Jackson 2.6.0 (or a newer version).

相关标签:
1条回答
  • 2021-01-13 20:23

    When you want to override a version, you need to look at the pom.xml of the module. You see in the link that there are three Jackson dependencies explicitly declared

    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-base</artifactId>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
    </dependency>
    

    When you want to use a new version, you have a couple options to go about it. You can explicitly declare all of them in your pom.xml file with the version you want, or you can exclude all the dependencies, and just explicitly declare the main one.

    For the option of explicitly declaring, you can just all the three above, specifying the version you want. Explicit declarations take precedence over transitive dependencies. So the explicitly declared ones will always be the ones pulled in, rather than the ones pulled in by Jersey.

    For the option of excluding, you can exclude them from from the jersey-media-json-jackson, and just add the main one the pulls everything else in. In this case jackson-jaxrs-json-provider pulls the other two in, so you really only need to declare that one

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.23.1</version>
        <exclusions>
            <exclusion>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-annotations</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.fasterxml.jackson.jaxrs</groupId>
                <artifactId>jackson-jaxrs-base</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>2.7.0</version>
    </dependency>
    

    Either one of these ways should work.

    What happens when you don't declare all dependencies when changing version, is that an older version jar can be pulled in transitively, and one of the newer versions tries to use some classes in that older version jar, and you end up with these kind of errors.

    0 讨论(0)
提交回复
热议问题