[英]How to load JSON Schema file from java
My Project is a maven project and inside resources folder - src/main/resources folder I have a json schema file - "jsonschema.json "我的项目是一个 Maven 项目,在资源文件夹 - src/main/resources 文件夹中我有一个 json 模式文件 - “jsonschema.json”
package : src/main/resources包:src/main/resources
file : jsonschema.json文件:jsonschema.json
Now i want to validate my jsonobject with json schema现在我想用 json 模式验证我的 jsonobject
How to load the schema.json file in to the code :如何将 schema.json 文件加载到代码中:
Is the below line correct?下面的行是否正确?
JsonNode schema = JsonLoader.fromResource("/jsonschema.json"); // correct? or correct me
JsonNode data = JsonLoader.fromString(jsonData);
ProcessingReport report = validator.validate(schema, data);
This may help you这可能会帮助你
Place jsonschema file on project root directory or in resource and read schema using normal file read and store it in variable say str
将 jsonschema 文件放在项目根目录或资源中,并使用普通文件读取读取模式并将其存储在变量中,例如
str
booleab isValidRequest=false;
String requestData; // data to validate
String str; //schema
JsonNode requestDataJsonNode = com.github.fge.jackson.JsonLoader.fromString(requestData);
final JsonNode schemaNode = JsonLoader.fromString(str);
final JsonNode schemaNode=JsonLoader.fromResource("/jsonschema.json"); // for your query
final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
JsonValidator validator = factory.getValidator();
ProcessingReport processingReport= validator.validate(schemaNode, requestDataJsonNode);
if(processingReport!=null)
{
isValidRequest=processingReport.isSuccess();
}
} catch (Exception e) {
}
If You are getting exception while executing the program.如果您在执行程序时遇到异常。 add dependencies listed in below [link]
添加下面列出的依赖项[链接]
http://mvnrepository.com/artifact/com.github.fge/json-schema-validator/2.2.5 http://mvnrepository.com/artifact/com.github.fge/json-schema-validator/2.2.5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.