【校验复杂格式JSON】

说明

最近在做项目的时候遇到一个需求,需要对前端传过来的 json
进行校验,然后根据业务需求给模板填充数据。
下面是简单的校验思路,只需要稍稍研究一下校验 json 的用法就可以实现校验功能,简单的一批,上代码。

一、要求

校验如下 json,要求为:
     1、headerList  是一个数组类型,其中的每个元素都必须是一个对象。
	 2、headerList  对象必须包含  name  和  value  两个必需的属性。
	 3、name  属性是一个字符串类型,且它的最小长度必须为1,且不能只包含空白字符。这个属性没有其他限制。
	 4、value  属性是一个字符串类型,且它的最小长度必须为1,且不能只包含空白字符。这个属性没有其他限制。
	 5、rawParam  是一个对象类型,它有两个属性:  content  和  type 。
	 6、content  属性可以是字符串类型或 null 值,且它的最小长度为0。这个属性没有其他限制。
	 7、type  属性必须是  json 、 xml 、 html  或  plain  中的一个字符串值。

被校验的 JSON

{
  "headerList": [
    {
      "name": "Content-Type",
      "value": "application/json"
    },
    {
      "name": "Authorization",
      "value": "Bearer token123"
    }
  ],
  "rawParam": {
    "content": "{\"name\": \"John\", \"age\": 30}",
    "type": "json"
  }
}

二、引入坐标

<dependency>
    <groupId>com.github.fge</groupId>
    <artifactId>json-schema-validator</artifactId>
    <version>2.2.15</version>
</dependency>

三、根据需要编写用于检验 JSON 的JSON

用于校验的 JSON

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "headerList": {
      "type": "array",
      "items": {
        "type": "object",
        "required": ["name", "value"],
        "properties": {
          "des": {
            "type": "string"
          },
          "name": {
            "type": "string",
            "minLength": 1,
            "pattern": "^(?!\\s*$).+",
            "error": "请求头名称无效。必须是非空字符串。"
          },
          "value": {
            "type": "string",
            "minLength": 1,
            "pattern": "^(?!\\s*$).+",
            "error": "请求头值无效。必须是非空字符串。"
          }
        }
      }
    },
    "rawParam": {
      "type": "object",
      "properties": {
        "content": {
          "type": ["string", "null"],
          "minLength": 0,
          "error": "内容数据无效。必须是字符串或 null。"
        },
        "type": {
          "type": "string",
          "enum": ["json", "xml", "html", "plain"],
          "error": "内容类型无效。必须是以下值之一:json、xml、html、plain。"
        }
      }
    }
  },
  "required": ["headerList", "rawParam"],
  "additionalProperties": false
}	

四、完整代码

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

import java.io.IOException;

public class JsonValidator {

    public static final String JSON_DATA = "被校验 JSON";

    public static final String JSON_SCHEMA = "用于校验的 JSON";

    public static void main(String[] args) {
        try {
            validateJson(JSON_DATA, JSON_SCHEMA);
        } catch (IOException | ProcessingException e) {
            e.printStackTrace();
        }
    }

    public static void validateJson(String jsonData, String jsonSchema) throws IOException, ProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();

        JsonNode jsonNode = objectMapper.readTree(jsonData);
        JsonNode schemaNode = objectMapper.readTree(jsonSchema);

        JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
        JsonSchema schema = factory.getJsonSchema(schemaNode);

        ProcessingReport report = schema.validate(jsonNode);
        if (!report.isSuccess()) {
            System.out.println("JSON 数据不符合 JSON Schema 的规则:");
            System.out.println(report);
        } else {
            System.out.println("JSON 数据符合 JSON Schema 的规则。");
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值