有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

第三方库类的java重写XmlAdapter

我正在使用jaxbMarshaller为第三方库类生成xml。由于将日历对象转换为字符串的库XmlAdapter没有使用时区字段,所以marshaller为pojo类的每个日历字段生成错误的xml

第三方库XmlAdapter正在使用以下类进行日历到字符串的转换:

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 for Calendar字段的行为,并尝试了以下示例,但似乎不起作用:

我的自定义XmlAdapter正在使用以下类进行转换:

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;
    }

有谁能帮我知道这是否可行,如果可行,我错在哪里


共 (1) 个答案

  1. # 1 楼答案

    所以我找到了解决办法。我扩展了第三方库的XmlAdapter,并在DateConversion中插入了时区字段,如:

    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;
        }