java 告诉 JAXB 使用注释将 <xs:dateTime> 解组为 Date 类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3953433/
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
Tell JAXB to unmarshal <xs:dateTime> to Date class using annotations
提问by Iravanchi
When using JAXB with Java-First, fields/properties of type java.util.Date
are marshalled and unmarshalled as xs:dateTime
and everything works as expected.
将 JAXB 与 Java-First 一起使用时,类型的字段/属性java.util.Date
会xs:dateTime
按预期进行编组和解组,并且一切正常。
But if the type of the field/property is Object
, JAXB unmarshals xs:dateTime
to XMLGregorianCalendarImpl
.
但如果字段/属性的类型是Object
,JAXB 将解组xs:dateTime
为XMLGregorianCalendarImpl
。
I need to find a way that makes JAXB unmarshal date-time values to java.util.Date
by itself using annotations. Otherwise, I'll need to go through all unmarshalled values in each use case.
我需要找到一种方法,使 JAXBjava.util.Date
使用注释自行解组日期时间值。否则,我需要在每个用例中检查所有未编组的值。
Even if there were some after-unmarshall-hook to define on the classes containing Object fields and convert the instances manually would be good. But I couldn't find anything that can be used this way either.
即使在包含 Object 字段的类上定义一些 after-unmarshall-hook 并手动转换实例也会很好。但我也找不到任何可以用这种方式使用的东西。
Note that I have limited access to the JAXB context, as it is used inside Apache CXF.
请注意,我对 JAXB 上下文的访问权限有限,因为它在 Apache CXF 中使用。
采纳答案by bdoughan
You can set the type property on @XmlElement.
您可以在@XmlElement 上设置类型属性。
@XmlElement(type=Date.class)
public Object getGenericDateProperty() {
return date;
}
Edit:
编辑:
Since you don't know the type you could use an XmlAdapter. If the unmarshalled value is an XMLGregorianCalendar convert it to a Date. For more info on XmlAdapter see:
由于您不知道您可以使用 XmlAdapter 的类型。如果未编组的值是 XMLGregorianCalendar,则将其转换为日期。有关 XmlAdapter 的更多信息,请参阅:
回答by Iravanchi
In addition to Blaise Doughan's answer:
除了 Blaise Doughan 的回答:
I could finally figure this out, thanks for help from Blaise Doughan. Actually his answer works with just a small change: if there's several types expected to be unmarshalled as the Object property, there needs to be multiple @XmlElement
annotations placed on it using @XmlElements
annotation.
我终于可以解决这个问题了,感谢 Blaise Doughan 的帮助。实际上,他的回答只进行了一个小改动:如果有几种类型需要被解组为 Object 属性,则需要@XmlElement
使用@XmlElements
annotation在其上放置多个annotation。
Here's my code now:
这是我的代码:
@XmlElements
({
@XmlElement(name = "dateValue", type = Date.class),
@XmlElement(name = "stringValue", type = String.class),
@XmlElement(name = "booleanValue", type = Boolean.class),
@XmlElement(name = "listValue", type = ArrayList.class),
@XmlElement(name = "bytesValue", type = Byte[].class)
})
public Object getFieldValue()
{
return fieldValue;
}
Note: specifying "name" is required for this to work, since there should be a way for the marshaller / unmarshaller to identify the type of the content.
注意:为此需要指定“名称”,因为编组器/解组器应该有一种方法来识别内容的类型。
There are two minor issues here:
这里有两个小问题:
You need to specify a list of all of the types expected (which is logical, given the case of marshalling)
There's no way to specify a single name for this property. In my case, where JAXB is used in CXF web services, code generated from WSDL in .NET names this field as "Item". If there was a way, for example, to wrap the XML elements in another one which has a single name, the generated code could be a little bit nicer.
您需要指定所有预期类型的列表(考虑到编组情况,这是合乎逻辑的)
无法为此属性指定单个名称。就我而言,在 CXF Web 服务中使用 JAXB 时,从 .NET 中的 WSDL 生成的代码将此字段命名为“项目”。例如,如果有一种方法可以将 XML 元素包装在另一个具有单一名称的元素中,那么生成的代码可能会更好一些。