如何正确使用Jackson Mixin注释来实例化第三方类?

时间:2015-03-04 15:11:32

标签: java json serialization jackson mixins

我有一个第三方库类(来自Apache Axis),我想通过Jackson JSON序列化:

public class NonNegativeInteger extends BigInteger {

    public NonNegativeInteger(byte[] val) {
        super(val);
        checkValidity();
    } // ctor

    public NonNegativeInteger(int signum, byte[] magnitude) {
        super(signum, magnitude);
        checkValidity();
    } // ctor

    public NonNegativeInteger(int bitLength, int certainty, Random rnd) {
        super(bitLength, certainty, rnd);
        checkValidity();
    } // ctor

    public NonNegativeInteger(int numBits, Random rnd) {
        super(numBits, rnd);
        checkValidity();
    } // ctor

    public NonNegativeInteger(String val) {
        super(val);
        checkValidity();
    }

    public NonNegativeInteger(String val, int radix) {
        super(val, radix);
        checkValidity();
    } // ctor

    /**
     * validate the value against the xsd definition
     */
    private BigInteger zero = new BigInteger("0");
    private void checkValidity() {
        if (compareTo(zero) < 0) {
            throw new NumberFormatException(
                    Messages.getMessage("badNonNegInt00")
                    + ":  " + this);
        }
    } // checkValidity

    /**
     * Work-around for http://developer.java.sun.com/developer/bugParade/bugs/4378370.html
     * @return BigIntegerRep
     * @throws ObjectStreamException
     */ 
    public Object writeReplace() throws ObjectStreamException {
        return new BigIntegerRep(toByteArray());
    }

    protected static class BigIntegerRep implements java.io.Serializable {
        private byte[] array;
        protected BigIntegerRep(byte[] array) {
            this.array = array;
        }
        protected Object readResolve() throws java.io.ObjectStreamException {
            return new NonNegativeInteger(array);
        }
    }
}

我的实体类包含一个我希望通过JSON序列化的NonNegativeInteger字段:

public class TestEntity {

    private NonNegativeInteger number;

    public NonNegativeInteger getNumber() {
        return number;
    }

    public void setNumber(NonNegativeInteger number) {
        this.number = number;
    }
}

当我通过Jackson JSON序列化上述对象时,我收到以下错误:

Can not instantiate value of type [simple type, class org.apache.axis.types.NonNegativeInteger] from Integral number; no single-int-arg constructor/factory method

然后我查看了POST请求实体,它实际上是由杰克逊序列化的{"number" : 10}。但由于NonNegativeInteger没有构造函数采用单个int,因此Jackson无法实例化NonNegativeInteger对象。所以我按照某人的建议为NonNegativeInteger添加了一个Mixin类,这样它就会有一个int为arg的构造函数:

public abstract class NonNegativeIntegerMixin extends NonNegativeInteger {

    @JsonCreator
    public NonNegativeIntegerMixin(int val) {
        super(String.valueOf(val));
    }
}

然后我在我的JSON配置类中注册了它:

 objectMapper.addMixInAnnotations(NonNegativeInteger.class, NonNegativeIntegerMixin.class);

但它没有帮助,它仍然报告了同样的错误。我尝试手动将JSON请求体写为{"number": "10"}然后它运行良好。但是我的客户端使用Jackson来序列化NonNegativeInteger。杰克逊自动转换为{"number": 10},没有引号。我该如何解决这个错误?

修改:

NonNegativeInteger类没有任何类字段(常量zero字段除外)。 number密钥来自我的TestEntity课程。因此,即使我在@JsonProperty mixin类中添加NonNegativeIntegerMixin注释,Jackson JSON也不会使用int类型arg实例化NonNegativeInteger。因此,我仍然遇到同样的错误。

1 个答案:

答案 0 :(得分:0)

您的混音注释对我来说似乎是正确的。所以这可能是一个错误;所以也许在以下文件中提出错误:

https://github.com/FasterXML/jackson-databind/issues/

问题的一个可能原因是类正在扩展BigInteger,它具有现有的解串器。这可能导致注释被忽略,以及实例的反序列化。