gpt4 book ai didi

java - 从 pojo : how do I add "description" automatically? 生成 JsonSchema

转载 作者:搜寻专家 更新时间:2023-10-31 19:55:04 25 4
gpt4 key购买 nike

我正在尝试从我的项目中的 pojos 自动生成 JsonSchema:代码如下所示:

ObjectMapper mapper = new ObjectMapper();
SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();
mapper.acceptJsonFormatVisitor(clazz, visitor);
JsonSchema jsonSchema = visitor.finalSchema();
String schemaString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonSchema);

当clazz这样定义时:

public class ZKBean
{
public String anExample;
public int anInt;
}

I end up with this:

{
"type" : "object",
"id" : "urn:jsonschema:com:emc:dpad:util:ZKBean",
"properties" : {
"anInt" : {
"type" : "integer"
},
"anExample" : {
"type" : "string"
}
}
}

这一切都很棒。我想要做的是将“描述”键添加到模式中,这样我就有了类似的东西:

{
"type" : "object",
"id" : "urn:jsonschema:com:emc:dpad:util:ZKBean",
"properties" : {
"anInt" : {
"type" : "integer",
"description" : "Represents the number of foos in the system"
},
"anExample" : {
"type" : "string",
"description" : "Some descriptive description goes here"
}
}
}

我假设有一些注释可以放在我的 ZKBean 类的字段上,但是经过半天的 futzing 我没有找到。这是要走的路吗?或者我需要对访客做些什么吗?

谢谢,杰西

最佳答案

您可以使用 @JsonPropertyDescription 注释来生成自 Jackson 2.4.1 起有效的 json 模式。这是一个例子:

public class JacksonSchema {
public static class ZKBean {
@JsonPropertyDescription("This is a property description")
public String anExample;
public int anInt;
}

public static void main(String[] args) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();
mapper.acceptJsonFormatVisitor(ZKBean.class, visitor);
JsonSchema jsonSchema = visitor.finalSchema();
System.out.println(mapper
.writerWithDefaultPrettyPrinter().writeValueAsString(jsonSchema));
}
}

输出:

{
"type" : "object",
"id" : "urn:jsonschema:stackoverflow:JacksonSchema:ZKBean",
"properties" : {
"anExample" : {
"type" : "string",
"description" : "This is a property description"
},
"anInt" : {
"type" : "integer"
}
}
}

关于java - 从 pojo : how do I add "description" automatically? 生成 JsonSchema,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24515917/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com