我正在将SOAP WS转换为Client的REST WS。现在,在“数据类型对象”中具有返回类型为XMLGregorianCalendar的字段。
由于我需要保持Input / Output结构不变,所以我不能更改任何变量名称及其返回类型。
在Rest WS中,我们使用json对象作为返回值。
进程:-我们将输入发送到dto,然后调用procedure,并以java.util.date格式获得来自过程的响应。 现在,我需要将此java.util.date(数字)更改为Date,格式与XMLGregorianCalendar相同。
感谢您的帮助。
我尝试了以下代码。
将DTO变量的返回类型更改为java.util.Date
private Date informeFechaDlgncmnto;
@JsonSerialize(using = JsonDateTimeSerializer.class)
public Date getInformeFechaDlgncmnto() {
return informeFechaDlgncmnto;
}
public void setInformeFechaDlgncmnto(Date informeFechaDlgncmnto) {
this.informeFechaDlgncmnto = informeFechaDlgncmnto;
}
JsonDateTimeSerializer类
private static final SimpleDateFormat formatter = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss.SSS");
@Override
public void serialize(Date date, JsonGenerator gen,
SerializerProvider provider) throws IOException,
JsonProcessingException {
String formattedDate = formatter.format(date);
gen.writeString(formattedDate);
}
REST WS的当前输出:-
“ informeFechaDlgncmnto”:“ 2019-04-09T12:20:44.000”,
SOAP WS代码
DTO中的变量
@XmlSchemaType(name = "dateTime")
protected XMLGregorianCalendar informeFechaDlgncmnto;
public XMLGregorianCalendar getInformeFechaDlgncmnto() {
return informeFechaDlgncmnto;
}
public void setInformeFechaDlgncmnto(XMLGregorianCalendar value) {
this.informeFechaDlgncmnto = value;
}
在Servicelayer中更改gregorianCalendar的代码
private ConsultaFuratRespType addTZInformeFechaDlgncmnto(
ConsultaFuratRespType consultaFuratRespType) {
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss.SSS");
simpleDateFormat.setTimeZone(TimeZone
.getTimeZone("UTC-0700";));
Calendar gregorianCalendar = new GregorianCalendar(
TimeZone.getTimeZone("UTC-0700";));
gregorianCalendar = consultaFuratRespType.getData().getDatosFurat()
.getInformeFechaDlgncmnto().toGregorianCalendar();
String gregorianCalendarString = simpleDateFormat
.format(gregorianCalendar.getTime());
Date date = simpleDateFormat.parse(gregorianCalendarString);
GregorianCalendar gregCalInformeFechaDlgncmnto = new GregorianCalendar(
TimeZone.getTimeZone(ConstantesApp.TIMEZONE_UTC_MINUS_5000));
gregCalInformeFechaDlgncmnto = (GregorianCalendar) GregorianCalendar
.getInstance();
gregCalInformeFechaDlgncmnto.setTime(date);
consultaFuratRespType.getData().getDatosFurat()
.setInformeFechaDlgncmnto(gregCalInformeFechaDlgncmnto);
return consultaFuratRespType;
} catch (DatatypeConfigurationException e) {
return consultaFuratRespType;
} catch (Exception e) {
return consultaFuratRespType;
}
SOAPUI中的输出
2019-04-09T12:20:44.0-05:00
输出应相同。
当前输出:-
SOAP WS:-2019-04-09T12:20:44.0-05:00
REST WS:-2019-04-09T12:20:44.000