本文介绍了如何正确使用Jackson Mixin注释来实例化第三方类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a third party library class (from Apache Axis) that I want to serialize by 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 字段:

I have my entity class containing a NonNegativeInteger field that I want to serialize by JSON:

public class TestEntity {

    private NonNegativeInteger number;

    public NonNegativeInteger getNumber() {
        return number;
    }

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

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

When I serialize the above object by Jackson JSON, I got the following error:

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的构造函数:

Then I looked at the POST request entity, it is actually {"number" : 10} as serialized by Jackson. But since NonNegativeInteger doesn't have a constructor taking a single-int, Jackson can't instantiate a NonNegativeInteger object. So I followed someone's suggestions to add a Mixin class for NonNegativeInteger so that it will have a constructor with int as arg:

public abstract class NonNegativeIntegerMixin extends NonNegativeInteger {

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

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

Then I registered it in my JSON configuration class:

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

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

But it doesn't help, it still reported the same error. I tried manually writing the JSON request body to be {"number": "10"} then it worked well. But my client side used Jackson to serialize NonNegativeInteger. Jackson automatically converts to {"number": 10} without quotes. How can I fix this error?

编辑

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

The NonNegativeInteger class doesn't have any class field (except a constant zero field). The number key is from my TestEntity class. So I even if I add @JsonProperty annotation in my NonNegativeIntegerMixin mixin class, Jackson JSON will not instantiate a NonNegativeInteger with an int type arg. Therefore, I still got the same error.

推荐答案

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

Your mix-in annotation seems correct to me. So this could be a bug; so maybe file bug at:

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

One possible reason for problems is that class is extending BigInteger, which has existing deserializer. This could result in annotations being ignored, wrt how instances are deserialized.

这篇关于如何正确使用Jackson Mixin注释来实例化第三方类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 09:24