我在注释模型类以支持所需的 XML 解组时遇到问题

发布于 2024-12-26 04:34:16 字数 4986 浏览 1 评论 0原文

我有兴趣使用 OXM 框架来支持以下 XML 的解组,这些 XML 将作为请求的 HTTP POST 有效负载传递给 RESTful API 调用。

这是我第一次尝试使用 OXM 框架,并且在正确注释模型类时遇到问题。我正在使用 Spring 3.0 和 JAXB2 Marshaller,但我对具体的 marshaller 实现并不关心。

问题:

1) 注释模型类以反映我所需的 XML 结构的最佳策略是什么?我在下面有一个如何注释类的示例,当我尝试创建 Marshaller 时,它会导致我的 XmlElements 的 Class 有两个同名“XXX”的属性。

2)我可以采取什么策略来支持我的注释和类?我将 SpatialExtent 定义为一个接口,然后实现 GeoBoundingBox XmlElement 的类实现了该接口。这适用于 Java,但不适用于 JAXB。

3) 是否有任何地方可以改进所需的 XML 以简化编组/解组?

任何帮助表示赞赏! --Stephan

背景:

这是我想要使用的 XML 示例:

<?xml version="1.0" encoding="UTF-8"?>
<AnalysisSettings>
    <service id="urn:nasa:giovanni:latlonplot"/>
    <spatialExtent>
        <GeoBoundingBox>
            <south>20.0</south>
            <north>90.0</north>
            <west>0.0</west>
            <east>180.0</east>
        </GeoBoundingBox>
    </spatialExtent>
    <temporalExtent>
        <TimePeriod>
            <startTime>2008-01-01T00:00:00Z</startTime>
            <endTime>2008-01-31T00:00:00Z</endTime>
        </TimePeriod>
    </temporalExtent>
    <variables>
        <Variable>
            <dataset id="urn:nasa:modis:MYD08_D3.005"/>
            <parameter id="urn:nasa:modis:Optical_Depth_Land_And_Ocean_Mean"/>
        </Variable>
        <Variable>
            <dataset id="urn:nasa:modis:M0D08_D3.005"/>
            <parameter id="urn:nasa:modis:Optical_Depth_Land_And_Ocean_Mean"/>
        </Variable>
    </variables>
</AnalysisSettings>

GeoBoundingBox 和 TimePeriod 并不是空间和时间范围的唯一可能类型,但到目前为止我还不必定义任何其他类型。

我正在使用 Spring 包注释。

我如何注释模型类的一个示例:

@XmlRootElement
@XmlType(name="AnalysisSettings")
public class AnalysisSettings {

    @XmlElement(name="service")
    private Service service;

    @XmlElement(name="spatialExtent")
    private SpatialExtent spatialExtent;

    @XmlElement(name="temporalExtent")
    private TemporalExtent temporalExtent ;

    @XmlElement(name="variables")
    private Variable[] variables;

    // standard getter and setter methods...
}

所有 XmlElements 注释都位于引用模型对象的类方法上,所有 XmlAttribute 注释都位于引用数据类型(例如 private String id)的类方法上。

我有一个早期的 XSD:

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="AnalysisSettings" type="AnalysisSettingsType" />
  <xsd:complexType name="AnalysisSettingsType">
    <xsd:sequence>
      <xsd:element name="service" type="serviceType" />
      <xsd:element name="spatialExtent" type="spatialExtentType" />
      <xsd:element name="temporalExtent" type="temporalExtentType" />
      <xsd:element name="variables" type="variablesType" />
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="variablesType">
    <xsd:sequence>
      <xsd:element maxOccurs="unbounded" name="Variable" type="variableType" />
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="variableType">
    <xsd:sequence>
      <xsd:element name="dataset" type="datasetType" />
      <xsd:element name="parameter" type="parameterType" />
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="parameterType">
    <xsd:attribute name="id" type="xsd:anyURI" />
  </xsd:complexType>
  <xsd:complexType name="datasetType">
    <xsd:attribute name="id" type="xsd:anyURI" />
  </xsd:complexType>
  <xsd:complexType name="temporalExtentType">
    <xsd:sequence>
      <xsd:element name="TimePeriod" type="TimePeriodType" />
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="TimePeriodType">
    <xsd:sequence>
      <xsd:element name="startTime" type="xsd:dateTime" />
      <xsd:element name="endTime" type="xsd:dateTime" />
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="spatialExtentType">
    <xsd:sequence>
      <xsd:element name="GeoBoundingBox" type="GeoBoundingBoxType" />
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="GeoBoundingBoxType">
    <xsd:sequence>
      <xsd:element name="south" type="xsd:decimal" />
      <xsd:element name="north" type="xsd:decimal" />
      <xsd:element name="west" type="xsd:decimal" />
      <xsd:element name="east" type="xsd:decimal" />
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="serviceType">
    <xsd:attribute name="id" type="xsd:anyURI" />
  </xsd:complexType>
