java Spring REST API 中的 Json 模式验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34858709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 23:27:57  来源:igfitidea点击:

Json schema validation in Spring REST APIs

javarestspring-mvcspring-bootjson-schema-validator

提问by Alec

I'm building a REST API using Spring Boot and [Hymanson-module-jsonSchema] (https://github.com/FasterXML/Hymanson-module-jsonSchema) for JSON schema generation. I'm looking the best way to validate the request JSON payload arriving to my APIs endpoints (Spring controllers) against the defined JSON schema defined for the exposed resource, validation includes check required fields , format , min and max values, etc.. everything we can validate against the schema.

我正在使用 Spring Boot 和 [Hymanson-module-jsonSchema] ( https://github.com/FasterXML/Hymanson-module-jsonSchema)构建 REST API 以生成 JSON 模式。我正在寻找根据为公开资源定义的已定义 JSON 模式验证到达我的 API 端点(Spring 控制器)的请求 JSON 有效负载的最佳方法,验证包括检查必需字段、格式、最小值和最大值等。一切我们可以根据架构进行验证。

Seems Hymanson json schema module is useful for schema generation but not for validation, am I right? Any suggestion on how to achieve what I'm trying to do?

似乎 Hymanson json 模式模块对模式生成很有用,但对验证没有用,对吗?关于如何实现我想要做的任何建议?

回答by luboskrnac

If you take a look at JSON schema site, there are only two libraries for validation in Java.

如果您查看JSON schema site,就会发现 Java 中只有两个用于验证的库。

  1. The ont that Jorge Campos suggested is mature, but looking for new maintainer: https://github.com/fge/json-schema-validator
  2. Second one is relatively new: http://github.com/everit-org/json-schema
  1. Jorge Campos 建议的 ont 已经成熟,但正在寻找新的维护者:https: //github.com/fge/json-schema-validator
  2. 第二个比较新:http: //github.com/everit-org/json-schema

I was recently in situation where I had to choose one or the other and I picked first option. It is being used also by Rest Assuredlibrary under the hood.

我最近处于必须选择一个或另一个的情况,我选择了第一个选项。它也被Rest Assured图书馆在幕后使用。

回答by Ankush Puri

You can also look at Rest Assured Json Schema validator

您还可以查看 Rest Assured Json Schema 验证器

https://www.baeldung.com/rest-assured-json-schema

https://www.baeldung.com/rest-assured-json-schema