英文:
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 = "accounts")
@JacksonXmlProperty(localName = "account")
private List<Account> accounts = Lists.newArrayList();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论