如何删除 id 字段("id": "urn:jsonschema:org:gradle:Person") 来自使用 Jackson 创建的 JSON 模式?
生成的架构
{
"type" : "object",
"id" : "urn:jsonschema:org:gradle:Person",
"properties" : {
"name" : {
"type" : "string"
}
}
}
对于 POJO 类(Person.class)
import com.fasterxml.jackson.annotation.JsonProperty;
public class Person {
@JsonProperty("name")
private String name;
}
使用 JSON 架构生成器
import java.io.IOException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.jsonSchema.JsonSchema;
import com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator;
public final class GetJsonSchema {
public static String getJsonSchema2(Class clazz) throws IOException {
ObjectMapper mapper = new ObjectMapper();
JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator(mapper);
JsonSchema jsonSchema = jsonSchemaGenerator.generateSchema(clazz);
return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonSchema);
}
}
像这样调用
System.out.println(JsonSchema.Create(Person.class));
最佳答案
只需将 id 设置为 null
即可。
例如:
jsonSchema.setId(null);
关于java - 使用 jackson 从 POJO 创建 JSON 模式时,从 JSON 模式中删除 "id",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41114170/