[英]validate a Java POJO using JSON schema or equivalent?
It's there a way or technology like JSON schema in which I get a JSON description of the data structure and I can validate in JAVA if fits the description. 有像JSON模式这样的方法或技术,通过该方法或技术可以获得数据结构的JSON描述,并且可以在JAVA中进行验证(如果适合该描述)。 I want something like JSON schema to validate incoming data, but I want to validate that the POJOs or Maps as Java objects, not the JSON document themselves. 我希望使用JSON模式之类的东西来验证传入数据,但是我想验证POJO或Maps是否为Java对象,而不是JSON文档本身。
Make the POJO a JSON just to validate it isn't a practical option, thanks! 将POJO设为JSON只是为了验证它不是实际的选择,谢谢!
is a way to do this with JSON schema or any other technologies? 用JSON模式或任何其他技术做到这一点的一种方法?
UPDATE: SUMMARY AS far as I know: JSON Schema is a JSON document used to validate JSON documents. 更新:总结据我所知:JSON Schema是用于验证JSON文档的JSON文档。 I want a schema written in JSON to validate Java POJOs. 我想要用JSON编写的架构来验证Java POJO。
the example below (I use the validate as a static function of class Validator ): 下面的示例(我使用validate作为Validator类的静态函数):
public static void main(String[] args) {
String schema = "{\n" +
" \"type\": \"object\",\n" +
" \"properties\": {\n" +
" \"street_address\": { \"type\": \"string\" },\n" +
" \"city\": { \"type\": \"string\" },\n" +
" \"state\": { \"type\": \"string\" }\n" +
" },\n" +
" \"required\": [\"street_address\", \"city\", \"state\"]\n" +
"}";
Object foo = new Hashtable();
((Map)foo).put("street_address","first street");
((Map)foo).put("street_address","The city");
((Map)foo).put("street_address","Not the USA");
Validator validator = new Validator(schema);
if (Validator.validate(foo, schema)) {
System.out.println("It's validated");
} else
System.err.println("It isn't validated");
}
You are looking for the OPEN API: https://github.com/OAI/OpenAPI-Specification 您正在寻找OPEN API: https : //github.com/OAI/OpenAPI-Specification
This is the former SWAGGER TOOL ( https://swagger.io ) which allows to define objects in JSON / YAML / XML. 这是前SWAGGER TOOL( https://swagger.io ),它允许以JSON / YAML / XML定义对象。 This is used a lot to: 这经常用于:
This allow you not only to document your POJOs but many other things in additional. 这不仅使您可以记录您的POJO,而且还可以记录许多其他内容。 It is quite powerful. 它非常强大。
I just came accross this project which does exactly what you are asking for: https://github.com/everit-org/json-schema 我刚遇到这个项目,它确实满足您的要求: https : //github.com/everit-org/json-schema
This is an example of a schema: 这是模式的示例:
{
"type" : "object",
"properties" : {
"rectangle" : {"$ref" : "#/definitions/Rectangle" }
},
"definitions" : {
"size" : {
"type" : "number",
"minimum" : 0
},
"Rectangle" : {
"type" : "object",
"properties" : {
"a" : {"$ref" : "#/definitions/size"},
"b" : {"$ref" : "#/definitions/size"}
}
}
}
}
Example JSON: JSON示例:
{
"rectangle" : {
"a" : -5,
"b" : 5
}
}
In this case the thrown ValidationException
will point to #/rectangle/a
. 在这种情况下,抛出的ValidationException
将指向#/rectangle/a
。
try {
schema.validate(rectangleSingleFailure);
} catch (ValidationException e) {
// prints #/rectangle/a: -5.0 is not higher or equal to 0
System.out.println(e.getMessage());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.