我打算将我的POJO
转换为JSON Schema
。
在现有的POJO
中,我们有codehaus
包中的注释:@JsonProperty("address")
,其中相应的导入为:
import org.codehaus.jackson.annotate.JsonProperty;
我无法使用codehaus
api生成架构,因为我们具有递归JSON
结构并且得到StackOverflowError
。
所以,我尝试使用fasterxml
的jackson-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);
如果可能,只需将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:我知道这不是理想的答案(它不包含直接解决方案,但是我想向您指出一些您应该从中开始解决问题的主题,对此,评论太小了。