美文网首页
JsonSchema的使用

JsonSchema的使用

作者: 一起DP吧 | 来源:发表于2019-08-16 15:21 被阅读0次

在系统交互之间,采用Json的数据格式执行协议,来完成数据传输,算是前置校验吧

省去schema文件的编写,大致内容如下,网上copy的

{
... 
 "vegetables":  { 
   "type":  "array", 
   "items":  {  "$ref":  "#/definitions/veggie"  } 
 } 
   }, 
   "definitions":  { 
 "veggie":  { 
   "type":  "object", 
   "required":  [  "veggieName",  "veggieLike"  ], 
   "properties":  { 
     "veggieName":  { 
       "type":  "string", 
       "description":  "The name of the vegetable." 
     }, 
     "veggieLike":  { 
       "type":  "boolean", 
       "description":  "Do I like this vegetable?" 
     } 
...}

只使用代码描述,不在进行过多赘述,我不太喜欢说话

抽象出大致要使用的顶级接口Complier,好了我似乎特别喜欢Complier,好多都是它。

public interface Complier {

    void complie(byte[] bytes);
}

实现顶级接口Complier

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.main.JsonSchema;
import com.github.fge.jsonschema.main.JsonSchemaFactory;

/**
 * @Description //TODO
 * @Author taren.Tian
 * @Date 14:59 2019/8/16
 **/
public class MyDataComplier implements Complier {

private JsonSchema schema;

private final ObjectMapper objectMapper = new ObjectMapper();

public MyDataComplier() {
    try {
        //读入自定义的schemas文件
        JsonNode sh = objectMapper.readTree(this.getClass()
                .getClassLoader()
                .getResourceAsStream("schemas/yourJson.json"));
        final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
        schema = factory.getJsonSchema(sh);
    } catch (Exception e) {

    }
}

@Override
public void complie(byte[] bytes) {
    try {
        JsonNode jsonNode = objectMapper.readTree(bytes);
        //先校验数据的格式 然后进行业务逻辑的处理
        ProcessingReport report = schema.validate(jsonNode);
        if (!report.isSuccess()) {
            throw new Exception(report.toString());
        }
        //doSomething()
    } catch (Exception e) {

    }
}
}

相关文章

  • JsonSchema的使用

    在系统交互之间,采用Json的数据格式执行协议,来完成数据传输,算是前置校验吧 省去schema文件的编写,大致内...

  • 使用JSONSchema断言

    对于复杂的JSON结构,虽然可以使用JSONPath快速提取相应的值。然而对于JSON响应的整体结构和各字段类型,...

  • JSON Schema入门

    JsonSchema官方文档入门文档入门文档生成Schema工具 使用Json的好处(什么是Schema): 描述...

  • JsonSchema进行接口测试

    何为JsonSchema 了解元数据的同学很容易理解JsonSchema和Json的关系。元数据是描述数据的数据,...

  • 接口自动化中的jsonSchema及契约测试

    目录 场景介绍:接口自动化及契约测试 jsonschema介绍 契约测试实现步骤及Demo jsonschema编...

  • 使用jsonschema校验json数据

    jsonschema用来标记和校验json数据,可在自动化测试中验证json的整体结构和字段类型 首先,了解一下j...

  • Validate API json payload

    I found openstack use jsonschema to validate API. Volupt...

  • JsonSchema 整合SpringBoot

    JsonSchema 就是一个json格式的 校验规则 校验写的json是否在你的规定内 以后工作遇到的问题也会...

  • JsonSchema中format的用法

    当你实例化validator时,如果没有给它传format_checker参数, jsonschema是不会自动校...

  • Jsonschema2pojo从JSON生成Java类(Mave

    1.说明 jsonschema2pojo工具可以从JSON Schema(或示例JSON文件)生成Java类型,并...

网友评论

      本文标题:JsonSchema的使用

      本文链接:https://www.haomeiwen.com/subject/zqqqsctx.html