覆盖第 3 方库的 XmlAdapter class
Overriding XmlAdapter for 3rd party library class
我正在为第三方库 class 使用 jaxbMarshaller 生成 xml。由于将 Calendar 对象转换为字符串的库 XmlAdapter 未使用 TimeZone 字段,因此编组器为 pojo class.
的每个 Calendar 字段生成错误的 xml
第 3 方库 XmlAdapter 使用以下 class 将日历转换为字符串:
public class DateConversion {
public static String printDate(Calendar value) {
if(value != null) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
return format.format(value.getTime());
}
return null;
}
}
所以我想为日历字段覆盖 XmlAdapter 的行为并尝试了下面的示例,但它似乎不起作用:
我的自定义 XmlAdapter 使用以下 class 进行转换:
public class DateConversion {
public static String printDate(Calendar value, TimeZone timeZone) {
if(value != null) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
format.setTimeZone(timeZone);
return format.format(value.getTime());
}
return null;
}
}
然后我完成了如下注册:
public @Nullable
String toPdxmlString(final @NotNull Deals input) {
try {
final Marshaller marshaller = jaxbContext.createMarshaller();
final DateFormatterAdapter dateFormatterAdapter = new DateFormatterAdapter(PdxmlDateTimeUtil.FXONLINE_DEFAULT_DEAL_TIMEZONE);
marshaller.setAdapter(dateFormatterAdapter);
StringWriter writer = new StringWriter();
marshaller.marshal(input, writer);
return writer.toString();
} catch (JAXBException exception) {
LOGGER.error("Unable to marshall the given input Deals: {}, into String using JAXB Context: {}, ... ", input, jaxbContext, exception);
}
return null;
}
任何人都可以帮助我知道这是否可行,如果可以,我哪里出错了?
所以我找到了我的解决方案。我扩展了第 3 方库的 XmlAdapter,并在 DateConversion 中插入了 TimeZone 字段,例如:
public class DateFormatterAdapter extends Adapter2 {
private final TimeZone timeZone;
public DateFormatterAdapter(final TimeZone timeZone) {
this.timeZone = timeZone;
}
@Override
public Calendar unmarshal(String value) {
return javax.xml.bind.DatatypeConverter.parseDate(value);
}
@Override
public String marshal(Calendar calendar) {
return DateConversion.printDate(calendar, timeZone);
}
}
最后将扩展的 XmlAdapter 注册为:
public @Nullable
String toPdxmlString(final @NotNull Deals input) {
try {
final Marshaller marshaller = jaxbContext.createMarshaller();
final DateFormatterAdapter dateFormatterAdapter = new DateFormatterAdapter(PdxmlDateTimeUtil.FXONLINE_DEFAULT_DEAL_TIMEZONE);
marshaller.setAdapter(Adapter2.class, dateFormatterAdapter);
StringWriter writer = new StringWriter();
marshaller.marshal(input, writer);
return writer.toString();
} catch (JAXBException exception) {
LOGGER.error("Unable to marshall the given input Deals: {}, into String using JAXB Context: {}, ... ", input, jaxbContext, exception);
}
return null;
}
我正在为第三方库 class 使用 jaxbMarshaller 生成 xml。由于将 Calendar 对象转换为字符串的库 XmlAdapter 未使用 TimeZone 字段,因此编组器为 pojo class.
的每个 Calendar 字段生成错误的 xml第 3 方库 XmlAdapter 使用以下 class 将日历转换为字符串:
public class DateConversion {
public static String printDate(Calendar value) {
if(value != null) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
return format.format(value.getTime());
}
return null;
}
}
所以我想为日历字段覆盖 XmlAdapter 的行为并尝试了下面的示例,但它似乎不起作用:
我的自定义 XmlAdapter 使用以下 class 进行转换:
public class DateConversion {
public static String printDate(Calendar value, TimeZone timeZone) {
if(value != null) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
format.setTimeZone(timeZone);
return format.format(value.getTime());
}
return null;
}
}
然后我完成了如下注册:
public @Nullable
String toPdxmlString(final @NotNull Deals input) {
try {
final Marshaller marshaller = jaxbContext.createMarshaller();
final DateFormatterAdapter dateFormatterAdapter = new DateFormatterAdapter(PdxmlDateTimeUtil.FXONLINE_DEFAULT_DEAL_TIMEZONE);
marshaller.setAdapter(dateFormatterAdapter);
StringWriter writer = new StringWriter();
marshaller.marshal(input, writer);
return writer.toString();
} catch (JAXBException exception) {
LOGGER.error("Unable to marshall the given input Deals: {}, into String using JAXB Context: {}, ... ", input, jaxbContext, exception);
}
return null;
}
任何人都可以帮助我知道这是否可行,如果可以,我哪里出错了?
所以我找到了我的解决方案。我扩展了第 3 方库的 XmlAdapter,并在 DateConversion 中插入了 TimeZone 字段,例如:
public class DateFormatterAdapter extends Adapter2 {
private final TimeZone timeZone;
public DateFormatterAdapter(final TimeZone timeZone) {
this.timeZone = timeZone;
}
@Override
public Calendar unmarshal(String value) {
return javax.xml.bind.DatatypeConverter.parseDate(value);
}
@Override
public String marshal(Calendar calendar) {
return DateConversion.printDate(calendar, timeZone);
}
}
最后将扩展的 XmlAdapter 注册为:
public @Nullable
String toPdxmlString(final @NotNull Deals input) {
try {
final Marshaller marshaller = jaxbContext.createMarshaller();
final DateFormatterAdapter dateFormatterAdapter = new DateFormatterAdapter(PdxmlDateTimeUtil.FXONLINE_DEFAULT_DEAL_TIMEZONE);
marshaller.setAdapter(Adapter2.class, dateFormatterAdapter);
StringWriter writer = new StringWriter();
marshaller.marshal(input, writer);
return writer.toString();
} catch (JAXBException exception) {
LOGGER.error("Unable to marshall the given input Deals: {}, into String using JAXB Context: {}, ... ", input, jaxbContext, exception);
}
return null;
}