How to use Jersey with a newer version of jackson

后端 未结 1 1221
无人共我
无人共我 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

    
        com.fasterxml.jackson.jaxrs
        jackson-jaxrs-base
    
    
        com.fasterxml.jackson.jaxrs
        jackson-jaxrs-json-provider
    
    
        com.fasterxml.jackson.core
        jackson-annotations
    
    

    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

    
        org.glassfish.jersey.media
        jersey-media-json-jackson
        2.23.1
        
            
                com.fasterxml.jackson.core
                jackson-annotations
            
            
                com.fasterxml.jackson.jaxrs
                jackson-jaxrs-base
            
        
    
    
        com.fasterxml.jackson.jaxrs
        jackson-jaxrs-json-provider
        2.7.0
    
    

    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)
提交回复
热议问题