Json schema validation in Spring REST APIs

27,315

Solution 1

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

  1. The one 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

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

Share:
27,315
Alec
Author by

Alec

Updated on July 09, 2022

Comments

  • Alec
    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
    Alec over 8 years
    thanks 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
    user252690 about 8 years
    mJson also fully supports schema validation and it's much simpler to use than the above: bolerio.github.io/mjson
  • sinner
    sinner over 5 years
    Please note that now is only one library is recommended: json-schema.org/implementations.html#validators
  • Israel dela Cruz
    Israel dela Cruz almost 5 years
    Please give some explanation to the content of a link answer.
  • Ankush Puri
    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.