用java 代码编写出 : 使用jsonSchema 校验数据
时间: 2023-03-16 07:44:30 浏览: 238
答:以下是用于使用JSONSchema校验数据的Java代码:import com.github.fge.jsonschema.main.JsonSchemaFactory;import com.github.fge.jsonschema.report.ProcessingReport;import com.github.fge.jsonschema.core.exceptions.ProcessingException;import com.github.fge.jsonschema.core.report.LogLevel;import com.github.fge.jsonschema.core.report.ProcessingMessage;import java.io.File;public class JsonSchemaValidator { public static boolean validate(String jsonSchemaPath, File jsonDataFile) throws ProcessingException {
JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
ProcessingReport report = factory.getSyntaxValidator().validateSchema(jsonSchemaPath);
if(!report.isSuccess()) {
for (ProcessingMessage msg : report) {
if(msg.getLogLevel() == LogLevel.ERROR || msg.getLogLevel() == LogLevel.FATAL) {
System.err.println(msg);
return false;
}
}
}
report = factory.getValidator().validate(jsonSchemaPath, jsonDataFile);
if(report.isSuccess()) {
return true;
}
for (ProcessingMessage msg : report) {
System.err.println(msg);
}
return false;
}
}
阅读全文