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

问题描述 投票:2回答:1

我正在替换ISO8601Utils,由于SonarQube引发以下错误,下面将对此进行评论:Remove this use of "ISO8601Utils"; it is deprecated.要替换它,我将使用外部json模式生成器模块,https://github.com/FasterXML/jackson-module-jsonSchema或其他。我通读了链接,但不了解如何使用对象映射器来替换此行: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是否具有相同的格式。

我更新的班级:

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

但是,我在注释行中得到了空指针异常。我认为这与注入或嘲笑对象有关,但我不确定应该如何解决此问题。

java json jackson sonarqube deprecated
1个回答
4
投票
从版本ISO8601DateFormat开始不推荐使用

ISO8601DateFormatISO8601Utils,并且不提供明确的文档说明不推荐使用的操作。 GitHub上有一个问题:ISO8601Utils与您的问题有关。

[我猜,您团队中的某人只是使用了他/她发现将2.9序列化为ISO8601DateFormat is deprecated but replacement is unclear格式的头等舱,却没有引起很大的注意,它来自Date库。我建议不要使用由于其他原因而使用的库中的内部ISO8601类。在这种情况下,对于Jackson /*Utilsserialising。现在,我们有许多选项可以设置日期格式,因此您应该为自己选择一个。通常,这取决于您使用哪个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

有关更多信息,请阅读:

  1. true
  2. yyyy-MM-ddThh:mm:ss.sss'Z'
© www.soinside.com 2019 - 2024. All rights reserved.