JAVA Json-Schema接口校验利器

何为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
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值