Reputation: 263
I need a way to validate a specific formatted json that can represent a class (in my case Book class) strings using Jackson. Is there any way of doing this using jsonSchema? Or I have to do it in different way
ObjectMapper mapper = new ObjectMapper();
JsonSchema jsonSchema = mapper.generateJsonSchema(Book.class);
Upvotes: 3
Views: 6084
Reputation: 3031
EDIT
You need to use a third party library for this.
i.e. https://github.com/fge/json-schema-validator
ObjectMapper objectMapper = new ObjectMapper();
// this line will generate JSON schema from your class
JsonNode schemaNode = objectMapper.generateJsonSchema(StageDetail.class).getSchemaNode();
// make your JSON to JsonNode
JsonNode jsonToValidate = JsonLoader.fromString(JSON_TO_VALIDATE);
// validate it against the schema
ProcessingReport validate = JsonSchemaFactory.byDefault().getJsonSchema(schemaNode).validate(jsonToValidate);
// validate.messages contains error massages
System.out.println("Valid? " + validate.isSuccess());
Upvotes: 5