xml - Jackson: using builder with nondefault constructor -
i have following class:
@jsondeserialize(builder = transaction.builder.class) public final class transaction { @jacksonxmltext private final transactiontype transactiontype; @jacksonxmlproperty(isattribute = true) private final boolean transactionallowed; private transaction(builder builder) { transactiontype = builder.transactiontype; transactionallowed = builder.transactionallowed; } public static final class builder { private final transactiontype transactiontype; private boolean transactionallowed; public builder(transactiontype transactiontype) { this.transactiontype = transactiontype; } public builder withtransactionallowed() { transactionallowed = true; return this; } public transaction build() { return new transaction(this); } } }
transactiontype
simple enum:
public enum transactiontype { pu, cv; }
when create new transaction instance , serialize using jackson mapper following xml:
<transaction transactionallowed="true">pu</transaction>
the problem cannot deserialize it. following exception:
com.fasterxml.jackson.databind.jsonmappingexception: no suitable constructor found type [simple type, class transaction$builder]
if put @jsoncreator on builder constructor this:
@jsoncreator public builder(transactiontype transactiontype) { this.transactiontype = transactiontype; }
i following exception:
com.fasterxml.jackson.databind.exc.invalidformatexception: can not construct instance of transaction string value 'transactionallowed': value not 1 of declared enum instance names: [pu, cv]
if put @jsonproperty on constructor parameter this:
@jsoncreator public builder(@jsonproperty transactiontype transactiontype) { this.transactiontype = transactiontype; }
i error:
com.fasterxml.jackson.databind.jsonmappingexception: not find creator property name '' (in class transaction$builder)
any ideas how around this?
after made work writing 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); } }
notice value of @jsonproperty not empty, can value except empty string. don't believe best solution ever works. yet won't accept own answer there better solution.
Comments
Post a Comment