用Jackson反序列化受保护的构造函数

时间:2012-04-25 14:37:06

标签: json serialization jackson

您好我正在尝试使用jackson序列化和反序列化带有受保护构造函数的类(SimpleExpression)。当我使用gson时,我没有任何问题,但看起来jackson无法处理受保护的构造函数。我尝试使用mixin-annotations但没有奏效。序列化工作没有任何问题。杰克逊抛出:

com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type

有任何帮助吗? 我的代码:

private static SimpleExpression create(String json) throws JsonParseException, JsonMappingException, IOException
{
    ObjectMapper mapper = new ObjectMapper().setVisibility(PropertyAccessor.ALL, Visibility.ANY);
    SimpleExpression sp = null;
    sp = mapper.readValue(json, SimpleExpression.class);
    return sp;
}

SimpleExpression类,我省略了getter和setter。

public class SimpleExpression implements Criterion
{
private static final long serialVersionUID = 1L;

private final String propertyName;

private final Object value;

private boolean ignoreCase;

private final String op;

private final String type;

protected SimpleExpression(String propertyName, Object value, String op)
{
    this.propertyName = propertyName;
    this.value = value;
    this.op = op;
    this.type = value.getClass().getName();
}

protected SimpleExpression(String propertyName, Object value, String op, boolean ignoreCase)
{
    this.propertyName = propertyName;
    this.value = value;
    this.ignoreCase = ignoreCase;
    this.op = op;
    this.type = value.getClass().getName();
}
}

2 个答案:

答案 0 :(得分:3)

受保护的部分应该没有问题(它们被发现没问题)但是构造函数可能需要参数。要指示要使用的非默认构造函数,请使用@JsonCreator;但除此之外,它取决于使用何种构造函数(或静态工厂方法)。

但要了解细节,需要进行类定义。另一种可能性是你试图处理非静态内部类。

答案 1 :(得分:0)

如果any1来到线程。 我遇到了同样的问题。 我只是添加一个构造函数

protected SimpleExpression(){}

它工作正常