简体   繁体   English

如何使用jackson将java对象序列化为xml属性?

[英]How to serialize java object as xml attribute with jackson?

is there a way to serialize a java var (eg int) via jackson as an xml attribute? 有没有办法通过jackson序列化java var(例如int)作为xml属性? I can not find any spezific jackson or json annotation (@XmlAttribute @javax.xml.bind.annotation.XmlAttribute) to realize this. 我找不到任何特定的jackson或json注释(@XmlAttribute @ javax.xml.bind.annotation.XmlAttribute)来实现这一点。

eg 例如

public class Point {

    private int x, y, z;

    public Point(final int x, final int y, final int z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }

    @javax.xml.bind.annotation.XmlAttribute
    public int getX() {
        return x;
    }
    ...
}

What I want: 我想要的是:

<point x="100" y="100" z="100"/>

but all I got is: 但我得到的只是:

<point>
    <x>100</x>
    <y>100</y>
    <z>100</z>
</point>

Is there a way to get attributes instead of elements? 有没有办法获取属性而不是元素? Thanks for help! 感谢帮助!

Okay I found a solution. 好的,我找到了解决方案。

It wasn't necessary to register an AnnotaionIntrospector if you use jackson-dataformat-xml 如果使用jackson-dataformat-xml,则无需注册AnnotaionIntrospector

File file = new File("PointTest.xml");
XmlMapper xmlMapper = new XmlMapper();
xmlMapper.writeValue(file, new Point(100, 100, 100));

The missing TAG was 丢失的标签是

@JacksonXmlProperty(isAttribute=true) @JacksonXmlProperty(isAttribute =真)

so just change the getter to: 所以只需将吸气剂更改为:

@JacksonXmlProperty(isAttribute=true)
public int getX() {
    return x;
}

and it works fine. 它工作正常。 Just follow this how to: 只需按照以下方式:

https://github.com/FasterXML/jackson-dataformat-xml https://github.com/FasterXML/jackson-dataformat-xml

@JacksonXmlProperty allows specifying XML namespace and local name for a property; @JacksonXmlProperty允许为属性指定XML名称空间和本地名称; as well as whether property is to be written as an XML element or attribute. 以及是否将属性写为XML元素或属性。

Have you registered JaxbAnnotationIntrospector ? 你注册了JaxbAnnotationIntrospector吗?

ObjectMapper mapper = new ObjectMapper();
AnnotationIntrospector introspector = new JaxbAnnotationIntrospector();
// make deserializer use JAXB annotations (only)
mapper.getDeserializationConfig().setAnnotationIntrospector(introspector);
// make serializer use JAXB annotations (only)
mapper.getSerializationConfig().setAnnotationIntrospector(introspector);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Jackson dataformat.xml 序列化一个 LookAndFeel object in Z93F7489F444B? - How to use Jackson dataformat.xml to serialize a LookAndFeel object in java? 如何使用杰克逊将此序列化为xml? - How to serialize this to xml using Jackson? 如何在Java中使用常量属性序列化xml? - How serialize xml with constant attribute in java? Java+Jackson+XML:将一个java对象属性序列化为同名的XML元素 - Java+Jackson+XML: serialize a java object properties as XML elements with same names Java杰克逊| 如何序列化指定具体接口的对象 - Java Jackson | How to serialize object specifying concrete interface 如何使用 Jackson 将 Java8 LocalDateTime 序列化为 json 作为日历对象 - How to serialize Java8 LocalDateTime to json as calendar object using Jackson 如何将带有xml字符串作为字段的对象序列化为xml并为此字段设置属性名称(杰克逊对象映射器)? - How to serialize an object with xml string as field to xml and set property name for this field (Jackson Object Mapper)? 如何使用Jackson和Hibernate序列化和反序列化Set属性 - How to serialize and deserialize a Set attribute with Jackson and Hibernate 使用Jackson XML映射器将Java List序列化为XML - Serialize Java List to XML using Jackson XML mapper 如何使用 Jackson 将对象序列化为 json,包括 ArrayList - How to serialize object to json with Jackson, including ArrayList
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM