Unable to upgrade to Jackson 2.1.4, Jersey ignorin

2020-04-07 19:24发布

问题:

The Short Version:

I am using Jersey 1.17 and Jackson 2.1.4, but Jersey is ignoring every single Jackson-annotation that I use. Why!? Has anybody encountered the same problem?

The Long Version:

I have a RESTful Web Service that was using Jersey 1.17 and Jackson 1.9.2 and I was using the @JsonManagedReference and @JsonBackReference annotations to solve the problem with bidirectional relationships and everything was working fine.

The other day I saw the 2.1.4 version of Jackson that included the @JsonIdentityInfo annotation which seemed like a great solution for my problems so I changed the jar files to Jackson 2.1.4. However, the promising @JsonIdentityInfo annotation was not working and Jersey was ignoring it completely, and after a bit of debugging and changing the code, I found out that even the previous @JsonManagedReference and @JsonBackReference annotations are being ignored by Jersey. So it seems that Jersey is completely ignoring the Jackson 2.1.4 annotations. Why!? Has anybody encountered the same problem?

回答1:

As stated in http://wiki.fasterxml.com/JacksonAnnotations:

Important note: Jackson 1.x and 2.x annotations live in different Java and Maven packages: see Jackson 2.0 for full explanation:

  • 1.x annotations are in Java package org.codehaus.jackson.annotate, in Jackson core jar
  • 2.x annotations are in Java package com.fasterxml.jackson.annotation, in "jackson-databind" jar.

So could be an "import-related" problem. Another reason (as already stated by @HiJon89) is to use the right JacksonJsonProvider version. For version 2.x it's automatically registered while inserting the "jackson-jaxrs-json-provider" combined with "jersey-core".

My currently working update to jackson 2.1.4 (from a 1.9.2 jersey required dependency) was:

  • remove jersey-json dependency
  • add jersey-core and jackson-jaxrs-json-provider

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-core</artifactId>
        <version>${jersey.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>${jackson-jaxrs-json-provider.version}</version>
    </dependency>
    

The latter jar has a file "META-INF/services/javax.ws.rs.ext.MessageBodyWriter" that tell jersey what class act as a provider.

So I've no other advise than to double-check that you don't have the old jars anymore in your classpath.



回答2:

In my case jackson annotations were ignored because of proguard