嗨,我正在尝试使用 jackson 来序列化和反序列化具有 protected 构造函数的类(SimpleExpression)。当我使用 gson 时,我没有任何问题,但 jackson 似乎无法处理 protected 构造函数。我尝试使用 mixin-annotations 但没有成功。序列化工作没有任何问题。 jackson throw :
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();
}
}
最佳答案
protected 部分应该不会有问题(它们被发现没问题),但构造函数可能需要参数。要指示要使用的非默认构造函数,请使用 @JsonCreator
;但除此之外,这取决于使用哪种构造函数(或静态工厂方法)。
但要了解详细信息,需要类定义。另一种可能性是您正在尝试处理非静态内部类。
关于json - 使用 Jackson 反序列化 protected 构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10317955/