Json-Schema-Validator库:

时间:2016-12-11 03:31:06

标签: java json json-schema-validator

我使用库https://github.com/daveclayton/json-schema-validator(@daveclayton)来验证我生成的json对jsonschema。

private static boolean validateTestdataAgainstSchema(String testdata) throws IOException{
    try{
         ObjectMapper mapper = new ObjectMapper();
         SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();
         mapper.acceptJsonFormatVisitor(Configuration.getBaseBeanClass(), visitor);
         JsonSchema schema = visitor.finalSchema();
         FileSystem.writeToFile("schema.json",mapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema));
         final File jsonSchemaFile = new File("schema.json");
         final URI uri = jsonSchemaFile.toURI();   

       JsonNode data = JsonLoader.fromString(testdata);

       JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
       com.github.fge.jsonschema.main.JsonSchema schemaa = factory.getJsonSchema(uri.toString());
       ProcessingReport report = schemaa.validate(data);
       return report.isSuccess();
    }catch(Exception ex){
        ex.printStackTrace();
    }
    return false;
}

我在验证时遇到以下异常。有人可以帮忙吗

com.github.fge.jsonschema.core.exceptions.ProcessingException: fatal: URI scheme "urn" not supported (URI: "urn:jsonschema:au:com:superchoice:services:enterprise:domain:i6:amount:NonNegativeAmount#")
level: "fatal"
scheme: "urn"
uri: "urn:jsonschema:au:com:superchoice:services:enterprise:domain:i6:amount:NonNegativeAmount#"

JSONSchema

JSONData

0 个答案:

没有答案