为什么我不能将这个简单的对象映射到 Java/Jersey 中的 XML 文本?

我有一个用 Java 中的 Jersey 创建的 REST API。对于一个请求,我想以 JSON 格式返回一对坐标元组的列表。为此,我有一个类,它是一个ArrayList、一个Tuple2类和一个Coords类的包装器。我使用它javax.xml.bind.annotations来自动生成我的类的 XML/JSON。


但由于我不明白我的Coords类不能映射到 XML 的原因。


我尝试过不同类型的属性(Integers而不是int),在@XmlAttribute不同的位置(在属性之前和吸气剂之前)和不同的属性XmlAccessType(PROPERTY而不是NONE),但结果是相同的。


这是我的坐标类:


package model;


import javax.xml.bind.annotation.XmlAccessorType;

import javax.xml.bind.annotation.XmlRootElement;

import javax.xml.bind.annotation.XmlAttribute;

import static javax.xml.bind.annotation.XmlAccessType.NONE;


@XmlRootElement

@XmlAccessorType(NONE)

public class Coords {

    @XmlAttribute private int x;

    @XmlAttribute private int y;


    public Coords(final int x, final int y) {

        this.x = x;

        this.y = y;

    }


    public Coords() {

        this.x = 0;

        this.y = 0;

    }


    public int getX() {

        return this.x;

    }


    public int getY() {

        return this.y;

    }

}


这是它在我的 Tuple2 中的呈现方式


package model;


import javax.xml.bind.annotation.XmlAccessorType;

import javax.xml.bind.annotation.XmlRootElement;

import javax.xml.bind.annotation.XmlAttribute;

import static javax.xml.bind.annotation.XmlAccessType.NONE;


@XmlRootElement

@XmlAccessorType(NONE)

public class Tuple2 {

    private Coords c1;

    private Coords c2;

// ...

    @XmlAttribute 

    public Coords getFirst() {

        return this.c1;

    }


    @XmlAttribute 

    public Coords getSecond() {

        return this.c2;

    }

// ...

}


慕少森
浏览 84回答 1
1回答

慕容708150

