如何使用codehaus包类从POJO生成JSON模式时添加描述属性

问题描述 投票:1回答:1

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

在现有的POJO中,我们有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个回答
0
投票

如果可能,只需将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:我知道这不是理想的答案(它不包含直接解决方案,但是我想向您指出一些您应该从中开始解决问题的主题,对此,评论太小了。

© www.soinside.com 2019 - 2024. All rights reserved.