告诉JAXB unmarshal< xs:dateTime>使用注释到Date类 [英] Tell JAXB to unmarshal <xs:dateTime> to Date class using annotations
问题描述
将JAXB与Java-First一起使用时, java.util.Date
类型的字段/属性被编组并解组为 xs:dateTime
,一切都按预期工作。
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 unmarshals xs:dateTime
to XMLGregorianCalendarImpl
。
But if the type of the field/property is Object
, JAXB unmarshals xs:dateTime
to XMLGregorianCalendarImpl
.
我需要找到一种方法,使用注释使JAXB解组日期时间值为 java.util.Date
。否则,我需要在每个用例中查看所有未编组的值。
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.
即使在包含Object字段的类上定义了一些after-unmarshall-hook也是如此并手动转换实例会很好。但我找不到任何可以这种方式使用的东西。
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.
请注意,我对JAXB上下文的访问权限有限,因为它在Apache CXF中使用。
Note that I have limited access to the JAXB context, as it is used inside Apache CXF.
推荐答案
您可以在@XmlElement上设置type属性。
You can set the type property on @XmlElement.
@XmlElement(type=Date.class)
public Object getGenericDateProperty() {
return date;
}
编辑:
由于您不知道可以使用XmlAdapter的类型。如果unmarshalled值是XMLGregorianCalendar,则将其转换为Date。有关XmlAdapter的更多信息,请参阅:
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:
- http://bdoughan.blogspot.com/2010/07/xmladapter-jaxbs-secret-weapon.html
这篇关于告诉JAXB unmarshal< xs:dateTime>使用注释到Date类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!