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


结果:

{meta:{...}}


问题:


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

最佳答案

我的第一篇文章!

实际上,您可以使用@XmlRootElement来命名“外部”标签。如果您需要另一个外部标签,我不确定如何实现。

您的第二个担心可能是因为您放置了@XmlElement的位置。我将其放在吸气剂方法上,对我来说效果很好。

对于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();
    }

}


希望这能回答您的问题!
干杯
最高

09-05 13:13
查看更多