如何在JAXB / JSON中命名复杂对象?

时间:2015-08-18 14:06:24

标签: java json jaxb

@XmlRootElement(name = "test")
public class MyDTO {
    @XmlElement(name = "test2)
    private MyObject meta;
}

结果:

{meta:{...}}

问题:

  1. 我想要一些名为“test”的“外部”标签
  2. 为什么meta的@XmlElement(name“属性不起作用?

1 个答案:

答案 0 :(得分:2)

我的第一篇文章!

确实,你可以命名你的"外部"标记@XmlRootElement。如果你需要另一个外部标签,我不知道如何实现这一点。

您的第二个问题可能是因为放置@XmlElement的位置。我把它放在我的getter方法上,它在我之前工作得很好。

对于JSON输出我使用了jersey-json-1.18。 以下工作也适用于您可以定义的其他复杂类型,而不是" String meta"。

这是我能够产生的输出:

作为JSON

{"myId":"id1","myMeta":"text1"}

作为XML

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<mytupel>
    <myId>id1</myId>
    <myMeta>text1</myMeta>
</mytupel>

这是我的目标:

@XmlRootElement(name = "mytupel")
public class Tupel {
    // @XmlElement(name = ) does not work here - defined it on the getter method
    private String id;

    // @XmlElement(name = ) does not work here - defined it on the getter method
    private String meta;

    /**
     * Needed for JAXB
     */
    public Tupel() {
    }

    /**
     * For Test purpose...
     */
    public Tupel(String id, String text) {
        super();
        this.id = id;
        this.meta = text;
    }

    @XmlElement(name = "myId")
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @XmlElement(name = "myMeta")
    public String getMeta() {
        return meta;
    }

    public void setMeta(String meta) {
        this.meta = meta;
    }

    /**
     * For Test purpose...
     */
    @Override
    public String toString() {
        return id + ": " + meta;
    }
}

这是我的小类来生成输出XML文件......

public class Main {

    private static final String TUPEL_1_XML = "./tupel1.xml";
    private static final String TUPEL_2_XML = "./tupel2.xml";

    public static void main(String[] args) throws JAXBException, FileNotFoundException {
        // init JAXB context/Marhsaller stuff
        JAXBContext context = JAXBContext.newInstance(Tupel.class);
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        Unmarshaller unmarshaller = context.createUnmarshaller();

        // create some Datatypes
        Tupel data1 = new Tupel("id1", "text1");
        Tupel data2 = new Tupel("id2", "42");

        // produce output
        marshaller.marshal(data1, new File(TUPEL_1_XML));
        marshaller.marshal(data2, new File(TUPEL_2_XML));

        // read from produced output
        Tupel data1FromXml = (Tupel) unmarshaller.unmarshal(new FileReader(TUPEL_1_XML));
        Tupel data2FromXml = (Tupel) unmarshaller.unmarshal(new FileReader(TUPEL_2_XML));

        System.out.println(data1FromXml.toString());
        System.out.println(data2FromXml.toString());

        System.out.println(marshalToJson(data1FromXml));
        System.out.println(marshalToJson(data2FromXml));
    }

    public static String marshalToJson(Object o) throws JAXBException {
        StringWriter writer = new StringWriter();
        JAXBContext context = JSONJAXBContext.newInstance(o.getClass());

        Marshaller m = context.createMarshaller();
        JSONMarshaller marshaller = JSONJAXBContext.getJSONMarshaller(m, context);
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshallToJSON(o, writer);
        return writer.toString();
    }

}

希望这能回答你的问题! 干杯 最大