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

2024-02-29

我有一个第三方库类(来自 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);
        }
    }
}

我的实体类包含NonNegativeInteger我想通过 JSON 序列化的字段:

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没有采用单整型的构造函数,Jackson 无法实例化NonNegativeInteger目的。所以我按照某人的建议添加了 Mixin 类NonNegativeInteger这样它将有一个以 int 作为参数的构造函数:

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}不带引号。我该如何修复这个错误?

Edit:

The NonNegativeInteger类没有任何类字段(除了常量zero场地)。这number钥匙来自我的TestEntity班级。所以我即使添加@JsonProperty我的注释中NonNegativeIntegerMixinmixin 类,Jackson JSON 不会实例化NonNegativeInteger带有 int 类型 arg。因此,我仍然遇到同样的错误。


您的混合注释对我来说似乎是正确的。所以这可能是一个错误;所以也许可以在以下位置提交错误:

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何正确使用Jackson Mixin注解实例化第三方类? 的相关文章

随机推荐