【问题标题】:jackson/json schema - replacing ISO8601Utils with the external json schema generator modulejackson/json 模式 - 用外部 json 模式生成器模块替换 ISO8601Utils
【发布时间】:2019-02-19 17:28:50
【问题描述】:

我正在替换 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();
    }
}

任何帮助将不胜感激!

附:我正在编写一个单元测试来验证 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));
  }

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

【问题讨论】:

  • 您的问题似乎错过了您正在尝试做什么以及您如何尝试这样做,并跳入了当我们不知道您在尝试什么时我该如何执行此方法与它有关。你最好解释一下你想做什么。
  • 感谢您的回复!我只是想用新替换的类替换已弃用的 ISO8610Utils 类。有人告诉我使用外部 json 模式生成器模块来代替 ISO8601Utils 的使用。我通读了我提供的链接,但我不明白这将如何替换 SonarQube 发出警告的行,即String value = ISO8601Utils.format(date, true);。我想用新行替换这一行,这样 SonarQube 就不会再抛出错误并且代码编译正常。最终,我现在只是想清理 SonarQube 错误。
  • 但这没有任何意义 JSON Schema 是一种定义 JSON 对象结构的方法,非常适合于验证传输的数据,类似于 XML 的 SOAP。如果是这种情况,它似乎只是在尝试日期格式,为什么不使用默认的java.util.Datestackoverflow.com/questions/18480633/…

标签: java json jackson sonarqube deprecated


【解决方案1】:

ISO8601DateFormatISO8601Utils 自版本 2.9 起已弃用,并且不提供明确的文档说明弃用后的操作。 GitHub 上有一个问题:ISO8601DateFormat is deprecated but replacement is unclear 与您的问题相关。

我猜,您团队中的某个人刚刚使用了他/她发现的将Date 序列化为ISO8601 格式的第一类,并且没有引起人们对它来自Jackson 库的关注。我建议不要使用您出于其他原因使用的库中的内部 *Utils 类。在这种情况下为serialising/deserialisingJSON。现在,我们有很多选择如何格式化日期,您应该为自己选择一个。大多数情况下,这取决于您使用哪个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?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-24
    • 2018-09-08
    • 1970-01-01
    • 1970-01-01
    • 2018-06-30
    • 2015-09-19
    • 2017-02-22
    相关资源
    最近更新 更多