Restful 接口数据校验(json schema)
Posted Adorable_Rocy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Restful 接口数据校验(json schema)相关的知识,希望对你有一定的参考价值。
前言:最近工作中需要对接口数据进行监控校验,在熟读官网文档之后,发现用起来是真的挺不错的
JSON Schema是什么?
- JSON Schema其实也是一组特殊的json数据,它是用来标记和校验JSON数据的,目前该约定的草案已经到第七版了(draft-07)。JSON Schema语法格式也十分易读,可读性很强。
如何获取JSON Schema呢?
- JSON Schema在各个语言下都有基本的实现,不同语言当然对约定草案支持的程度也不通,小编在这里使用的JAVA语言
- 官网doc
1.搭建识别工具类
json schema坐标
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-schema-validator</artifactId>
<version>3.1.1</version>
</dependency>
- JsonValidateUtil
private final static JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
/**
* 校验JSON
* @param schema json模式数据(可以理解为校验模板)
* @param instance 需要验证Json数据
* @return ProcessingReport
*/
public static ProcessingReport validatorJsonSchema(String schema, String instance) throws IOException{
ProcessingReport processingReport =null;
JsonNode jsonSchema = JsonLoader.fromString(schema);
JsonNode jsonData = JsonLoader.fromString(instance);
processingReport =factory.byDefault().getValidator().validateUnchecked(jsonSchema, jsonData);
boolean success = processingReport.isSuccess();
if (!success) {
Iterator<ProcessingMessage> iterator = processingReport.iterator();
while (iterator.hasNext()) {
log.error(String.valueOf(iterator.next()));
}
}
return processingReport;
}
- ReadJsonFile
/**
* 读取Json文件为String json
* @param filePath filePath为文件的相对于resources的路径
* @return String
*/
public static String readJsonFileAsString(String filePath) {
filePath = ReadJsonFile.class.getResource(filePath).getPath();
String jsonStr ="";
try {
File jsonFile =new File(filePath);
FileReader fileReader =new FileReader(jsonFile);
Reader reader =new InputStreamReader(new FileInputStream(jsonFile), "utf-8");
int ch =0;
StringBuffer sb =new StringBuffer();
while ((ch = reader.read()) != -1) {
sb.append((char) ch);
}
fileReader.close();
reader.close();
jsonStr = sb.toString();
return jsonStr;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* 读取Json文件为JsonNode
* @param filePath filePath为文件的绝对路径
* @return JsonNode
*/
public static JsonNode readJsonFileAsJsonNode(String filePath) {
JsonNode instance =null;
try {
instance =new JsonNodeReader().fromReader(new FileReader(filePath));
} catch (IOException e) {
e.printStackTrace();
}
return instance;
}
2.JSON Schema & JSON
- JSON
{
"json": {
"checked": false,
"dimensions": {
"width": 5,
"height": 10
},
"id": 1,
"name": "A green door",
"price": 12.5,
"tags": [
"home",
"green",
"yellow"
]
}
- JSON Schema
"jsonSchema":{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "http://example.com/root.json",
"type": "object",
"title": "The Root Schema",
"description": "The root schema is the schema that comprises the entire JSON document.",
"default": {
},
"required": [
"checked",
"dimensions",
"id",
"name",
"price",
"tags"
],
"properties": {
"checked": {
"$id": "#/properties/checked",
"type": "boolean",
"title": "The Checked Schema",
"description": "An explanation about the purpose of this instance.",
"default": false,
"examples": [
false
]
},
"dimensions": {
"$id": "#/properties/dimensions",
"type": "object",
"title": "The Dimensions Schema",
"description": "An explanation about the purpose of this instance.",
"default": {
},
"examples": [
{
"height": 10.0,
"width": 5.0
}
],
"required": [
"width",
"height"
],
"properties": {
"width": {
"$id": "#/properties/dimensions/properties/width",
"type": "integer",
"title": "The Width Schema",
"description": "An explanation about the purpose of this instance.",
"default": 0,
"examples": [
5
]
},
"height": {
"$id": "#/properties/dimensions/properties/height",
"type": "integer",
"title": "The Height Schema",
"description": "An explanation about the purpose of this instance.",
"default": 0,
"examples": [
10
]
}
}
},
"id": {
"$id": "#/properties/id",
"type": "integer",
"title": "The Id Schema",
"description": "An explanation about the purpose of this instance.",
"default": 0,
"examples": [
1
]
},
"name": {
"$id": "#/properties/name",
"type": "string",
"title": "The Name Schema",
"description": "An explanation about the purpose of this instance.",
"default": "",
"examples": [
"A green door"
]
},
"price": {
"$id": "#/properties/price",
"type": "number",
"title": "The Price Schema",
"description": "An explanation about the purpose of this instance.",
"default": 0,
"examples": [
12.5
]
},
"tags": {
"$id": "#/properties/tags",
"type": "array",
"title": "The Tags Schema",
"description": "An explanation about the purpose of this instance.",
"default": [
],
"examples": [
[
"home",
"green"
]
],
"items": {
"$id": "#/properties/tags/items",
"type": "string",
"title": "The Items Schema",
"description": "An explanation about the purpose of this instance.",
"default": "",
"examples": [
"home",
"green"
]
}
}
}
}
}
详情查看官网学习
以上是关于Restful 接口数据校验(json schema)的主要内容,如果未能解决你的问题,请参考以下文章
Flask 学习-33.restful-full 请求参数校验reqparse.RequestParser()
springMVC学习(11)-json数据交互和RESTful支持