我有一个必须接受 JSON 请求的 Restful 网络服务 (JAVA)。我必须首先根据我拥有的 JSON 模式验证此 JSON。 我不确定再次验证 JSON JSON 模式的最佳 JAVA 库是什么。 我用过json-schema-validator -2.1.7 库但它不是很有帮助。即使认为我的 JSON 不是有效的 JSON,我也没有收到任何错误。
这是我用于 json-schema-validator-2.1.7 的代码
InputStream jsonSchemaInputStream = Assessment.class.getClassLoader().getResourceAsStream("Schemas/AssessmentMetrics.json");
ObjectMapper mapper = new ObjectMapper();
// Allows to retrieve a JSONSchema object on various sources
// supported by the ObjectMapper provided
JSONSchemaProvider schemaProvider = new JacksonSchemaProvider(mapper);
// Retrieves a JSON Schema object based on a file
JSONSchema schema = schemaProvider.getSchema(jsonSchemaInputStream);
// Validates a JSON Instance object stored in a file
List<String> errors = schema.validate(contents);
最佳答案
值得探索的项目:
- https://github.com/java-json-tools/json-schema-validator
- https://github.com/everit-org/json-schema
- https://github.com/networknt/json-schema-validator
Here是一个不错的列表。
Here是一个在线沙箱。
关于java - Java 中的 JSON 模式 validator 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18950047/