我正在尝试映射两个属性,都是XMLGregorianCalendar类型。通过dozer尝试实现这一点,值被映射,但我得到了如下所示的奇怪的日期时间值
XML中的输入
<urn1:ReservationDate>2015-02-11</urn1:ReservationDate>
<urn1:ReservationTime>03:28:00</urn1:ReservationTime>JSON格式的输出
"reservationDate": 1423593000000,
"reservationTime": -7320000我的dozer映射如下所示
<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns="http://dozer.sourceforge.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://dozer.sourceforge.net
http://dozer.sourceforge.net/schema/beanmapping.xsd">
<configuration>
<stop-on-errors>true</stop-on-errors>
<date-format>MM/dd/yyyy HH:mm</date-format>
<!-- <date-format>MM-dd-yyyy</date-format> -->
<wildcard>true</wildcard>
<custom-converters>
<converter type="com.xxx.util.XMLGC2XMLGC">
<class-a>javax.xml.datatype.XMLGregorianCalendar</class-a>
<class-b>javax.xml.datatype.XMLGregorianCalendar</class-b>
</converter>
</custom-converters>
</configuration>
<mapping>
<class-a>com.xxx..ReservationType
</class-a>
<class-b>com.xxx..ReservationDto
</class-b>
<field>
<a>pnrLocator</a>
<b>pnrLocator</b>
</field>
<field>
<a>reservationDate</a>
<b>reservationDate</b>
<a-hint>java.util.GregorianCalendar</a-hint>
</field>
</mapping>
</mappings> XMLGC2XMLGC类代码
public class XMLGC2XMLGC extends
DozerConverter<XMLGregorianCalendar, XMLGregorianCalendar> {
public XMLGC2XMLGC() {
super(XMLGregorianCalendar.class, XMLGregorianCalendar.class);
}
@Override
public XMLGregorianCalendar convertFrom(XMLGregorianCalendar src,
XMLGregorianCalendar dest) {
return src;
}
@Override
public XMLGregorianCalendar convertTo(XMLGregorianCalendar src,
XMLGregorianCalendar dest) {
return dest;
} 我甚至尝试了日期到XMLGregorianCalendar的转换,因为我阅读了一些文档,说它应该是自动发生的,但在执行相同的操作时,我得到了类似以下的异常
公共:类public不能访问具有修饰符\“java.lang.IllegalAccessException\”的类org.dozer.util.ReflectionUtils的成员
请帮帮忙
发布于 2015-02-18 19:13:16
这里的问题是JSON序列化。我必须编写一个自定义序列化程序,以便在JSON中正确显示XMLGregorianCalendar日期
代码如下
public class CustomDateSerializer extends JsonSerializer<XMLGregorianCalendar> {
@Override
public void serialize(XMLGregorianCalendar value, JsonGenerator gen, SerializerProvider arg2) throws
IOException, JsonProcessingException {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = formatter.format(value.toGregorianCalendar().getTime());
gen.writeString(formattedDate);
}
}并对相应的字段进行了如下注释
@JsonSerialize(using = CustomDateSerializer.class)
private XMLGregorianCalendar reservationDate;这足以在JSON中获得正确的日期格式:)
希望这对某些人有帮助!
https://stackoverflow.com/questions/28555730
复制相似问题