我目前正在生成这样的 Json 架构:
class Item {
public int id;
public string name;
public string description;
}
ObjectMapper mapper = new ObjectMapper();
SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();
mapper.acceptJsonFormatVisitor(Item.class, visitor);
JsonSchema schema = visitor.finalSchema();
System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema));
打印内容:
{
"type": "object",
"id": "urn:jsonschema:de:foo:bar:User",
"properties": {
"id": { "type": "integer" },
"name": { "type": "string" },
"description": { "type": "string" }
}
}
现在我想从 Item 类的注释中向每种类型附加附加信息。我希望它提供有关如何显示该字段的输入的信息。
class User {
public int id;
public string name;
@MyJsonSchemaAnnotation(fieldName = "input", value = "textarea")
public string description;
}
这会给我:
{
"type": "object",
"id": "urn:jsonschema:de:foo:bar:User",
"properties": {
"id": { "type": "integer" },
"name": { "type": "string" },
"description": { "type": "string", "input": "textarea" }
}
}
我认为这类似于 @JsonDescription 或 JSR 303 Annotations。如果这可能的话,我有点迷失了,如果可以的话,我必须以哪种方式实现它。因此,如果有人能给我提示在哪里查看它,我将不胜感激!
最佳答案
mbknor-jackson-jsonSchema package 将 java POJO 转换为 json 模式。它有 @JsonPropertyDescription
注释,可用于自定义架构。
请注意,到目前为止,它仅支持 json 模式的草案 4。
关于通过注释将 Java 类转换为 JSON 自定义架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56524127/