java校验方案选择

参数校验的方案

1. 简单参数校验,使用Guava的Preconditions。

参考:Guava学习笔记:Preconditions优雅的检验参数

2. Bean校验,使用Hibernate Validator

参考:Hibernate Validator The Bean Validation reference implementation.

3. Map,JSON校验,使用JSONSchema

参考:json-schema

 

JSONSchema校验的优点

1. JSONSchema是一种规范,是跨语言的。

2. 适用于以json对象交互的接口中。适应json数据变化多端的特性。

3. Java中对JSON对象校验的缺失,由JSONSchema来弥补

4. 让校验变得文档化,便于集中管理

 

Java操作JSONSchema的案例

1. maven中添加依赖

<!-- https://mvnrepository.com/artifact/com.github.fge/json-schema-validator -->
<dependency>
    <groupId>com.github.fge</groupId>
    <artifactId>json-schema-validator</artifactId>
    <version>2.2.6</version>
</dependency>

2. 编写jsonschema文档

{
    "$schema":"http://json-schema.org/draft-04/schema",
    "type":"object",
    "properties": {
        "name": {
            "type":"string"
        },
        
        "versions": {
            "type":"array",
            "items": {
                "type":"object",
                "properties": {
                    "id": {
                        "type":"string"
                    },
                    
                    "version": {
                        "type":"integer"
                    },
                    
                    "comment": {
                        "type":"string"
                    }
                },
                
                "required":["id", "version"],
                "minItems":1
            }
        }
    },
    
    "required":["name", "versions"]
}

3. java代码

import java.io.IOException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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.ProcessingReport;
import com.github.fge.jsonschema.main.JsonSchema;
import com.github.fge.jsonschema.main.JsonSchemaFactory;

public class JsonShemaValidator {

    private static Log log = LogFactory.getLog(JsonShemaValidator.class);

    private final static JsonSchemaFactory factory = JsonSchemaFactory.byDefault();

    /**
     * validate instance and Schema,here including two functions. as follows:
     * first: the Draft v4 will check the syntax both of schema and instance.
     * second: instance validation.
     * 
     * @param mainSchema
     * @param instance
     * @return
     * @throws IOException
     * @throws ProcessingException
     */
    public static ProcessingReport validatorSchema(String mainSchema, String instance) throws IOException, ProcessingException {
        JsonNode mainNode = JsonLoader.fromString(mainSchema);
        JsonNode instanceNode = JsonLoader.fromString(instance);
        JsonSchema schema = factory.getJsonSchema(mainNode);
        ProcessingReport processingReport = schema.validate(instanceNode);
        log.info(processingReport);

        return processingReport;
    }

}

 

参考:

总结一下最近用到的技术(2)--JsonSchema和JsonSchemaValidator

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值