【问题标题】:How to add description attribute while generating JSON schema from POJOs using codehaus package classes如何在使用 codehaus 包类从 POJO 生成 JSON 模式时添加描述属性
【发布时间】:2019-10-25 19:38:01
【问题描述】:

我打算将我的POJOs 转换为JSON Schema

在现有的POJOs 中,我们有来自codehaus 包的注释: @JsonProperty("address") 其中对应的导入是:

import org.codehaus.jackson.annotate.JsonProperty;

我不能使用codehaus api 来生成架构,因为我们有一个递归的JSON 结构,我得到StackOverflowError

所以,我尝试使用fasterxmljackson-module-jsonschema API 来做到这一点,效果很好。

我得到的示例输出:

"Registration" : {
      "type" : "object",
      "id" : "urn:jsonschema:com:xyz.abc.Address",
}

我有两个要求:

  • 除了“type”和“id”,我还想要一个description属性。我可以添加@JsonPropertyDescription 属性,这会起作用,但是每个属性都会有一个来自codehaus 的注释和另一个来自fasterxml 包的注释。 codehaus 中是否有可用于此目的的等效注解?

  • 有没有办法在“id”属性中只使用非限定类名(只有“地址”,即没有限定的对象路径“xyz.abc.Address”并且没有“urn:jsonschema” ) ?

使用fasterxml 生成模式的代码:

ObjectMapper mapper = new ObjectMapper();
JsonSchemaGenerator schemaGen = new JsonSchemaGenerator(mapper);
JsonSchema schema = schemaGen.generateSchema(DashboardDef.class);
String generatedJsonSchema = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema);

【问题讨论】:

  • 您的问题解决了吗?下面的答案有帮助吗?

标签: java json jackson jsonschema fasterxml


【解决方案1】:

如果可能的话,只需将codehaus 注释替换为fasterxml 一个。 codehaus 不再受支持,应避免使用。欲了解更多信息,请查看:org.codehaus.jackson versus com.fasterxml.jackson.core

如果你真的想深入研究这两个库的项目,你应该看看AnnotationIntrospector 类,它允许额外的配置和链接许多不同的库与Jackson 框架。查看类似问题:How to make fasterxml ObjectMapper work with codehaus annotations

要操作JSON Schema,您可以实现自己的SchemaFactoryWrapper。另见:

PS:我知道这不是理想的答案(它不包含直接解决方案),但我想指出一些你应该从解决问题开始的主题,而 cmets 太小了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-22
    • 1970-01-01
    • 2016-10-16
    • 2019-06-23
    • 2016-02-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多