我使用JSON Schema Validator(https://github.com/fge/json-schema-validator)来验证这个简单对象的内容:
{
"firstName": "Bob",
"lastName": "Smith",
"age": 23
}
我的架构由以下人员定义:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"age": {
"type": "integer"
}
},
"required": ["firstName", "lastName", "age"]
}
我编写了以下代码来使用模式验证对象:
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
// Create the source configuration file
ObjectNode sourceConfig = mapper.createObjectNode();
sourceConfig.put("firstName", "Bob");
sourceConfig.put("lastName", "Smith");
sourceConfig.put("age", 23);
// Create the schema used for validation
ObjectNode schema = mapper.createObjectNode();
schema.put("$schema", "http://json-schema.org/draft-04/schema#");
schema.put("type", "object");
ObjectNode properties = mapper.createObjectNode();
ObjectNode firstName = mapper.createObjectNode();
firstName.put("type", "string");
properties.put("firstName", firstName);
ObjectNode lastName = mapper.createObjectNode();
lastName.put("type", "string");
properties.put("lastName", lastName);
ObjectNode age = mapper.createObjectNode();
age.put("type", "integer");
properties.put("age", age);
List<String> required = new ArrayList<String>();
required.add("firstName");
required.add("lastName");
required.add("age");
// Convert the list to an arraynode and add to schema
ArrayNode array = mapper.valueToTree(required);
schema.putArray("required").addAll(array);
schema.put("properties", properties);
JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
JsonSchema schemaValidation = null;
try {
schemaValidation = factory.getJsonSchema(schema);
}
catch (ProcessingException e) {
e.printStackTrace();
}
ProcessingReport report = null;
try {
report = schemaValidation.validate(sourceConfig);
}
catch (ProcessingException e) {
e.printStackTrace();
}
}
这是我运行程序时出现的错误:
Exception in thread "main" java.lang.NoSuchFieldError: WRITE_BIGDECIMAL_AS_PLAIN
at com.github.fge.jackson.JacksonUtils.newMapper(JacksonUtils.java:155)
at com.github.fge.jackson.JacksonUtils.<clinit>(JacksonUtils.java:55)
at com.github.fge.jackson.JsonNodeReader.<init>(JsonNodeReader.java:82)
at com.github.fge.jackson.JsonLoader.<clinit>(JsonLoader.java:50)
at com.github.fge.jsonschema.SchemaVersion.<init>(SchemaVersion.java:63)
at com.github.fge.jsonschema.SchemaVersion.<clinit>(SchemaVersion.java:44)
at com.github.fge.jsonschema.core.load.configuration.LoadingConfigurationBuilder.<init>(LoadingConfigurationBuilder.java:117)
at com.github.fge.jsonschema.core.load.configuration.LoadingConfiguration.byDefault(LoadingConfiguration.java:151)
at com.github.fge.jsonschema.main.JsonSchemaFactoryBuilder.<init>(JsonSchemaFactoryBuilder.java:67)
at com.github.fge.jsonschema.main.JsonSchemaFactory.newBuilder(JsonSchemaFactory.java:123)
at com.github.fge.jsonschema.main.JsonSchemaFactory.byDefault(JsonSchemaFactory.java:113)
at PlayingWithJsonSchemaValidation.main(PlayingWithJsonSchemaValidation.java:114)
当我通过调试器运行程序时,JsonSchemaFactory factory = JsonSchemaFactory.byDefault();我不确定如何修复此错误,或者代码究竟出了什么问题。有人可以给我一些解决方法吗?