简体   繁体   English

使用Java指定SOAP响应中元素的顺序

[英]Specify order of elements in a SOAP response using java

I have a web service that returns a dataset object that contains the current weather forecast along with 0 or more weather alerts for a county/state. 我有一个Web服务,该服务返回一个数据集对象,该对象包含当前天气预报以及一个县/州的0个或多个天气警报。 The dataset object just contains a Weather object and an array of Alerts objects. 数据集对象仅包含一个Weather对象和一系列Alerts对象。 One of the clients of this would like to have it so the response gives the weather first instead of the alerts. 其中的一位客户希望拥有它,因此响应将首先给出天气而不是警报。 Is there a way to specify the order of the response elements? 有没有办法指定响应元素的顺序? I thought I could just change the WSDL to map out the weather first then the alerts, but that didn't do anything. 我以为我可以先更改WSDL以先绘制天气,然后再绘制警报,但这并没有做任何事情。

Here's the generic WSDL sheet: 这是通用的WSDL工作表:
(well, it showed formatted in the preview but not after posting... how can I post formatted XML on here? I tried using back-ticks as well as pre and code). (好吧,它在预览中显示了格式化的格式,但在发布后没有显示……如何在此处发布格式化的XML?我尝试使用反引号以及pre和code)。

<wsdl:definitions ...>
 <wsdl:types>
  <schema elementFormDefault="qualified" targetNamespace="http://ws.sample.com" xmlns="http://www.w3.org/2001/XMLSchema">
   <import namespace="http://objects.sample.com"/>
   <element name="getAll">
    <complexType>
     <sequence>
      <element name="county" type="xsd:string"/>
      <element name="state" type="xsd:string"/>
      <element name="latitude" type="xsd:double"/>
      <element name="longitude" type="xsd:double"/>
     </sequence>
    </complexType>
   </element>
   <element name="getAllResponse">
    <complexType>
     <sequence>
      <element name="getAllReturn" type="tns1:DataSet"/>
     </sequence>
    </complexType>
   </element>
   <complexType name="ArrayOf_tns1_Alert">
    <sequence>
     <element maxOccurs="unbounded" minOccurs="0" name="item" type="tns1:Alert"/>
    </sequence>
   </complexType>
  </schema>
  <schema elementFormDefault="qualified" targetNamespace="http://objects.sample.com" xmlns="http://www.w3.org/2001/XMLSchema">
   <import namespace="http://ws.sample.com"/>
   <complexType name="Alert">
    <sequence>
     <element name="county" nillable="true" type="xsd:string"/>
     <element name="endDate" nillable="true" type="xsd:dateTime"/>
     <element name="locationCode" nillable="true" type="xsd:string"/>
     <element name="startDate" nillable="true" type="xsd:dateTime"/>
     <element name="state" nillable="true" type="xsd:string"/>
     <element name="title" nillable="true" type="xsd:string"/>
     <element name="warning" nillable="true" type="xsd:string"/>
    </sequence>
   </complexType>
   <complexType name="Weather">
    <sequence>
     <element name="chancePrecipitation" type="xsd:int"/>
     <element name="period" nillable="true" type="xsd:string"/>
     <element name="skyConditions" nillable="true" type="xsd:string"/>
     <element name="temperature" type="xsd:int"/>
     <element name="temperatureType" nillable="true" type="xsd:string"/>
     <element name="temperatureUnit" nillable="true" type="xsd:string"/>
     <element name="windDirection" nillable="true" type="xsd:string"/>
     <element name="windSpeed" type="xsd:int"/>
     <element name="windUnit" nillable="true" type="xsd:string"/>
    </sequence>
   </complexType>
   <complexType name="DataSet">
    <sequence>
     <element name="weather" nillable="true" type="tns1:Weather"/>
     <element name="alert" nillable="true" type="impl:ArrayOf_tns1_Alert"/>
    </sequence>
   </complexType>
  </schema>
 </wsdl:types>
   <wsdl:message name="getAllResponse">
      <wsdl:part element="impl:getAllResponse" name="parameters"/>
   </wsdl:message>
   <wsdl:message name="getAllRequest">
      <wsdl:part element="impl:getAll" name="parameters"/>
   </wsdl:message>
   <wsdl:portType name="TSTWeather">
      <wsdl:operation name="getAll">
         <wsdl:input message="impl:getAllRequest" name="getAllRequest"/>
         <wsdl:output message="impl:getAllResponse" name="getAllResponse"/>
      </wsdl:operation>
   </wsdl:portType>
   <wsdl:binding name="TSTWeatherSoapBinding" type="impl:TSTWeather">
      <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
      <wsdl:operation name="getAll">
         <wsdlsoap:operation soapAction=""/>
         <wsdl:input name="getAllRequest">
            <wsdlsoap:body use="literal"/>
         </wsdl:input>
         <wsdl:output name="getAllResponse">
            <wsdlsoap:body use="literal"/>
         </wsdl:output>
      </wsdl:operation>
   </wsdl:binding>
   <wsdl:service name="TSTWeatherService">
      <wsdl:port binding="impl:TSTWeatherSoapBinding" name="TSTWeather">
         <wsdlsoap:address location="http://localhost:8282/Services/service/TSTWeather"/>
      </wsdl:port>
   </wsdl:service>
</wsdl:definitions>

I don't see how I could specify the order of my service response. 我看不到如何指定服务响应的顺序。

在许多情况下,仅更改WSDL不会更改服务,而是由服务确定XML中元素的顺序。

We can change the order by adding JAXB annotations in the particular java file. 我们可以通过在特定的Java文件中添加JAXB批注来更改顺序。

For example: @XmlType(propOrder = {"x", "y", "z"}) 例如:@XmlType(propOrder = {“ x”,“ y”,“ z”})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM