{
"type": "record",
"name": "Person",
"fields": [
{"name": "name", "type": "string"},
{"name": "age", "type": "int"},
{"name": "email", "type": ["null", "string"], "default": null}
]
}
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
@JsonDeserialize(as = Person.class)
@JsonSerialize(as = Person.class)
public class Person {
public String name;
public int age;
public String email;
}
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.avro.AvroMapper;
import com.fasterxml.jackson.dataformat.avro.AvroSchema;
import com.fasterxml.jackson.dataformat.avro.AvroValidator;
public class DataValidator {
public static void main(String[] args) throws Exception {
AvroSchema schema = new AvroSchema(new ObjectMapper().readTree(getSchemaJson()));
AvroValidator validator = AvroValidator.forSchema(schema);
AvroMapper mapper = new AvroMapper();
Person person = mapper.readerFor(Person.class)
.with(schema)
.readValue(getAvroData());
validator.validate(person);
}
}