gpt4 book ai didi

java - 从java中的pojo生成json模式 - 自定义日期类型

转载 作者:行者123 更新时间:2023-11-30 01:55:09 24 4
gpt4 key购买 nike

我正在使用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 声明为字符串。

为此,您需要创建一个 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);

关于java - 从java中的pojo生成json模式 - 自定义日期类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54796877/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com