Xml Jackson:使用带非默认构造函数的生成器
我有以下课程:Xml Jackson:使用带非默认构造函数的生成器,xml,jackson,deserialization,builder,Xml,Jackson,Deserialization,Builder,我有以下课程: TransactionType是一个简单的枚举: 当我创建一个新的事务实例并使用Jackson mapper对其进行序列化时,我得到以下xml: <transaction transactionAllowed="true">PU</transaction> 如果我将@JsonCreator放在生成器构造函数上,如下所示: 我得到以下例外情况: com.fasterxml.jackson.databind.JsonMappingException:
TransactionType
是一个简单的枚举:
当我创建一个新的事务实例并使用Jackson mapper对其进行序列化时,我得到以下xml:
<transaction transactionAllowed="true">PU</transaction>
如果我将@JsonCreator放在生成器构造函数上,如下所示:
我得到以下例外情况:
com.fasterxml.jackson.databind.JsonMappingException:
No suitable constructor found for type [simple type, class Transaction$Builder]
com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not construct
instance of Transaction from String value 'transactionAllowed': value not one of
declared Enum instance names: [PU, CV]
如果我将@JsonProperty放在构造函数参数上,如下所示:
我得到另一个错误:
com.fasterxml.jackson.databind.JsonMappingException: Could not find creator
property with name '' (in class Transaction$Builder)
有什么办法解决这个问题吗?毕竟我写了这个生成器: 请注意,@JsonProperty的值不是空的,它实际上可以是除空字符串之外的任何值。我不相信这是有史以来最好的解决方案,但它是有效的。但是我不会接受我自己的答案,因为可能有更好的解决方案。对于“@JsonProperty”,您应该填写属性的名称,在您的案例中应该是“transactionType”。如果将“@JsonProperty”留空,则在生成器构造函数中有多个参数,则会引发Json异常。
@JsonCreator
public Builder(TransactionType transactionType) {
this.transactionType = transactionType;
}
com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not construct
instance of Transaction from String value 'transactionAllowed': value not one of
declared Enum instance names: [PU, CV]
@JsonCreator
public Builder(@JsonProperty TransactionType transactionType) {
this.transactionType = transactionType;
}
com.fasterxml.jackson.databind.JsonMappingException: Could not find creator
property with name '' (in class Transaction$Builder)
public static final class Builder {
@JacksonXmlText
private final TransactionType transactionType;
private boolean transactionAllowed;
@JsonCreator
public Builder(@JsonProperty(" ") TransactionType transactionType) {
this.transactionType = transactionType;
}
public Builder withTransactionAllowed() {
transactionAllowed = true;
return this;
}
public Transaction build() {
return new Transaction(this);
}
}