为什么在序列化时Jackson使用了错误的元素名称?

huangapple go评论127阅读模式
英文:

Why is Jackson using the wrong element name when serializing?

问题

我有一个对象,我想让Jackson序列化成这样...

    <AccountsResponse>
        <accounts>
            <account/>
            <account>
                <userId>user</userId>
                ...
            </account>
        </accounts>
    </AccountsResponse>

为了尝试这个,我创建了以下的类...

    @Getter
    @Setter
    @ToString
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    public class Payload {
        @JacksonXmlProperty(localName = "errormessage")
        private String errorMessage;
    }

    @Getter
    @Setter
    @ToString
    public class AccountsResponse extends Payload{
        @JsonIgnore
        private static Logger LOGGER = LogManager.getLogger(AccountsResponse.class);

        @JacksonXmlProperty(localName = "accounts")
        private List<Account> accounts = Lists.newArrayList();
        public static AccountsResponse mapFromResultSet(ResultSet rs)
                throws SQLException
        {
            AccountsResponse response = new AccountsResponse();
            do {
                Account acct = Account.mapFromResultSet(rs);
                response.getAccounts().add(acct);
            } while (rs.next());
            return response;
        }
        public String toXml() throws JsonProcessingException {
            ObjectMapper mapper = new XmlMapper();
            return mapper.writeValueAsString(this);
        }
    }

    @Getter
    @Setter
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public class Account extends ResultSetParser{
        ...
    }

但是当我序列化时,我得到...

    <AccountsResponse>
        <accounts>
            <accounts/>
            <accounts>
                <userId>user</userId>
                ...
            </accounts>
        </accounts>
    </AccountsResponse>

正如你所看到的,问题在于子标签应该是 `account`,但实际上是 `accounts`。我尝试了一些本地名称的修改,但是找不到正确的组合。我做错了什么?
英文:

I have a Object that I would like Jackson to serialize like this...

<AccountsResponse>
    <accounts>
        <account/>
        <account>
            <userId>user</userId>
            ...
        </account>
    </accounts>
</AccountsResponse>

To try this I create the following class...

@Getter
@Setter
@ToString
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class Payload {
    @JacksonXmlProperty(localName = "errormessage")
    private String errorMessage;
}


@Getter
@Setter
@ToString
public class AccountsResponse extends Payload{
    @JsonIgnore
    private static Logger LOGGER = LogManager.getLogger(AccountsResponse.class);

    @JacksonXmlProperty(localName = "accounts")
    private List<Account> accounts = Lists.newArrayList();
    public static AccountsResponse mapFromResultSet(ResultSet rs)
            throws SQLException
    {
        AccountsResponse response = new AccountsResponse();
        do {
            Account acct = Account.mapFromResultSet(rs);
            response.getAccounts().add(acct);
        } while (rs.next());
        return response;
    }
    public String toXml() throws JsonProcessingException {
        ObjectMapper mapper = new XmlMapper();
        return mapper.writeValueAsString(this);
    }
}

@Getter
@Setter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Account extends ResultSetParser{
    ...
}

But when I serialize I get...

<AccountsResponse>
    <accounts>
        <accounts/>
        <accounts>
            <userId>user</userId>
            ...
        </accounts>
    </accounts>
</AccountsResponse>

As you can see the problem here is the child tags should be account but in fact are accounts. I tried hacking around with the localname but can't find the right mixture of VooDoo. What am I doing wrong?

答案1

得分: 1

我会在AccountsResponse中更改帐户列表的注解:

public class AccountsResponse extends Payload{

    @JacksonXmlElementWrapper(localName = "accounts")
    @JacksonXmlProperty(localName = "account")
    private List<Account> accounts = Lists.newArrayList();

}
英文:

I would change annotations on account list in AccountsResponse:

public class AccountsResponse extends Payload{

    @JacksonXmlElementWrapper(localName = &quot;accounts&quot;)
    @JacksonXmlProperty(localName = &quot;account&quot;)
    private List&lt;Account&gt; accounts = Lists.newArrayList();

}

huangapple
  • 本文由 发表于 2020年10月8日 03:56:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/64251432.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定