问题描述
我正在尝试使用 Jackson 将对象序列化和反序列化为 XML,但是我在这样做时遇到了麻烦......
我的对象:
- 是一个只有 getter 和构造函数的不可变类
- 它包含子对象消息列表
- 我想将这个包含在元素
<messages>
中的列表和每条消息序列化为一个<message>
元素 - 据我所知,我必须在 getter 上放置@JsonProperty("message") @JacksonXmlElementWrapper(localName = "messages")
批注,因为当我放置它时在构造函数上,它不会产生所需的输出(两者都是相同的<message(s)>
)
<response id="response-id">
<messages>
<message code="a"/>
<message code="b"/>
</messages>
</response>
- 我需要反序列化这个结构,但是由于它是具有最终变量的不可变对象,我必须通过构造函数反序列化它。
代码片段:
public class Playground {
public static void main(String[] args) throws JsonProcessingException {
Response response = new Response("response-id",List.of(new Message("a"),new Message("b")));
XmlMapper xmlObjectMapper = new XmlMapper(new XmlFactory());
xmlObjectMapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS,true);
final String value = xmlObjectMapper.writeValueAsstring(response);
System.out.println(value);
assert ("<response id=\"response-id\"><messages><message code=\"a\"/><message "
+ "code=\"b\"/></messages></response>")
.equals(value) : "Desired format does not match!";
final Response deserializedValue = xmlObjectMapper.readValue(value,Response.class);
//final String deserialized = xmlObjectMapper.writeValueAsstring(deserializedValue);
//assert value.equals(deserialized) : "Does not match";
}
}
@JsonInclude(Include.NON_EMPTY)
@JacksonXmlRootElement(localName = "response")
@JsonPropertyOrder({"id","messages"})
class Response {
private final String id;
private final List<Message> messages;
@JsonCreator
public Response(
@JacksonXmlProperty(localName = "id",isAttribute = true) final String id,@JsonProperty("message") final List<Message> messages) {
this.id = id;
this.messages = messages;
}
@JacksonXmlProperty(localName = "id",isAttribute = true)
public String getId() {
return id;
}
@JsonProperty("message")
@JacksonXmlElementWrapper(localName = "messages")
public List<Message> getMessages() {
return messages;
}
}
@JsonIgnoreProperties(ignoreUnkNown = true)
@JsonInclude(Include.NON_EMPTY)
class Message {
private final String code;
public Message(@JacksonXmlProperty(localName = "code",isAttribute = true) final String code) {
this.code = code;
}
@JacksonXmlProperty(localName = "code",isAttribute = true)
public String getCode() {
return code;
}
}
- 当我尝试在没有在构造函数中注释
messages
的情况下反序列化它时,我得到异常:Invalid type deFinition for type 'Response': Argument #1 of constructor [constructor for 'Response' (2 args),annotations: {interface JsonCreator=@JsonCreator(mode=DEFAULT)} has no property name (and is not Injectable): can not use as property-based Creator
- 当我添加
@JsonProperty("message")
注释时,出现异常:Invalid deFinition for property 'messages' (of type 'Response'): Could not find creator property with name 'messages' (kNown Creator properties: [id,message])
- 当我将其更改为
@JsonProperty("messages")
(复数)时,我得到:Duplicate property 'messages' for [simple type,class Response]
- 当我同时添加
@JsonProperty("message") @JacksonXmlElementWrapper(localName = "messages")
时,它会产生异常:Invalid deFinition for property 'messages' (of type 'Response'): Could not find creator property with name 'messages' (kNown Creator properties: [id,message])
。 (这两个注释中的任何消息/消息组合都不起作用)
我做错了什么?我需要使用哪些注释来通过反序列化检索所需的 XML 输出?
解决方法
对于未来的谷歌员工:
这似乎与 jackson-dataformat-xml 问题有关: https://github.com/FasterXML/jackson-dataformat-xml/issues/187
还发布了解释和解决方法。