【问题标题】:Java Class to JSON custom Schema by AnnotationJava Class to JSON custom Schema by Annotation
【发布时间】:2019-06-10 09:56:47
【问题描述】:

我目前正在像这样生成我的 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。如果这是可能的,我有点迷茫,如果可以,我必须以哪种方式实现它。因此,如果有人能给我提示在哪里看它,将不胜感激!

【问题讨论】:

    标签: java jackson annotations jsonschema


    【解决方案1】:

    mbknor-jackson-jsonSchema 包将 java POJO 转换为 json 模式。它具有@JsonPropertyDescription 注释,可用于自定义架构。

    请注意,它只支持 json 模式的草案 4,直到现在。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多