为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕少森 2024-01-05 10:59:41
我有一个用 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;    }// ...}
查看完整描述

1 回答

?
慕容708150

TA贡献1831条经验 获得超4个赞

您的问题是由于滥用 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 {

    private Coords c1;

    private Coords c2;


    public Tuple2(Coords c1, Coords c2) {

        this.c1 = c1;

        this.c2 = c2;

    }


    public Tuple2() {

        c1 = new Coords(0, 0);

        c2 = new Coords(0, 0);

    }


    @XmlElement

    public Coords getFirst() {

        return this.c1;

    }


    @XmlElement

    public Coords getSecond() {

        return this.c2;

    }

}

这将产生如下所示的结果:


<tuple2>

    <first x="2" y="4"/>

    <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 {

    private Coords c1;

    private Coords c2;


    public Tuple2(Coords c1, Coords c2) {

        this.c1 = c1;

        this.c2 = c2;

    }


    public Tuple2() {

        c1 = new Coords(0, 0);

        c2 = new Coords(0, 0);

    }


    @XmlAttribute

    public String getFirst() {

        return this.c1.toString();

    }


    @XmlAttribute

    public String getSecond() {

        return this.c2.toString();

    }

}

重写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 {

    @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 x;

    }


    public int getY() {

        return y;

    }


    @Override

    public String toString() {

        StringBuilder builder = new StringBuilder();

        builder.append("Coords [x=");

        builder.append(x);

        builder.append(", y=");

        builder.append(y);

        builder.append("]");

        return builder.toString();

    }

}

这将产生与此类似的结果:


<tuple2 first="Coords [x=2, y=4]" second="Coords [x=12, y=12]"/>

属性first和的值second将是任何方法返回toString()的值Coords。


查看完整回答
反对 回复 2024-01-05
  • 1 回答
  • 0 关注
  • 43 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信