Json schema validation in Spring REST APIs
Solution 1
If you take a look at JSON schema site, there are only two libraries for validation in Java.
- The one that Jorge Campos suggested is mature, but looking for new maintainer: https://github.com/fge/json-schema-validator
- Second one is relatively new: 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 Assured library under the hood.
Solution 2
You can also look at Rest Assured Json Schema validator
https://www.baeldung.com/rest-assured-json-schema
Alec
Updated on July 09, 2022Comments
-
Alec almost 2 years
I’m building a REST API using Spring Boot and [jackson-module-jsonSchema] (https://github.com/FasterXML/jackson-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.
Seems jackson 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?
-
Alec over 8 yearsthanks for your answer @luboskrnac I tried github.com/fge/json-schema-validator and is looking good, when testing it I realized that jackson is generating JSON schema v3 and validator is exepcting v4-draft, I'm considering to change Jackson for other library , do you have any suggestion?
-
user252690 about 8 yearsmJson also fully supports schema validation and it's much simpler to use than the above: bolerio.github.io/mjson
-
sinner over 5 yearsPlease note that now is only one library is recommended: json-schema.org/implementations.html#validators
-
Israel dela Cruz almost 5 yearsPlease give some explanation to the content of a link answer.
-
Ankush Puri over 4 years@IsraeldelaCruz The idea here is to define json schema in your classpath and then validate the response against this schema using json validator. something like this assertThat().body(matchesJsonSchemaInClasspath("event_0.json")) You can go through the example on the link and let me know if you still have any questions.