您的问题是由于滥用 xml 注释造成的。您可以Tuple2通过使用 注释将 a 定义为 xml 根元素@XmlRootElement,并通过使用 注释 get 方法将其字段定义为 xml 属性@XmlAttribute。翻译过来就是:<tuple2 first="first_attributes_vale" second="second_attributes_value" />现在,两个字段都是 类型,通过使用 注释该类Coords来将其声明为另一个 xml 元素,并且其字段为 xml 属性。当序列化为 xml 时,它将是:Coords@XmlRootElementCoords<coords x="value" y="value" />序列化时会出现问题Tuple2。它的字段应该是 xml 属性,但实际上Coords是另一个 xml 元素。Xml 属性不能包含嵌套元素,只能包含值。解决方案根据您的需要,您可以通过两种不同的方式解决这个问题。不过,我不推荐第二种方法,因为它很奇怪(即使它有效)并且会在客户端产生额外的工作(请参阅下面的解释)。第一种方法使用注释来注释getFirst()和getSecond()方法@XmlElement。package model;import static javax.xml.bind.annotation.XmlAccessType.NONE;import javax.xml.bind.annotation.XmlAccessorType;import javax.xml.bind.annotation.XmlElement;import javax.xml.bind.annotation.XmlRootElement;@XmlRootElement@XmlAccessorType(NONE)public class Tuple2 {&nbsp; &nbsp; private Coords c1;&nbsp; &nbsp; private Coords c2;&nbsp; &nbsp; public Tuple2(Coords c1, Coords c2) {&nbsp; &nbsp; &nbsp; &nbsp; this.c1 = c1;&nbsp; &nbsp; &nbsp; &nbsp; this.c2 = c2;&nbsp; &nbsp; }&nbsp; &nbsp; public Tuple2() {&nbsp; &nbsp; &nbsp; &nbsp; c1 = new Coords(0, 0);&nbsp; &nbsp; &nbsp; &nbsp; c2 = new Coords(0, 0);&nbsp; &nbsp; }&nbsp; &nbsp; @XmlElement&nbsp; &nbsp; public Coords getFirst() {&nbsp; &nbsp; &nbsp; &nbsp; return this.c1;&nbsp; &nbsp; }&nbsp; &nbsp; @XmlElement&nbsp; &nbsp; public Coords getSecond() {&nbsp; &nbsp; &nbsp; &nbsp; return this.c2;&nbsp; &nbsp; }}这将产生如下所示的结果:<tuple2>&nbsp; &nbsp; <first x="2" y="4"/>&nbsp; &nbsp; <second x="12" y="12"/></tuple2>第二种方法这是解决这个问题的奇怪方法。它可以工作,但会在客户端产生额外的工作,因为 的值Coords被编码为字符串值,并且需要在接收端进行解析。getFirst()将和方法的返回类型更改getSecond()为String并覆盖toString()的方法Coords。package model;import static javax.xml.bind.annotation.XmlAccessType.NONE;import javax.xml.bind.annotation.XmlAccessorType;import javax.xml.bind.annotation.XmlAttribute;import javax.xml.bind.annotation.XmlRootElement;@XmlRootElement@XmlAccessorType(NONE)public class Tuple2 {&nbsp; &nbsp; private Coords c1;&nbsp; &nbsp; private Coords c2;&nbsp; &nbsp; public Tuple2(Coords c1, Coords c2) {&nbsp; &nbsp; &nbsp; &nbsp; this.c1 = c1;&nbsp; &nbsp; &nbsp; &nbsp; this.c2 = c2;&nbsp; &nbsp; }&nbsp; &nbsp; public Tuple2() {&nbsp; &nbsp; &nbsp; &nbsp; c1 = new Coords(0, 0);&nbsp; &nbsp; &nbsp; &nbsp; c2 = new Coords(0, 0);&nbsp; &nbsp; }&nbsp; &nbsp; @XmlAttribute&nbsp; &nbsp; public String getFirst() {&nbsp; &nbsp; &nbsp; &nbsp; return this.c1.toString();&nbsp; &nbsp; }&nbsp; &nbsp; @XmlAttribute&nbsp; &nbsp; public String getSecond() {&nbsp; &nbsp; &nbsp; &nbsp; return this.c2.toString();&nbsp; &nbsp; }}重写toString()以下方法Coords:package model;import static javax.xml.bind.annotation.XmlAccessType.NONE;import javax.xml.bind.annotation.XmlAccessorType;import javax.xml.bind.annotation.XmlAttribute;import javax.xml.bind.annotation.XmlRootElement;@XmlRootElement@XmlAccessorType(NONE)public class Coords {&nbsp; &nbsp; @XmlAttribute private int x;&nbsp; &nbsp; @XmlAttribute private int y;&nbsp; &nbsp; public Coords(final int x, final int y) {&nbsp; &nbsp; &nbsp; &nbsp; this.x = x;&nbsp; &nbsp; &nbsp; &nbsp; this.y = y;&nbsp; &nbsp; }&nbsp; &nbsp; public Coords() {&nbsp; &nbsp; &nbsp; &nbsp; this.x = 0;&nbsp; &nbsp; &nbsp; &nbsp; this.y = 0;&nbsp; &nbsp; }&nbsp; &nbsp; public int getX() {&nbsp; &nbsp; &nbsp; &nbsp; return x;&nbsp; &nbsp; }&nbsp; &nbsp; public int getY() {&nbsp; &nbsp; &nbsp; &nbsp; return y;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public String toString() {&nbsp; &nbsp; &nbsp; &nbsp; StringBuilder builder = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; builder.append("Coords [x=");&nbsp; &nbsp; &nbsp; &nbsp; builder.append(x);&nbsp; &nbsp; &nbsp; &nbsp; builder.append(", y=");&nbsp; &nbsp; &nbsp; &nbsp; builder.append(y);&nbsp; &nbsp; &nbsp; &nbsp; builder.append("]");&nbsp; &nbsp; &nbsp; &nbsp; return builder.toString();&nbsp; &nbsp; }}这将产生与此类似的结果:<tuple2 first="Coords [x=2, y=4]" second="Coords [x=12, y=12]"/>属性first和的值second将是任何方法返回toString()的值Coords。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java