从 java 中的 pojo 生成 json 模式 - 自定义日期类型
Generate json schema from pojo in java - custom date type
我正在使用 https://github.com/mbknor/mbknor-jackson-jsonSchema 生成 json 架构,但是当我的对象包含 LocalDate 时,LocalDate 将如下所示:
"LocalDate" : {
"type" : "object",
"additionalProperties" : false,
"properties" : {
"year" : {
"type" : "integer"
},
"month" : {
"type" : "string",
"enum" : [ "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER" ]
},
"era" : {
"$ref" : "#/definitions/Era"
},
"dayOfYear" : {
"type" : "integer"
},
"dayOfWeek" : {
"type" : "string",
"enum" : [ "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY", "SUNDAY" ]
},
"leapYear" : {
"type" : "boolean"
},
"dayOfMonth" : {
"type" : "integer"
},
"monthValue" : {
"type" : "integer"
},
"chronology" : {
"$ref" : "#/definitions/IsoChronology"
}
},
"required" : [ "year", "dayOfYear", "leapYear", "dayOfMonth", "monthValue" ]
},
"Era" : {
"type" : "object",
"additionalProperties" : false,
"properties" : {
"value" : {
"type" : "integer"
}
},
"required" : [ "value" ]
},
"IsoChronology" : {
"type" : "object",
"additionalProperties" : false,
"properties" : {
"calendarType" : {
"type" : "string"
},
"id" : {
"type" : "string"
}
}
}
有人可以帮助我如何将 LocalDate type 更改为字符串并添加字段 format 这将是日期?
我的代码在 groovy 中,因为我正在编写 groovy 插件:
ObjectMapper mapper = new ObjectMapper()
JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator(mapper)
JsonNode schema = jsonSchemaGenerator.generateJsonSchema(MyClass.class)
我希望我的 LocalDate 字段看起来像这样:
"MyField": {
"type": "string",
"format": "date"
}
感谢您的帮助。
您可以配置客户序列化程序并在该序列化程序中序列化 LocalDate
。例如(我从他们的 github 自述文件中复制了一个片段);
@JsonSerialize(using = MySpecialSerializer.class)
@JsonSchemaInject( json = "{\"//your schema here\"}" )
public class MyPojo {
private LocalDate localDate;
public LocalDate getLocalDate() {
return localDate;
}
//and the rest the class
}
public class MySpecialSerializer extends JsonSerializer<MyPojo>
{
@Override
public void serialize(final MyPojo myPojo,
final JsonGenerator gen,
final SerializerProvider serializers)
throws IOException
{
gen.writeObject(localDate.format(DateTimeFormatter.ISO_DATE));
//and the other field serialization
}
}
如果需要,您也可以使用 jackson 的 java-8 日期模块。
您可以告诉模式生成器您想要在模式中声明某种类型,就好像它们是另一种类型一样。所以你可以说你想将每个 LocalDate 声明为一个字符串。
为此,您需要创建一个 JsonSchemaConfig
对象并将其传递给 JsonSchemaGenerator
构造函数。
在 classReMapping
映射中,您可以将类型重新映射到其他类型。
Map<Class<?>, Class<?>> classTypeReMapping = new HashMap<>();
classTypeReMapping.put(LocalDate.class, String.class);
可选地,在 typeToFormatMapping
映射中,您可以将类型映射到 format
注释。您为 LocalDate 使用的格式正是 JSON 架构规范中定义的 date
格式:
Map<String, String> typeToFormatMapping = new HashMap<>();
typeToFormatMapping.put(LocalDate.class.getName(), "date");
构建完整的 JsonSchemaConfig:
boolean autoGenerateTitleForProperties = false;
String defaultArrayFormat = null;
boolean useOneOfForOption = true;
boolean useOneOfForNullables = false;
boolean usePropertyOrdering = false;
boolean hidePolymorphismTypeProperty = false;
boolean disableWarnings = false;
boolean useMinLengthForNotNull = false;
boolean useTypeIdForDefinitionName = false;
boolean useMultipleEditorSelectViaProperty = false;
Set<Class<?>> uniqueItemClasses = Collections.emptySet();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
Map<Class<?>, Class<?>> classTypeReMapping = new HashMap<>();
classTypeReMapping.put(LocalDate.class, String.class);
// #####****##### Add remapped types here
Map<String, String> typeToFormatMapping = new HashMap<>();
typeToFormatMapping.put(LocalDate.class.getName(), "date");
// #####****##### (optional) Add format annotations for types here
JsonSchemaConfig config = JsonSchemaConfig.create(
autoGenerateTitleForProperties,
Optional.ofNullable(defaultArrayFormat),
useOneOfForOption,
useOneOfForNullables,
usePropertyOrdering,
hidePolymorphismTypeProperty,
disableWarnings,
useMinLengthForNotNull,
useTypeIdForDefinitionName,
typeToFormatMapping,
useMultipleEditorSelectViaProperty,
uniqueItemClasses,
classTypeReMapping,
Collections.emptyMap()
)
构建 JsonSchemaGenerator:
JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator(objectMapper, config);
Class<?> mainClassObject = ...;
JsonNode jsonSchema = jsonSchemaGenerator.generateJsonSchema(mainClassObject);
我正在使用 https://github.com/mbknor/mbknor-jackson-jsonSchema 生成 json 架构,但是当我的对象包含 LocalDate 时,LocalDate 将如下所示:
"LocalDate" : {
"type" : "object",
"additionalProperties" : false,
"properties" : {
"year" : {
"type" : "integer"
},
"month" : {
"type" : "string",
"enum" : [ "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER" ]
},
"era" : {
"$ref" : "#/definitions/Era"
},
"dayOfYear" : {
"type" : "integer"
},
"dayOfWeek" : {
"type" : "string",
"enum" : [ "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY", "SUNDAY" ]
},
"leapYear" : {
"type" : "boolean"
},
"dayOfMonth" : {
"type" : "integer"
},
"monthValue" : {
"type" : "integer"
},
"chronology" : {
"$ref" : "#/definitions/IsoChronology"
}
},
"required" : [ "year", "dayOfYear", "leapYear", "dayOfMonth", "monthValue" ]
},
"Era" : {
"type" : "object",
"additionalProperties" : false,
"properties" : {
"value" : {
"type" : "integer"
}
},
"required" : [ "value" ]
},
"IsoChronology" : {
"type" : "object",
"additionalProperties" : false,
"properties" : {
"calendarType" : {
"type" : "string"
},
"id" : {
"type" : "string"
}
}
}
有人可以帮助我如何将 LocalDate type 更改为字符串并添加字段 format 这将是日期?
我的代码在 groovy 中,因为我正在编写 groovy 插件:
ObjectMapper mapper = new ObjectMapper()
JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator(mapper)
JsonNode schema = jsonSchemaGenerator.generateJsonSchema(MyClass.class)
我希望我的 LocalDate 字段看起来像这样:
"MyField": {
"type": "string",
"format": "date"
}
感谢您的帮助。
您可以配置客户序列化程序并在该序列化程序中序列化 LocalDate
。例如(我从他们的 github 自述文件中复制了一个片段);
@JsonSerialize(using = MySpecialSerializer.class)
@JsonSchemaInject( json = "{\"//your schema here\"}" )
public class MyPojo {
private LocalDate localDate;
public LocalDate getLocalDate() {
return localDate;
}
//and the rest the class
}
public class MySpecialSerializer extends JsonSerializer<MyPojo>
{
@Override
public void serialize(final MyPojo myPojo,
final JsonGenerator gen,
final SerializerProvider serializers)
throws IOException
{
gen.writeObject(localDate.format(DateTimeFormatter.ISO_DATE));
//and the other field serialization
}
}
如果需要,您也可以使用 jackson 的 java-8 日期模块。
您可以告诉模式生成器您想要在模式中声明某种类型,就好像它们是另一种类型一样。所以你可以说你想将每个 LocalDate 声明为一个字符串。
为此,您需要创建一个 JsonSchemaConfig
对象并将其传递给 JsonSchemaGenerator
构造函数。
在 classReMapping
映射中,您可以将类型重新映射到其他类型。
Map<Class<?>, Class<?>> classTypeReMapping = new HashMap<>();
classTypeReMapping.put(LocalDate.class, String.class);
可选地,在 typeToFormatMapping
映射中,您可以将类型映射到 format
注释。您为 LocalDate 使用的格式正是 JSON 架构规范中定义的 date
格式:
Map<String, String> typeToFormatMapping = new HashMap<>();
typeToFormatMapping.put(LocalDate.class.getName(), "date");
构建完整的 JsonSchemaConfig:
boolean autoGenerateTitleForProperties = false;
String defaultArrayFormat = null;
boolean useOneOfForOption = true;
boolean useOneOfForNullables = false;
boolean usePropertyOrdering = false;
boolean hidePolymorphismTypeProperty = false;
boolean disableWarnings = false;
boolean useMinLengthForNotNull = false;
boolean useTypeIdForDefinitionName = false;
boolean useMultipleEditorSelectViaProperty = false;
Set<Class<?>> uniqueItemClasses = Collections.emptySet();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
Map<Class<?>, Class<?>> classTypeReMapping = new HashMap<>();
classTypeReMapping.put(LocalDate.class, String.class);
// #####****##### Add remapped types here
Map<String, String> typeToFormatMapping = new HashMap<>();
typeToFormatMapping.put(LocalDate.class.getName(), "date");
// #####****##### (optional) Add format annotations for types here
JsonSchemaConfig config = JsonSchemaConfig.create(
autoGenerateTitleForProperties,
Optional.ofNullable(defaultArrayFormat),
useOneOfForOption,
useOneOfForNullables,
usePropertyOrdering,
hidePolymorphismTypeProperty,
disableWarnings,
useMinLengthForNotNull,
useTypeIdForDefinitionName,
typeToFormatMapping,
useMultipleEditorSelectViaProperty,
uniqueItemClasses,
classTypeReMapping,
Collections.emptyMap()
)
构建 JsonSchemaGenerator:
JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator(objectMapper, config);
Class<?> mainClassObject = ...;
JsonNode jsonSchema = jsonSchemaGenerator.generateJsonSchema(mainClassObject);