Java 日期和日期时间应该如何序列化 SOAP (xml) 消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21597364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:35:14  来源:igfitidea点击:

How are date and datetime supposed to be serialized SOAP (xml) messages

javaweb-servicesdatetimesoapxml-serialization

提问by Himanshu

We are working on a building a java client with a third party SOAP Webservice, that is I have not access or control off server side code. We are just provided with the WSDL description file of the service. We are using Axis 1 (version 1.4 ).

我们正在使用第三方 SOAP Web 服务构建 Java 客户端,即我无法访问或控制服务器端代码。我们只提供了服务的 WSDL 描述文件。我们正在使用 Axis 1(版本 1.4)。

We have run into following issue related to date vs datetime serialization and deserialization. The said wsdl defines two types DateTime and DateRange

我们遇到了以下与日期与日期时间序列化和反序列化相关的问题。所说的 wsdl 定义了两种类型 DateTime 和 DateRange

<xs:element minOccurs="0" name="DateTime" type="xs:dateTime"/>
<xs:element minOccurs="0" name="DateRange">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="Start" type="xs:date"/>
      <xs:element name="End" type="xs:date"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

Where xs prefix denotes XML Schema, that is following is present

其中 xs 前缀表示 XML Schema,即存在以下内容

xmlns:xs="http://www.w3.org/2001/XMLSchema" 

The axis wsdl2java generates Java Objects whre datetime field is type to Calendar, and start, end fields are typed to java.util.Date

wsdl2java 轴生成 Java 对象,其中 datetime 字段类型为 Calendar,start、end 字段类型为 java.util.Date

When serialization happens the start and end fields are serialized to yyyy-mm-dd format , for example 2014-02-01 But when the actual call is made to server side we recieve following response

当序列化发生时,开始和结束字段被序列化为 yyyy-mm-dd 格式,例如 2014-02-01 但是当实际调用服务器端时,我们收到以下响应

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header/>
   <SOAP-ENV:Body>
      <SOAP-ENV:Fault>
         <faultcode>SOAP-ENV:Server</faultcode>
         <faultstring xml:lang="en">JiBX unmarshalling exception; nested exception is org.jibx.runtime.JiBXException: Missing 'T' separator in dateTime</faultstring>
      </SOAP-ENV:Fault>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

We used SOAP UI to build the request xml payload directly and observed if we pass just the date portion in start and end fields we get exact same response, while if we pass those two fields with time portion like a dateTime field it works, gives no fault message.

我们使用 SOAP UI 直接构建请求 xml 有效负载,并观察如果我们只在开始和结束字段中传递日期部分,我们会得到完全相同的响应,而如果我们像日期时间字段一样传递带有时间部分的这两个字段,它会起作用,没有故障信息。

This when taken together with what I could gleam from XMLSchema documentation for xs:date and xs:dateTime, in particular lexical and canonical representation section, seems to imply that

这与我从xs:datexs:dateTime 的XMLSchema 文档 (特别是词法和规范表示部分)中得到的信息结合起来,似乎暗示着

  1. Axis based serialization of start and end field to just yyyy-mm-dd and ignoring the time portion is correct. And client side code generated conforms to WSDL provided to us.

  2. Server side code does not conform to WSDL provided to us, it expects a dateTime field rather than a date field.

  3. Since date and dateTime field are required to be serialized in different formats by the schema definition. The result it failure at server side when it attempts to de-serialize the message

  1. 基于轴的开始和结束字段序列化为 yyyy-mm-dd 并忽略时间部分是正确的。生成的客户端代码符合提供给我们的 WSDL。

  2. 服务器端代码不符合提供给我们的 WSDL,它需要一个日期时间字段而不是日期字段。

  3. 由于架构定义要求日期和日期时间字段以不同格式序列化。结果在服务器端尝试反序列化消息时失败

To validate our hypothesis 1. we used Python's ZSI library to generate python stub and resulting xml from it.
Even that serializes start and end in yyyy-mm-dd format , with an additional 'Z' at the end. This again fails to get deserialized at server side, even though this serialization format conforms to xs:date as defined by XML Schema

为了验证我们的假设 1. 我们使用 Python 的 ZSI 库来生成 python 存根并从中生成 xml。
即使这样以 yyyy-mm-dd 格式序列化开始和结束,最后还有一个额外的“Z”。这再次无法在服务器端反序列化,即使此序列化格式符合 XML Schema 定义的 xs:date

<ns1:Start>2014-02-05Z</ns1:Start>

Can somebody confirm if hypothesis formed by us, based on observed behaviour, documentation and experiment with Python's ZSI is correct. Or if we are missing some detail

有人可以确认我们根据观察到的行为、文档和 Python 的 ZSI 实验形成的假设是否正确。或者如果我们遗漏了一些细节

采纳答案by Meno Hochschild

Hm, you reason about your DateRange fields start and end, but the message is clearly about your DateTime field which requires (as XML schema says) a T-separator between date portion and time portion, not a space or any other char.

嗯,你推理你的 DateRange 字段开始和结束,但消息显然是关于你的 DateTime 字段,它需要(如 XML 架构所说)日期部分和时间部分之间的 T 分隔符,而不是空格或任何其他字符。

Please check how you serialize your DateTime field (not the start or end fields!). It must be in format "yyyy-MM-dd'T'HH:mm:ss".

请检查您如何序列化日期时间字段(不是开始或结束字段!)。它必须是格式"yyyy-MM-dd'T'HH:mm:ss"