何为Json-Schema
Json-schema是描述你的JSON数据格式;JSON模式(应用程序/模式+ JSON)有多种用途,其中之一就是实例验证。验证过程可以是交互式或非交互式的。例如,应用程序可以使用JSON模式来构建用户界面使互动的内容生成除了用户输入检查或验证各种来源获取的数据。(来自百度百科)
相关jar包
<dependency>
<groupId>com.github.fge</groupId>
<artifactId>json-schema-validator</artifactId>
<version>2.2.6</version>
</dependency>
<!-- fasterxml -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.3.0</version>
</dependency>
package com.gaci;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jackson.JsonLoader;
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
import com.github.fge.jsonschema.core.report.ProcessingMessage;
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.main.JsonSchema;
import com.github.fge.jsonschema.main.JsonSchemaFactory;
import org.apache.commons.lang.ArrayUtils;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.Iterator;
/**
-
Created by Gaci on 2020/8/3.
*/
public class JSONSchemaUtil {
// 创建订单请求JSON格式校验
private static String schema;static {
// 获取创建订单格式校验 try { String str = "";
// String filePath = JSONSchemaUtil.class.getResource("/schema.json").getPath();// src目录下
// filePath = filePath.substring(1);
// InputStream in = new FileInputStream(new File(filePath));
InputStream in = new FileInputStream(new File(“E:\schema.json”));
BufferedReader reader = new BufferedReader(new InputStreamReader(in,“UTF-8”));
String line;
while ((line = reader.readLine()) != null) {
str += line;
}
schema = str;
// System.out.println(schema);
} catch (Exception e) {
e.printStackTrace();
}
}
private final static JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
/**
* 校验创建订单请求的格式
* @param mainSchema
* @param instance
* @return
* @throws IOException
* @throws ProcessingException
*/
// public static ProcessingReport validatorSchema(String mainSchema, String instance) throws