1、使用Maven引用mbknor-jackson-jsonschema_2.12
<dependency>
<groupId>com.kjetland</groupId>
<artifactId>mbknor-jackson-jsonschema_2.12</artifactId>
<version>1.0.28</version>
</dependency>
2、生成JsonSchema("additionalProperties" : false)
1)需要import的包
import com.fasterxml.jackson.databind.JsonNode;import com.fasterxml.jackson.databind.ObjectMapper;import com.kjetland.jackson.jsonSchema.JsonSchemaConfig;import com.kjetland.jackson.jsonSchema.JsonSchemaGenerator;
2)生成JsonSchema代码
ObjectMapper objectMapper = new ObjectMapper();JsonSchemaConfig config = JsonSchemaConfig.nullableJsonSchemaDraft4();JsonSchemaGenerator schemaGenerator = new JsonSchemaGenerator(objectMapper, config);JsonNode jsonNode = schemaGenerator.generateJsonSchema(Test.class);String jsonSchemaText = jsonNode.toString();
3)生成的jsonSchemaText
Test.java:
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class Test {
@JsonProperty(required = true)
private final String name;
private final TestChild child;
@JsonCreator
public Test (
@JsonProperty("name") String name,
@JsonProperty("child") TestChild child) {
this.name = name;
this.child = child;
}
public String getName () {
return name;
}
public TestChild getChild () {
return child;
}
}
TestChild.java:
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class TestChild {
@JsonProperty(required = true)
private final String childName;
@JsonCreator
public TestChild (@JsonProperty("childName") String childName) {
this.childName = childName;
}
public String getChildName () {
return childName;
}
}
jsonSchemaText:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Test",
"type": "object",
"additionalProperties": false,
"properties": {
"name": {
"type": "string"
},
"child": {
"oneOf": [
{
"type": "null",
"title": "Not included"
},
{
"$ref": "#/definitions/TestChild"
}
]
}
},
"required": [
"name"
],
"definitions": {
"TestChild": {
"type": "object",
"additionalProperties": false,
"properties": {
"childName": {
"type": "string"
}
},
"required": [
"childName"
]
}
}
}
免责声明:以上内容(如有图片或视频亦包括在内)有转载其他网站资源,如有侵权请联系删除
为你推荐
-
TV动画《入间同学入魔了》第三季制作确认 将于10月播出
根据西修原作制作的TV动画《入间同学入魔了》第三季宣布了将于2022年10月播出的消息。本作的宣传图也一并公开。在这次的新宣传图上,可以看到主人公入间和新角色巴巴托斯的样子。 《入间同学入魔了》作品讲述了被父母卖给了恶魔的14岁少年铃木入间在魔界学园生活的故事。TV动画第一季和第二季分别在20
-
华硕Z690主板发布 12代酷睿致胜神装
-
S9:ISGvsSPY 欧洲蛇咬下大白鲨赢得小组第一名!
{@SYS-PAGE-SPLIT@}
-
韩国画师冷门动物拟人作品
-
《幻塔》开服福利少?别被骗了 游戏里藏着大量白给的好东西
-
克苏鲁+伊藤润二《恐怖世界》2023年夏季推出1.0版 并登陆主机
-
iPhone15ProMax预计售价2万块,网曝有这些改变
加载中...