杰克逊JSON模式日期

gal*_*ben 2 java json jsonschema jackson

我想生成一个带有java.util.Date字段的类的JSON 模式。对于类型为日期的字段,我将得到:

“ fieldName”:{“ type”:“ integer”,“ format”:“ UTC_MILLISEC”}

我想要的是这样的:

“ fieldName”:{“ type”:“ string”,“ format”:“ date-time”}

我希望此配置对于所有POJOS都是全局的,而不仅仅是对特定的POJO。因此,对特定班级的注释对我没有帮助。

谢谢!

Ale*_*lov 5

这只是Dennis答案的一个示例,示例表明Jackson模式生成器SerializationFeature.WRITE_DATES_AS_TIMESTAMPS实际上已将其考虑在内。

public class JacksonSchema1 {

    public static class Bean {
        public String name;
        public Date date;
    }

    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();
        mapper.acceptJsonFormatVisitor(mapper.constructType(Bean.class), visitor);
        JsonSchema jsonSchema = visitor.finalSchema();
        System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonSchema));
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

{
  "type" : "object",
  "properties" : {
    "name" : {
      "type" : "string"
    },
    "date" : {
      "type" : "string",
      "format" : "DATE_TIME"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)