javajsonannotationsjackson

Jackson - Required property?


I'm using Jackson's readValue() method on an object mapper to read from a JSON file and convert it into my java object.

eg.

mapperObject.readValue( node, MyTargetClass.class )

Are there any annotations that I can set on MyTargetClass to enforce required attributes? For example, if I have a JSON object with properties ABC,DEF and GHI, and my Json is the following

{
  "ABC" : "somevalue"
  "DEF" : "someothervalue" 
}

I want it to fail somehow, and only succeed on the readValue if it contained ABC, DEF and GHI.


Solution

  • Jackson does not include validation functionality, and this is by design (i.e. that is considered out-of-scope). But what is usually used is Bean Validation API implementation. The nice thing about this is decoupling between data format handling, and validation logic. This is what frameworks like DropWizard use; and it's the direction JAX-RS (like Jersey) are taking things for JAX-RS 2.0.