Enforce not-null field in JSON object

后端 未结 4 808
余生分开走
余生分开走 2020-12-10 00:43

Our REST API receives some JSON objects input where some fields are required to be not null. Those can be either String/Integer or even might be some other class instance as

4条回答
  •  忘掉有多难
    2020-12-10 01:39

    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

提交回复
热议问题