</xsd:schema>

I am interested in using a OXM framework to support unmarshalling of the following XML that will be passed as the HTTP POST payload of a request to a RESTful API call.

This is my first time trying to use a OXM framework and I am having problems annotating my model classes correctly. I am using Spring 3.0 and the JAXB2 Marshaller, but I am indifferent as to the specific marshaller implementation.

Questions:

1) What is the best strategy for annotating my model classes to reflect my desired XML structure? I have an example of how I have been annotating my classes below, and it is resulting in Class has two properties with the same name "XXX" for my XmlElements when I try to create the Marshaller.

2) What strategy can I take to support in my annotations and classes? I was defining SpatialExtent as an interface and then the class that implements a GeoBoundingBox XmlElement implemented this interface. This works for Java but not for JAXB.

3) Are there any places where the desired XML can be improved to simplify marshalling/unmarshalling?

Any help is appreciated!
--Stephan

Background:

Here is an example of the XML I would like to consume:

<?xml version="1.0" encoding="UTF-8"?>
<AnalysisSettings>
    <service id="urn:nasa:giovanni:latlonplot"/>
    <spatialExtent>
        <GeoBoundingBox>
            <south>20.0</south>
            <north>90.0</north>
            <west>0.0</west>
            <east>180.0</east>
        </GeoBoundingBox>
    </spatialExtent>
    <temporalExtent>
        <TimePeriod>
            <startTime>2008-01-01T00:00:00Z</startTime>
            <endTime>2008-01-31T00:00:00Z</endTime>
        </TimePeriod>
    </temporalExtent>
    <variables>
        <Variable>
            <dataset id="urn:nasa:modis:MYD08_D3.005"/>
            <parameter id="urn:nasa:modis:Optical_Depth_Land_And_Ocean_Mean"/>
        </Variable>
        <Variable>
            <dataset id="urn:nasa:modis:M0D08_D3.005"/>
            <parameter id="urn:nasa:modis:Optical_Depth_Land_And_Ocean_Mean"/>
        </Variable>
    </variables>
</AnalysisSettings>

GeoBoundingBox and TimePeriod are not the only possible types for spatial and temporal extents, but I have not had to define any other types as of yet.

I am using Spring package annotations.

An example of how I have been annotating my model classes:

@XmlRootElement
@XmlType(name="AnalysisSettings")
public class AnalysisSettings {

    @XmlElement(name="service")
    private Service service;

    @XmlElement(name="spatialExtent")
    private SpatialExtent spatialExtent;

    @XmlElement(name="temporalExtent")
    private TemporalExtent temporalExtent ;

    @XmlElement(name="variables")
    private Variable[] variables;

    // standard getter and setter methods...
}

All XmlElements annotations are on class methods that reference model objects and all XmlAttribute annotations are on class methods that reference datatypes (e.g. private String id).

I have an early XSD:

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="AnalysisSettings" type="AnalysisSettingsType" />
  <xsd:complexType name="AnalysisSettingsType">
    <xsd:sequence>
      <xsd:element name="service" type="serviceType" />
      <xsd:element name="spatialExtent" type="spatialExtentType" />
      <xsd:element name="temporalExtent" type="temporalExtentType" />
      <xsd:element name="variables" type="variablesType" />
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="variablesType">
    <xsd:sequence>
      <xsd:element maxOccurs="unbounded" name="Variable" type="variableType" />
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="variableType">
    <xsd:sequence>
      <xsd:element name="dataset" type="datasetType" />
      <xsd:element name="parameter" type="parameterType" />
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="parameterType">
    <xsd:attribute name="id" type="xsd:anyURI" />
  </xsd:complexType>
  <xsd:complexType name="datasetType">
    <xsd:attribute name="id" type="xsd:anyURI" />
  </xsd:complexType>
  <xsd:complexType name="temporalExtentType">
    <xsd:sequence>
      <xsd:element name="TimePeriod" type="TimePeriodType" />
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="TimePeriodType">
    <xsd:sequence>
      <xsd:element name="startTime" type="xsd:dateTime" />
      <xsd:element name="endTime" type="xsd:dateTime" />
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="spatialExtentType">
    <xsd:sequence>
      <xsd:element name="GeoBoundingBox" type="GeoBoundingBoxType" />
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="GeoBoundingBoxType">
    <xsd:sequence>
      <xsd:element name="south" type="xsd:decimal" />
      <xsd:element name="north" type="xsd:decimal" />
      <xsd:element name="west" type="xsd:decimal" />
      <xsd:element name="east" type="xsd:decimal" />
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="serviceType">
    <xsd:attribute name="id" type="xsd:anyURI" />
  </xsd:complexType>
</xsd:schema>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文