【问题标题】:jsonschema and date typejsonschema 和日期类型
【发布时间】:2015-10-05 15:24:13
【问题描述】:

我刚刚开始使用 jsonschema 和下面的示例 “在 Java 项目中使用 jsonschema2pojo(嵌入式)” 在 https://github.com/joelittlejohn/jsonschema2pojo/wiki/Getting-Started

记住此处列出的 jsonschema 的数据类型 https://developers.google.com/discovery/v1/type-format?hl=en

我的模式对象可以描述为

{    
    "$schema": "http://json-schema.org/draft-04/schema",
    "description": "Document",
    "type": "object",

    "properties": {
        "displayDate": { "type": "date" },
        "displayName": { "type": "string" }
    }
}

不幸的是,生成的 Pojo 对象将是

package com.example;

public interface Document {

   java.lang.Object getDisplayDate();

   void setDisplayDate(java.lang.Object arg0);

   java.lang.String getDisplayName();

   void setDisplayName(java.lang.String arg0);

}

有一个对象类型的成员“displayDate”,而不是预期的日期。为什么?

【问题讨论】:

  • 我觉得应该是约会。
  • 是的,但是为什么对象会出来?
  • 如果你使用"type": "object", "javaType": "java.util.Date而不是"type": "date"呢?
  • 它导致:线程“main”中的异常 java.lang.NoClassDefFoundError: japa/parser/ParseException at org.jsonschema2pojo.rules.ObjectRule.createClass(ObjectRule.java:235) at org.jsonschema2pojo .rules.ObjectRule.apply(ObjectRule.java:99) at org.jsonschema2pojo.rules.ObjectRule.apply(ObjectRule.java:66)

标签: java json pojo jsonschema


【解决方案1】:

date 不是 type 的有效值。 displayDate 应该定义为

{ "type": "string", "format": "date" }

我不知道 jsonschema2pojo 是否会将其转换为您想要的 Date 对象,但它似乎默认为 Object 而不是在遇到 type 的无效值时抛出错误。

【讨论】:

  • 是的,这是一个进步,因为将 "displayDate": { "type": "date" }, 替换为 "displayDate": { "type": "string", "format": "日期”},结果为:code Exception in thread "main" java.lang.NoClassDefFoundError: org/joda/time/LocalDate at org.jsonschema2pojo.rules.FormatRule.getDateOnlyType(FormatRule.java:135) (...) Caused by: java.lang.ClassNotFoundException: org.joda.time.LocalDate at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
  • 好吧,你的解决方案是正确的。只需将 joda-time-2.0.jar 添加到 buildpath 并在编译到 classpath 时。谢谢
【解决方案2】:

根据最新的 jsonschema2pojo 文档,对于 Date 类型,您需要执行以下操作:-

{ "type": "string", "format": "date-time" }

在生成的 POJO 中,属性类型为 Date 对象

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-24
  • 1970-01-01
  • 2014-05-17
相关资源
最近更新 更多