jackson/json 架构 - 用外部 json 架构生成器模块替换 ISO8601Utils

jackson/json schema - replacing ISO8601Utils with the external json schema generator module

我正在替换下面注释的 ISO8601Utils,因为 SonarQube 会抛出以下错误:Remove this use of "ISO8601Utils"; it is deprecated. 要替换它,我会使用外部 json 模式生成器模块,https://github.com/FasterXML/jackson-module-jsonSchema 或其他。我通读了 link,但不明白如何使用对象映射器来替换这一行:String value = ISO8601Utils.format(date, true);

    public static class ISO8601DateFormat extends DateFormat {

    public ISO8601DateFormat() {}

    public StringBuffer format(Date date, StringBuffer toAppendTo,
            FieldPosition fieldPosition) {
        String value = ISO8601Utils.format(date, true);
        //Im not sure how I can replace this line with a new
        //replacement

        toAppendTo.append(value);
        return toAppendTo;
    }

    public Date parse(String source, ParsePosition pos) {
        pos.setIndex(source.length());
        return ISODateTimeFormat.dateTimeParser().parseDateTime(source).toDate();
    }

    public Object clone() {
        return this;
    }

    public String toString() {
        return this.getClass().getName();
    }
}

如有任何帮助,我们将不胜感激!

P.S。我正在编写一个单元测试来验证 ISO8601Utils 和 SimpleDateFormat 是否具有相同的格式。

我的更新 class:

    public static class ISO8601DateFormat extends DateFormat {

    public static final long serialVersionUID = 3549786448500970210L;

    public ISO8601DateFormat() {}

    @Override
    public StringBuffer format(Date date, StringBuffer toAppendTo,
            FieldPosition fieldPosition) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        String value = dateFormat.format(date);
        toAppendTo.append(value);
        return toAppendTo;
    }

    public Date parse(String source, ParsePosition pos) {
        pos.setIndex(source.length());
        return ISODateTimeFormat.dateTimeParser().parseDateTime(source).toDate();
    }

    public Object clone() {
        return this;
    }

    public String toString() {
        return this.getClass().getName();
    }
}

我的测试方法:

  @Test
    public void testDateFormat() {
     df = new DefaultHttpClientUtil.ISO8601DateFormat();
     Date date = new Date();
     // df.setTimeZone(TimeZone.getTimeZone("GMT")); I'm getting NPE   
     // for this line

    assertThat(df.format(date)).isEqualTo(ISO8601Utils.format(date, 
    true));
  }

但是,我收到注释行的空指针异常。我认为它与注入或模拟对象有关,但我不确定我应该如何解决这个问题。

ISO8601DateFormat and ISO8601Utils are deprecated since version 2.9 and do not provide clear documentation what to do after it was deprecated. There was a question on GitHub: ISO8601DateFormat is deprecated but replacement is unclear 与您的问题相关。

我猜,你们团队的某个人刚刚使用了第一个 class he/she 发现将 Date 序列化为 ISO8601 格式并且没有引起广泛关注它来自 Jackson 图书馆。我建议不要使用您出于其他原因使用的库中的内部 *Utils classes。在那种情况下 serialising/deserialising JSON。现在,我们有很多如何格式化日期的选项,您应该选择一个适合您的选项。大多数情况下,这取决于您使用哪个 JDK 版本。

所以,改为:

String value = ISO8601Utils.format(date, true);

例如使用SimpleDateFormat:

DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

如您所见,下面的示例为两种情况打印了相同的日期:

import com.fasterxml.jackson.databind.util.ISO8601Utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class JsonApp {

    public static void main(String[] args) throws Exception {
        Date date = new Date();
        DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        df2.setTimeZone(TimeZone.getTimeZone("GMT"));

        System.out.println(ISO8601Utils.format(date, true));
        System.out.println(df2.format(date));
    }
}

以上代码打印:

2019-02-19T19:37:14.809Z
2019-02-19T19:37:14.809Z

ISO8601Utils.format 的第二个参数是 true,这意味着您需要毫秒的结果,格式应类似于 yyyy-MM-ddThh:mm:ss.sss'Z'.

有关详细信息,请阅读:

  1. Converting ISO 8601-compliant String to java.util.Date
  2. How to convert time in YYYY-MM-DDTHH:mm:ss.SSSZ format to default timezone?