json是我们编写api时候用于数据传递的常用格式,那么你是否知道json schema呢?
在数据交换领域,json schema 以其强大的标准化能力,为定义和规范 json 数据的结构与规则提供了有力支持。通过一系列精心设计的关键字,json schema 能够详尽地描述数据的各项属性。然而,仅凭 json schema 本身,尚不足以验证 json 实例是否严格遵循预设的模式。此时,json schema 验证器的角色便显得尤为关键。这些验证器如同严格的检查官,确保每一个 json 文档都能忠实地反映出模式的定义。json schema 验证器,作为实现 json schema 规范的技术工具,其灵活的集成能力使得无论项目规模大小,都能轻松地将 json schema 融入开发流程,从而提升数据处理的效率与准确性。
下面我们来看看如何在spring boot应用中使用json schema校验json数据
动手试试
- 创建一个基本的spring boot应用,如果还不会可以点击查看快速入门
- 在
pom.xml
中添加json-schema-validator
依赖
<dependency> <groupid>com.networknt</groupid> <artifactid>json-schema-validator</artifactid> <version>1.4.0</version> </dependency>
创建json schema
在src/main/resources
目录下创建一个validation.json
文件,然后在里面制定一套详尽的验证规则,比如下面这样:
{ "$schema": "http://json-schema.org/draft-07/schema#", "title": "order event", "description": "order event schema for example", "required": ["order_id", "total_price", "products" ], "properties": { "order_id": { "type": "string" }, "event": { "enum": ["placed", "delivered", "returned"], "type": "string" }, "total_price": { "type": "number", "minimum": 0 }, "products": { "type": "array", "items": { "additionalproperties": true, "required": ["product_id", "price"], "minitems": 1, "properties": { "product_id": { "type": "string" }, "price": { "type": "number", "minimum": 0 }, "quantity": { "type": "integer" } } } } } }
创建 jsonschema 的 bean
当然,你也可以直接new来创建,但实战中还是推荐用spring管理这些实例,比如 下面这样:
@configuration public class jsonschemaconfiguration { private static final string schema_validation_file = "validation.json"; @bean public jsonschema jsonschema() { return jsonschemafactory .getinstance( specversion.versionflag.v7 ) .getschema( getclass().getresourceasstream( schema_validation_file ) ); } }
使用 jsonschema
@slf4j @service public class jsonschemavalidationservice{ @autowired private jsonschema jsonschema; public string validatejson(jsonnode jsonnode){ set<validationmessage> errors = jsonschema.validate(jsonnode); if(errors.isempty()){ log.info("event is valid"); }else{ log.info("event is invalid"); } return errors.tostring(); } }
在 web 层的应用
创建一个controller,当接收到来自客户端的json数据之后,就可以像下面这样对json数据进行校验:
import com.fasterxml.jackson.databind.jsonnode; @restcontroller public class jsonschemacontroller { @autowired private jsonschemavalidationservice service; @postmapping("/test") public string validateevent( @requestbody jsonnode jsonnode ){ return service.validatejson(jsonnode); } }
测试一下
启动 sprint boot 应用,然后使用你喜欢的http客户端工具对/test
接口发送测试请求:
比如,下面使用curl来进行测试:
符合规则的合法请求:
$ curl --location 'localhost:8080/test' \ --header 'content-type: application/json' \ --data '{ "order_id":"order134", "event": "placed", "products": [ { "product_id": "product_1", "price":20.5, "quantity":2 } ], "total_price": 41 }'
校验通过,返回:[],没有错误信息
不符合规则的非法请求(却少order id):
$ curl --location 'localhost:8080/test' \ --header 'content-type: application/json' \ --data '{ "event": "placed", "products": [ { "product_id": "product_1", "price":20.5, "quantity":2 } ], "total_price": 41 }'
校验失败,将返回错误信息:[$.order_id: is missing but it is required]
相关资料what is json schema?json schema validator
到此这篇关于spring boot 中使用 json schema 来校验复杂json数据的文章就介绍到这了,更多相关spring boot 校验json数据内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论