【问题标题】:Wrap XML response with JAXB and Jersey使用 JAXB 和 Jersey 包装 XML 响应
【发布时间】:2015-11-19 12:12:01
【问题描述】:

我使用 Jersey 来实现我的 REST API,并且我有以下服务:

@Path("discoverytopology")
public class DiscoveryTopology {

    @GET
    @Produces("application/xml")
    public List<Definition> getDefinitionList() {
        List<Definition> definitions = db.getDefinitions();
        return definitions;
    }
}

Definition 类由 XML 注释进行注释,XML 中的响应包含许多定义,例如:

<defition body>
<defition body>
....

但我想要这样的东西,我需要为 XML 中的每个定义一个 ID,就像:

<DefinitionList>
    <definition id=1>
        <definition body>
    </definition>
    <definition id=2>
        <definition body>
    </definition>
    ............
</DefinitionsList>

我该怎么做?

【问题讨论】:

  • 如果我的解决方案适合您,请告诉我。
  • 它工作,非常感谢详细的解释。
  • 您好,Cassio,我遇到了一个新问题,您能回答这个问题吗?非常感谢:stackoverflow.com/questions/33860128/…
  • 当然!让我看看吧。

标签: xml parsing jaxb jersey


【解决方案1】:

创建包装器

创建一个DefinitionWrapper 类,注释如下:

@XmlRootElement(name = "DefinitionList")
@XmlAccessorType(XmlAccessType.FIELD)
public class DefinitionWrapper {

    @XmlElement(name = "definition")
    private List<Definition> definitions;

    public List<Definition> getDefinitions() {
        return definitions;
    }

    public void setDefinitions(List<Definition> definitions) {
        this.definitions = definitions;
    }
}

确保Definition 类的注释如下:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Definition implements Serializable {

    @XmlAttribute
    private Integer id;

    // Other fields

    // Getters and setters
}

并更改您的端点方法以返回DefinitionWrapper

@Path("discoverytopology")
public class DiscoveryTopology {

    @GET
    @Produces("application/xml")
    public DefinitionWrapper getDefinitionList() {
        List<Definition> definitions = db.getDefinitions();
        DefinitionWrapper definitionWrapper = new DefinitionWrapper;
        definitionWrapper.setDefinitions(definitions);
        return definitionWrapper;
   }
}

当编组为 XML 时,结果将是:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<DefinitionList>
    <definition id="1"/>
    <definition id="2"/>
    <definition id="3"/>
</DefinitionList>

添加元素

您的Definition 类中可能需要除id 之外的其他字段。例如,考虑一下,您需要一个description 字段,它应该被编组为一个XML 元素。

要实现它,请使用 @XmlElement 注释您的字段,如下所示:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Definition implements Serializable {

    @XmlAttribute
    private Integer id;

    @XmlElement
    private String description;

    // Getters and setters
}

当编组为 XML 时,结果将是:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<DefinitionList>
    <definition id="1">
        <text>This is a description</text>
    </definition>
    <definition id="2">
        <text>This is a description</text>
    </definition>
    <definition id="3">
        <text>This is a description</text>
    </definition>
</DefinitionList>

添加值

您可以使用@XmlValue注释字段:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Definition implements Serializable {

    @XmlAttribute
    private Integer id;

    @XmlValue
    private String value;

    // Getters and setters
}

当您的对象被编组为 XML 时,结果将如下:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<DefinitionList>
    <definition id="1">This is the value</definition>
    <definition id="2">This is the value</definition>
    <definition id="3">This is the value</definition>
</DefinitionList>

但是,每个类只允许使用一个带有 @XmlValue 注释的字段。如果一个类有任何用@XmlElement注解的字段,它就不能有任何用@XmlValue注解的字段。

【讨论】:

    【解决方案2】:

    你需要一个对象来代表你的定义列表,该对象将被@XMLRootElelement注解,并且它必须包含Definition的列表

    JAX-B 不能创建 &lt;DefinitionList&gt; 元素,除非它知道它存在。

    然后您需要正确注释 Definition 类,以便您的 &lt;definition&gt; 元素看起来像您想要的,属性与属性等。

    我不确定您的 &lt;definition body&gt; withtin &lt;definition id="1"&gt; 是否只是表示“定义主体”的填充物,但如果不是,则在同一架构中拥有 2 个具有不同定义的元素可能会很棘手。不确定它是正确的 XML。

    【讨论】:

      猜你喜欢
      • 2014-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多