简体   繁体   English

如何将带有xml字符串作为字段的对象序列化为xml并为此字段设置属性名称(杰克逊对象映射器)?

[英]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.

相关问题 在Jackson对象映射器中自定义字段名称序列化 - Customizing Field Name Serialization in Jackson Object Mapper 带有杰克逊的xml序列化对象的基于类字段的根名称 - Root name based on class field for an xml serialized object with jackson 如何使用jackson将java对象序列化为xml属性? - How to serialize java object as xml attribute with jackson? 杰克逊Java到JSON对象映射器修改字段名称 - Jackson Java to JSON object mapper modifies field's name 如何指示杰克逊序列化对象内部的字段而不是自己的对象? - How to instruct Jackson to serialize a field inside an Object instead of the Object it self? 将XML属性映射到Jackson Xml Mapper中的指定标签(字段) - Map XML attribute to specified tag(field) in Jackson Xml Mapper 将 Jackson 字段序列化为 JSON 字符串(而不是对象)的注释 - Annotation to serialize Jackson field as a JSON string (instead of object) 使用Jackson XML Mapper,如何使用相同的本地名称序列化多个属性 - Using Jackson XML Mapper, how to serialize more than one properties using same local name 如何使用 Jackson dataformat.xml 序列化一个 LookAndFeel object in Z93F7489F444B? - How to use Jackson dataformat.xml to serialize a LookAndFeel object in java? 使用Jackson XML映射器将XML字符串反序列化为对象后,没有显示完整的项目列表 - Complete list of items are not showing up after deserializing XML String into object using Jackson XML mapper
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM