https://github.com/novotnyr/spring-jsonschema-validation
Adds support for JSON Schema validation in Spring MVC Controllers
https://github.com/novotnyr/spring-jsonschema-validation
json-schema spring spring-boot spring-mvc
Last synced: 9 days ago
JSON representation
Adds support for JSON Schema validation in Spring MVC Controllers
- Host: GitHub
- URL: https://github.com/novotnyr/spring-jsonschema-validation
- Owner: novotnyr
- Created: 2018-01-28T19:53:40.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2020-01-22T22:17:16.000Z (over 5 years ago)
- Last Synced: 2025-03-31T18:07:09.699Z (about 2 months ago)
- Topics: json-schema, spring, spring-boot, spring-mvc
- Language: Java
- Size: 29.3 KB
- Stars: 7
- Watchers: 1
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.markdown
Awesome Lists containing this project
README
Spring MVC JSON Schema Validation
=================================Validate Spring MVC Controller parameters against JSON schema.
Just annotate them with `@JsonRequestBody`.How does it work?
-----------------
By adding `@JsonRequestBody` to the controller method parameter,
we will be able to validate the corresponding parameter against JSON Schema.@RequestMapping(method = RequestMethod.POST, value = "/boxes")
public void register(@JsonRequestBody BoxRequest request)By convention, the `request` parameter will be validated against
JSON schema available in the `CLASSPATH`, in the `BoxController#register.json`
file.Any schema validation error will be converted to the `JsonSchemaValidationException`
which wraps standard Spring `Errors` instance. Such exception
can be converted to the proper response by `@ExceptionHandler` mechanism
and like.Configuring
-----------
To enable this facility, register the `JsonRequestBodyArgumentResolverRegisteringBeanPostProcessor`
bean in the application context.@Bean
static BeanPostProcessor jsonRequestBodyArgumentResolverRegisteringBeanPostProcessor() {
return new JsonRequestBodyArgumentResolverRegisteringBeanPostProcessor();
}Note that the post processor bean method must be static, as per
Spring Framework requirements for bean postprocessors.For details and complex example, see unit tests and `TestApplicationContext`.
Requirements
------------* Spring Framework 4.2.0 or newer
* Everit JSON Schema validatorAdvanced Usage
--------------### Combining JSON Schema validation and Spring MVC Validators
JSON Schema validation can be combined with the standard Spring MVC
validation. Just pass the `Errors` instance as the following
parameter and suppress the immediate validation exception throwing:@RequestMapping(method = RequestMethod.POST, value = "/boxes")
public void register(@JsonRequestBody(strict = false) BoxRequest request, Errors errors)By setting `strict` parameter to false, no validation exception will be thrown.
Instead, all validation exceptions will be converted to the `Errors` validation
errors.