https://json-schema.org/understanding-json-schema/index.html
JSON Schema是什么?
根据JSON Schema 规范的定义,JSON模式是一种JSON媒体类型,用于定义JSON数据的结构。JSON模式旨在定义JSON数据的验证、文档、超链接导航和交互控制
com.github.everit-org.json-schema
org.everit.json.schema
1.9.2
org.json
json
20180130
1. 首先需要有一个需要进行效验的实体类
2. 通过JSON.toJSONString将其转为实体类
3. getClass().getResourceAsStream是获取到json的配置文件
4. 将其变为new org.json.JSONObjec对象类型的数据,
5. 将我们 上面转为json的实体类变为 new JSONObjec
6. 通过schema.validate()去进行文件的效验,如果正确返回true,否则直接抛异常
7. 后面就是将异常翻译一下传给前端,让用户知道是哪个格式出错了
import com.alibaba.fastjson.JSON;
import com.bj.cy.Service.bradnApiService;
import com.bj.cy.enity.brand;
import com.bj.cy.utils.CheckAnalysisList;
import com.bj.cy.utils.CheckRowVo;
import com.bj.cy.utils.nety.test1;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.everit.json.schema.ValidationException;
import org.everit.json.schema.loader.SchemaLoader;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
@ApiOperation("检测数据效验")
@RequestMapping(value = "test2", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public Object test2(@RequestBody test1 test1) {
//这个是你打算验证的JSON
String userBehavioAudits = JSON.toJSONString(test1);
//这个就是你设定的标准JSON
InputStream inputStream = getClass().getResourceAsStream("/test.json");
org.json.JSONObject Schema = new org.json.JSONObject(new JSONTokener(inputStream));
org.everit.json.schema.Schema schema = SchemaLoader.load(Schema);
try {
JSONObject myJsonObject = new JSONObject(userBehavioAudits);
schema.validate(myJsonObject);
} catch (ValidationException e) {
String errorMessage = e.getAllMessages().toString().replace("not found", "为空值!");
String errorMessage1 = errorMessage.replace("is not a valid enum value", "不在枚举范围内!");
String errorMessage2 = errorMessage1.replace("expected maxLength:", "字段最大长度为:");
String errorMessage3 = errorMessage2.replace("actual:", "当前实际长度:");
String errorMessage4 = errorMessage3.replace("is not less or equal to ", "最大范围为为:");
String errorMessage5 = errorMessage4.replace("subject must not be valid against schema", "不可是以下枚举值");
return errorMessage5;
}
return "str";
}
@Data
public class test1 {
private String name;
private String sex;
private String filename;
}
{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "http://example.com/example.json",
"type": "object",
"additionalProperties": true,
"required": [
"name",
"sex",
"filename"
],
"properties": {
"name": {
"$id": "#/properties/name",
"type": "string",
"maxLength": 36,
"minLength": 10,
"not": {
"enum": [
"String",
"string"
]
}
},
"sex": {
"$id": "#/properties/sex",
"type": "string",
"not": {
"enum": [
"String",
"string"
]
}
},
"filename": {
"$id": "#/properties/filename",
"type": "string",
"not": {
"enum": [
"String",
"string"
]
}
}
}
}
@ApiOperation("检测数据效验")
@RequestMapping(value = "test3", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public Object test3(@RequestBody List test1) {
ListerrLis=new ArrayList<>();
//这个是你打算验证的JSON
String userBehavioAudits = JSON.toJSONString(test1);
//因为是数组所以要转为Jsonarrat类型的
JSONArray array = new JSONArray(userBehavioAudits);
//这个就是你设定的标准JSON
InputStream inputStream = getClass().getResourceAsStream("/test.json");
org.json.JSONObject Schema = new org.json.JSONObject(new JSONTokener(inputStream));
org.everit.json.schema.Schema schema = SchemaLoader.load(Schema);
for (int i = 0; i < test1.size(); i++) {
try {
JSONObject jsonObject = array.getJSONObject(i);
// JSONObject myJsonObject = new JSONObject(userBehavioAudits);
schema.validate(jsonObject);
} catch (ValidationException e) {
String errorMessage = e.getAllMessages().toString().replace("not found", "为空值!");
String errorMessage1 = errorMessage.replace("is not a valid enum value", "不在枚举范围内!");
String errorMessage2 = errorMessage1.replace("expected maxLength:", "字段最大长度为:");
String errorMessage3 = errorMessage2.replace("actual:", "当前实际长度:");
String errorMessage4 = errorMessage3.replace("is not less or equal to ", "最大范围为为:");
String errorMessage5 = errorMessage4.replace("subject must not be valid against schema", "不可是以下枚举值");
errLis.add(errorMessage5);
}
}
return errLis;
}
{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "http://example.com/example.json",
"type": "object",
"additionalProperties": true,
"required": [
"shoop",
"wor"
],
"properties": {
"shoop": {
"$id": "#/properties/name",
"type": "string",
"maxLength": 5
},
"wor": {
"$id": "#/properties/sex",
"type": "string",
"maxLength": 5
},
"test": {
"type": "object", //这个是主要的因为对象的类型是object类型
"properties": {
"name": {
"type": "string",
"maxLength": 5
}
}
}
}
}
@Data
public class test2 {
private String shoop;
private String wor;
private test1 test;
}
@Data
public class test1 {
private String name;
private String sex;
private String filename;
}
{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "http://example.com/example.json",
"type": "object",
"additionalProperties": true,
"required": [
"shoop",
"wor"
],
"properties": {
"shoop": {
"$id": "#/properties/name",
"type": "string",
"maxLength": 5
},
"wor": {
"$id": "#/properties/sex",
"type": "string",
"maxLength": 5
},
"test": {
"type": "array", //表名其是个数组
"items": { //表示每一个元素
"properties": {
"name": {
"type": "string",
"maxLength": 5
}
}
}
}
}
}
@Data
public class test3 {
private String shoop;
private String wor;
private List test;
}
@Data
public class test1 {
private String name;
private String sex;
private String filename;
}
/**
format 的可能取值:
"date-time":日期和时间在一起,例如, 2018-11-13T20:20:39+00:00。
"time":draft7的时间,例如,20:20:39+00:00
"date":draft7的日期,例如,2018-11-13。
"hostname": Internet 主机名
"idn-hostname":国际化 Internet 主机名
"ipv4":IPv4 地址
"ipv6":IPv6 地址
"uri":通用资源标识符 (URI) 。
"uri-reference":一个 URI 引用(URI 或相对引用)
"iri":“uri”的国际化等价物。
"iri-reference":“uri-reference”的国际化等价物
"uri-template":一个 URI 模板(任何级别)
"json-pointer":一个 JSON 指针
"relative-json-pointer":一个相对 JSON 指针。
"regex":正则表达式。
*/
{
"type": "string",
"format": "date-time"