[英]How to serialize an object with xml string as field to xml and set property name for this field (Jackson Object Mapper)?
I'm trying to serialize an object to xml with Object Mapper. 我正在尝试使用Object Mapper将对象序列化为xml。 An object's field is a xml string itself.
对象的字段本身就是xml字符串。 I use @JsonRawValue so Jackson won't escape xml charactets, like
<
or />
. 我使用@JsonRawValue,以便Jackson不会转义xml特征,例如
<
或/>
。 But, with @JsonRawValue Jackson ignores @JacksonXmlProperty annotation, and writes the string directly, omitting property name. 但是,通过@ JsonRawValue,Jackson会忽略@JacksonXmlProperty批注,并直接写入字符串,而忽略属性名。
So this code: 所以这段代码:
public class Example {
@JsonRawValue
@JacksonXmlProperty(localName = "SOME_NAME")
private String xml = "<xmlExample>123</xmlExample>";
}
produces: 生产:
<Example> <xmlExample>123</xmlExample> </Example>
When i want: 当我想要时:
<Example>
<SOME_NAME> <xmlExample>123</xmlExample> </SOME_NAME>
</Example>
So the proplem is that @JacksonXmlProperty doesn't work with @JsonRawValue. 因此,问题是@JacksonXmlProperty不适用于@JsonRawValue。 And i don't know how to get rid of @JsonRawValue, because without this annotation, Jackson escapes some xml characters.
而且我不知道如何摆脱@JsonRawValue,因为如果没有此批注,Jackson会转义一些xml字符。
UPDATE: Output generation code: 更新:输出生成代码:
Example example = new Example();
String s = new XmlMapper().vriteValueAsString(example);
Try 尝试
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
public static void main(String[] args) {
try {
Example example = new Example();
XmlMapper xmlMapper = new XmlMapper();
xmlMapper.enable(SerializationFeature.INDENT_OUTPUT);
String xmlString = xmlMapper.writeValueAsString(example);
System.out.println(xmlString);
} catch (Exception e) {
e.printStackTrace();
}
}
Output: 输出:
<Example><SOME_NAME><xmlExample>123</xmlExample></SOME_NAME></Example>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.