XML输出包含包装器"凭证"尽管我用defaultUseWrapper(false)排除了它。
UserDAO
public class UserDAO {
@JacksonXmlProperty(isAttribute = true)
private int id;
private Credentials credentials;
@JacksonXmlProperty(localName = "todos")
private TasksDAO tasks;
// getter, setter
}
凭证
public class Credentials {
@JacksonXmlProperty(localName = "name")
@JsonProperty("name")
private String username;
private String password;
// getter, setter
}
XmlWrapper配置
XmlMapper
.builder()
.defaultUseWrapper(false)
.addModule(new JavaTimeModule())
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.build()
XML输出<users>
<user id="1">
<credentials>
<password>123</password>
<name>Steve</name>
</credentials>
<todos/>
</user>
</users>
我的目标是使它与用户和密码一致。我应该如何删除包装器凭据?
所需输出
<users>
<user id="1">
<password>123</password>
<name>Steve</name>
<todos/>
</user>
</users>
Dirty Solution
<标题>UserDAOSerializer.java h1> UserDAO.java h1> Main2.java h1> Credentials.java h1>包org.example;
进口com.fasterxml.jackson.annotation.JsonProperty;
public class Credentials {
// @JacksonXmlProperty(localName = "name")
@JsonProperty("name")
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
<标题>运行结果<UserDAO id="1">
<password>123</password>
<name>Steve</name>
</UserDAO>
<标题>REF h1> ackson:为Map数据结构注册自定义XML序列化器标题>标题>@Bender回答结论
我知道这不是你想要的,这只是一个POC。
我认为你能应付别人。
我希望这个答案能解决你的问题。
标题>