As far as I know Jackson can only produce schemas for given types, but not do validation. There is json-schema-validator but it is no longer maintained.
Answer from Martina on Stack OverflowAs far as I know Jackson can only produce schemas for given types, but not do validation. There is json-schema-validator but it is no longer maintained.
1.) Add Dependency pom.xml :-
<dependency>
<groupId>com.github.fge</groupId>
<artifactId>json-schema-validator</artifactId>
<version>2.2.6</version>
</dependency>
2.) NoSqlEntity is a meta data for entity that can reside in no-sql database.
Initialized NoSqlEntity with schema file.
public static final NoSqlEntity entity = new NoSqlEntity("PAYOUT_ENTITY", "DB_","/schema/payout_entity.json");
public class NoSqlEntity {
private static final Map<String, NoSqlEntity> STORE = new HashMap<>();
private final AtomicLong seq = new AtomicLong(System.currentTimeMillis());
private IdentityGenerator identityGenerator;
private String entity;
private String collectionName;
private String jsonSchema;
private String idColumn = "id";
private String database;
public NoSqlEntity(String entity, String idColumn, String collectionPrefix, String jsonSchema) {
this.entity = entity;
this.idColumn = idColumn;
this.collectionName = collectionPrefix + "_" + entity;
this.jsonSchema = jsonSchema;
STORE.put(entity, this);
}
public NoSqlEntity(String collectionName, String jsonSchema) {
this.collectionName = collectionName;
this.jsonSchema = jsonSchema;
}
public static NoSqlEntity valueOf(String entityType) {
return STORE.get(entityType);
}
public boolean isNotNullSchema() {
return jsonSchema != null;
}
...
// Other Getter/Setter properties and methods.
}
3.) Sample format of validation schema file of payout_entity.json-
{
"properties":{
"txId":{"type":"string"}
}
"required" :["txId","currency"]
}
4.) JsonSchemaManager - Validate the incoming JSON schema and cache the schema as well.
public class JsonSchemaManager {
private final static Logger LOGGER = LoggerFactory.getLogger(JsonSchemaManager.class);
protected final static String LS = StandardSystemProperty.LINE_SEPARATOR.value();
private final JsonValidator validator = JsonSchemaFactory.byDefault().getValidator();
private final Map<NoSqlEntity, JsonNode> schemaMap = new HashMap<>();
public JsonNode load(NoSqlEntity noSqlEntity) throws IOException {
final JsonNode schema = JsonLoader.fromURL(this.getClass().getResource(noSqlEntity.getJsonSchema()));
schemaMap.put(noSqlEntity, schema);
return schema;
}
public void validateSchema(NoSqlEntity noSqlEntity, JsonNode toBeValidated, Consumer<ProcessingReport> consumer) {
try {
JsonNode schema = schemaMap.get(noSqlEntity);
if (schema == null) {
schema = load(noSqlEntity);
}
final ProcessingReport report = validator.validate(schema, toBeValidated);
if (!report.isSuccess()) {
consumer.accept(report);
}
} catch (IOException ex) { //NOSONAR
throw new InvalidRequestException(ex.toString());
} catch (ProcessingException ex) { //NOSONAR
throw new InvalidRequestException(ex.toString());
}
}
public synchronized boolean synchronizedCheck(NoSqlEntity noSqlEntity, JsonNode toBeValidated, Consumer<Map<String, Object>> messageConsumers) {
boolean flags = CommonUtils.unchecked(() -> {
validateSchema(noSqlEntity, toBeValidated, report -> {
report.forEach(processingMessage -> messageConsumers.accept(JsonConverter.jsonAsMapObject(processingMessage.asJson())));
});
return true;
}, ex -> {
throw new RuntimeException(ex.toString()); //NOSONAR
});
return flags;
}
}
5.) NoSqlRepository which persist meta data into NoSql DB.
@Component
public class NoSqlRepository {
private static final Logger LOGGER = LoggerFactory.getLogger(NoSqlRepository.class);
private final DocumentFormat documentFormat = DocumentFormat.JSON;
private static final String SEPARATOR = ",";
private static final ThreadLocal<MyLocalVariable> THREAD_LOCAL_VARIABLES = ThreadLocal.withInitial(() -> new MyLocalVariable());
static class MyLocalVariable {
private JsonSchemaManager schemaManager = new JsonSchemaManager();
private BasicBSONDecoder bsonDecoder = new BasicBSONDecoder();
public JsonSchemaManager getSchemaManager() {
return schemaManager;
}
public BasicBSONDecoder getBsonDecoder() {
return bsonDecoder;
}
}
private void checkSchemaIfAny(NoSqlEntity noSqlEntity, JsonNode entity) {
if (noSqlEntity.isNotNullSchema()) {
THREAD_LOCAL_VARIABLES.get().getSchemaManager().check(noSqlEntity, entity);
}
}
public String saveEntity(NoSqlEntity noSqlEntity, JsonNode entity){
// Before persisting payload into noSQL, validate payload against schema.
this.checkSchemaIfAny(noSqlEntity,entity);
}
// Other CURD methods here...
}
Videos
I searched for the best practice to enforce validation for incoming json data into a RESTful service. My suggestion is to use a MessageBodyReader which performs the validation inside the readFrom method. Below there is an message-body-reader example which is non-generic for the sake of simplicity.
I also was interesed in finding the best framework for doing json data validation. Because I use the jackson framework (version 1.8.5) for marshaling and unmarshaling between json and java, it would have been nice if this framework would provide a json data validation functionality. Unfortunately I couldn't find any possibility to do this with jackson. Finally I got it working with the json-schema-validator available at https://github.com. The version I use is 2.1.7
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import javax.servlet.ServletContext;
import javax.ws.rs.Consumes;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.Provider;
import org.codehaus.jackson.map.ObjectMapper;
import at.fhj.ase.dao.data.Address;
import at.fhj.ase.xmlvalidation.msbreader.MessageBodyReaderValidationException;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jackson.JsonLoader;
import com.github.fge.jsonschema.exceptions.ProcessingException;
import com.github.fge.jsonschema.main.JsonSchemaFactory;
import com.github.fge.jsonschema.main.JsonValidator;
import com.github.fge.jsonschema.report.ProcessingReport;
@Provider
@Consumes(MediaType.APPLICATION_JSON)
public class AddressJsonValidationReader implements MessageBodyReader<Address> {
private final String jsonSchemaFileAsString;
public AddressJsonValidationReader(@Context ServletContext servletContext) {
this.jsonSchemaFileAsString = servletContext
.getRealPath("/json/Address.json");
}
@Override
public boolean isReadable(Class<?> type, Type genericType,
Annotation[] annotations, MediaType mediaType) {
if (type == Address.class) {
return true;
}
return false;
}
@Override
public Address readFrom(Class<Address> type, Type genericType,
Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
throws IOException, WebApplicationException {
final String jsonData = getStringFromInputStream(entityStream);
System.out.println(jsonData);
InputStream isSchema = new FileInputStream(jsonSchemaFileAsString);
String jsonSchema = getStringFromInputStream(isSchema);
/*
* Perform JSON data validation against schema
*/
validateJsonData(jsonSchema, jsonData);
/*
* Convert stream to data entity
*/
ObjectMapper m = new ObjectMapper();
Address addr = m.readValue(stringToStream(jsonData), Address.class);
return addr;
}
/**
* Validate the given JSON data against the given JSON schema
*
* @param jsonSchema
* as String
* @param jsonData
* as String
* @throws MessageBodyReaderValidationException
* in case of an error during validation process
*/
private void validateJsonData(final String jsonSchema, final String jsonData)
throws MessageBodyReaderValidationException {
try {
final JsonNode d = JsonLoader.fromString(jsonData);
final JsonNode s = JsonLoader.fromString(jsonSchema);
final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
JsonValidator v = factory.getValidator();
ProcessingReport report = v.validate(s, d);
System.out.println(report);
if (!report.toString().contains("success")) {
throw new MessageBodyReaderValidationException(
report.toString());
}
} catch (IOException e) {
throw new MessageBodyReaderValidationException(
"Failed to validate json data", e);
} catch (ProcessingException e) {
throw new MessageBodyReaderValidationException(
"Failed to validate json data", e);
}
}
/**
* Taken from <a href=
* "http://www.mkyong.com/java/how-to-convert-inputstream-to-string-in-java/"
* >www.mkyong.com</a>
*
* @param is
* {@link InputStream}
* @return Stream content as String
*/
private String getStringFromInputStream(InputStream is) {
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String line;
try {
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sb.toString();
}
private InputStream stringToStream(final String str) throws UnsupportedEncodingException {
return new ByteArrayInputStream(str.getBytes("UTF-8"));
}
}
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.main.JsonSchema;
import com.github.fge.jsonschema.main.JsonSchemaFactory;
import com.github.fge.jackson.JsonLoader;
import com.fasterxml.jackson.databind.JsonNode;
public class ValidationJSON {
public static void main(String[] arr){
String jsonData = "{\"name\": \"prem\"}";
String jsonSchema = ""; //Schema we can generate online using http://jsonschema.net/
final JsonNode data = JsonLoader.fromString(jsonData);
final JsonNode schema = JsonLoader.fromString(jsonSchema);
final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
JsonValidator validator = factory.getValidator();
ProcessingReport report = validator.validate(schema, data);
System.out.println(report.isSuccess());
}
}
You can use FasterXML jackson with the module:json-schema-validator as holten proposed.
Include it in maven: com.github.java-json-tools json-schema-validator 2.2.8
Complementary to this, to generate schema from object instead of writing it manually you can also use another module: https://github.com/FasterXML/jackson-module-jsonSchema
I may add a functional example if needed
The json-schema-validator in GitHub, Perhaps It will help you to check the json object in java.
The main scope of GSON and Jackson is to handle the serialization/deserialization of your data to/from JSON. Both the generation and validation of JSON Schemas are not their (main) concern.
As per comments on your other question (How to display default value in JSON Schema using Jackson), there are a couple of alternatives out there for the schema generation โ the same applies to their validation.
List of Implementations (Generation & Validation)
As per R.Groote's answer, you can find some (but not all) of them on https://json-schema.org/implementations.html โ both generation and validation libraries.
Validation
Personally, I've only used networknt/json-schema-validator so far and found it very intuitive and easy to use. It uses Jackson under the hood, which was practical in my use-case as I was already using the same library for the (de)serialization of data from/to JSON โ so less additional dependencies.
Generation
Disclaimer: here comes the biased part as I'm the creator of victools/jsonschema-generator.
One example for a JSON Schema generation library would be victools/jsonschema-generator. That also uses Jackson internally, so again similar dependencies to the networknt/json-schema-validator.
The jsonschema-generator library does not populate the "default" value out-of-the-box, but you could easily configure it like this (based on the Jackson @JsonProperty annotation as per your other question):
SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2019_09, OptionPreset.PLAIN_JSON);
configBuilder.forFields().withDefaultResolver(field -> {
JsonProperty annotation = field.getAnnotationConsideringFieldAndGetter(JsonProperty.class);
return annotation == null || annotation.defaultValue().isEmpty() ? null : annotation.defaultValue());
});
SchemaGenerator generator = new SchemaGenerator(configBuilder.build());
JsonNode jsonSchema = generator.generateSchema(YourClass.class);
System.out.println(jsonSchema.toString());
If you are catering for numeric default values, you might have to include some conditional parsing of the @JsonProperty.defaultValue String then.
Additionally, if you want some more specific handling of Jackson annotations you could also consider adding the optional victools/jsonschema-module-jackson as well then.
EDIT (as response to your concerns having to define the default values twice โ in the code and on the annotation):
With the victools/jsonschema-generator you have full control over where the default value is coming from. One possible approach could be to look-up the default values from actual instances of the respective objects being encountered โ here filtering by your own package to skip external types.
The following is just a rough example of what is possible:
ConcurrentMap<Class<?>, Object> instanceCache = new ConcurrentHashMap<>();
configBuilder.forFields().withDefaultResolver(field -> {
Class<?> declaringClass = field.getDeclaringType().getErasedType();
if (!field.isFakeContainerItemScope()
&& declaringClass.getName().startsWith("your.package")) {
MethodScope getter = field.findGetter();
if (getter != null) {
try {
Object instance = instanceCache.computeIfAbsent(declaringClass, declaringClass::newInstance);
Object defaultValue = getter.getRawMember().invoke(instance);
return defaultValue;
} catch (Exception ex) {
// most likely missing a no-args constructor
}
}
}
return null;
});
I prefer to use GSON or Jackson: - https://www.baeldung.com/jackson-vs-gson
You could use Justify or json-schema-validator see below links: - https://json-schema.org/implementations.html - Validate JSON schema compliance with Jackson against an external schema file
I hope the above links give you enough information