Jackson ignoring annotation on dependency class

时间:2016-04-15 15:10:11

标签: spring jackson

I'm trying to serialise a class that belongs to project B from project A using jackson. Since this class has some funny method names I needed to use MixIn annotation to make it serialisable from project A. That worked well, but later I was asked to make this change directly into project B, since we want many other projects to be able to serialise/deserialise this class.

I ended up putting annotation directly into the class, so my class became

public class Dog {

public Dog(@JsonProperty("breed") String colour) {
    this.colour = colour;
}
@JsonProperty("breed")
private String colour;
}

This is just an example and it shows what the class looks like. The point here is, when I serialise/deserialise the class inside project B the annotations are picked up and everything works as expected, whilst when I serialise/deserialise inside project A (that has project B as a dependency) the annotations are ignored. Does anybody know why this is happening?

2 个答案:

答案 0 :(得分:1)

首先要做的是升级罐子。 新的Maven依赖项是:

   <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.7.2</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.7.3</version>
    </dependency> 
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.7.2</version>
        <type>jar</type>
    </dependency>

您可以使用不同的版本进行注释,但是开销太大而且不会带来任何好处。

请参阅:

org.codehaus.jackson versus com.fasterxml.jackson.core

http://wiki.fasterxml.com/JacksonUpgradeFrom19To20

答案 1 :(得分:0)

问题出在杰克逊版本中。 项目B使用org.codehaus.jackson注释,而项目A使用com.fasterxml.jackson fasterxml.jackson忽略了codehaus注释。