Enforce not-null field in JSON object
You can use JSON-SCHEMA as you can express many constraints on JSON fields with it: http://json-schema.org/
Then you can generate from the schema your java classes with @NotNull JSR 303 annotations and use bean validation on your object. It works with Jackson natively, so you should not have any problem.
For instance, you can use the maven plugin to do so: http://wiki.jsonschema2pojo.googlecode.com/git/site/0.3.7/generate-mojo.html
@Required
is a Spring framework annotation for injected beans, so I'd say don't use it for this purpose.
You can use this one instead:
http://robaustin.wikidot.com/annotations-and-notnull
@NotNull
String myString;
For runtime checks, try http://code.google.com/p/notnullcheckweaver/
JAX-RS separates quite nicely the deserialization from the validation, i.e. JSON-B (or Jackson) has by design no mechanism to enforce values to be non-null
, etc. Instead, you can use BeanValidation for that:
- Add a dependency to
javax.validation:validation-api
inprovided
scope. - Add the
javax.validation.constraints.NotNull
annotation to your field.
For more details, go here.