在为Java类型生成模式时,我想指定某些String值(或其他类型)可以为null或" Nullable",例如,如果有" name& #34;有这个要求的财产我想在输出中看到这样的东西:
"name" : { "type" : ["string", "null"] }
此外,如果我想允许值为整数或字符串,该怎么办?杰克逊的图式生成怎么能被告知呢?我的意思是这样的:
"id" : { "type" : ["string", "integer"] "required" : true }
但到目前为止,我与杰克逊一起尝试的事情并没有奏效。我有以下方法,在运行时将为输入类型创建一个JsonSchema:
public String getJsonSchema(Class<Message> type) throws IOException{
ObjectMapper m = new ObjectMapper();
JsonSchemaGenerator generator = new JsonSchemaGenerator(m);
JsonSchema schema = generator.generateSchema(type);
return m.writerWithDefaultPrettyPrinter().writeValueAsString(schema);
}
Message.java是这样的(我删除了setter以保持代码更短):
public class Message {
private String name;
private Long id;
@JsonProperty(value = "name", required = false)
@JsonPropertyDescription("User provided name.")
public String getName() {
return name;
}
@JsonProperty(value = "id", required = true)
public Long getId() {
return id;
}
}
前一个bean的输出是:
{ "type" : "object", "id" : "urn:jsonschema:spring:integration:quickstart:Message", "properties" : { "name" : { "type" : "string", "description" : "User provided name." }, "id" : { "type" : "integer", "required" : true } } }
问题是,需要使用哪种注释或配置选项&#34; null&#34;名称的可能类型之一?