Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/janloebel/json-schema-validation-starter
Spring Boot 2 Starter for Json Schema Validation
https://github.com/janloebel/json-schema-validation-starter
boot json jsonschema schema spring springboot starter validation
Last synced: 25 days ago
JSON representation
Spring Boot 2 Starter for Json Schema Validation
- Host: GitHub
- URL: https://github.com/janloebel/json-schema-validation-starter
- Owner: JanLoebel
- License: mit
- Created: 2019-12-21T15:13:34.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2024-04-22T08:29:46.000Z (7 months ago)
- Last Synced: 2024-10-01T05:01:24.377Z (about 1 month ago)
- Topics: boot, json, jsonschema, schema, spring, springboot, starter, validation
- Language: Java
- Homepage:
- Size: 53.7 KB
- Stars: 9
- Watchers: 3
- Forks: 6
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Json-Schema-Validation-Starter
This provides a Spring-Boot-Starter to include JsonSchemaValidation with the help of the [https://github.com/networknt/json-schema-validator](https://github.com/networknt/json-schema-validator) -library.
![Foo](https://cdn.buymeacoffee.com/buttons/default-orange.png)
## Usage
Include the starter into you're project.
You need to add jitpack to your `pom.xml` because this project is not available in the official maven repository.
```
jitpack.io
https://jitpack.io
```
_Note_: For Spring Boot < 3.0 please use 2.3.0, otherwise use 3.x.x for Spring Boot >= 3.0.
Add the `json-schema-validation-starter`-dependency to your `pom.xml`
```com.github.JanLoebel
json-schema-validation-starter
3.0.1```
After that simply create a json-schema and put it into e.g.: `resources/jsonschema/book.json`.
The last step is now to let your entity know that it should be validated and which schema it should use.```
@JsonSchemaValidation("classpath:jsonschema/book.json")
public class Book {
private String title;
private String author;
}
```Alternatively, you need to add this annotation in your controller like below. This is helpful when you generate your classes from a schema or can't edit them.
```
@RestController
@RequestMapping("/books")
public BooksConroller {
@PostMapping
public ResponseEntity createBook(@RequestBody @JsonSchemaValidation("classpath:jsonschema/book.json") Book bookDto) {
//...
return bookDto;
}
}
```The schemas are read by a bean that implements the `JsonSchemaProvider` interface. By default, the `DefaultJsonSchemaProvider` is used, which can load schemas by a URL or from the classpath. If necessary, a custom schema provider can be implemented and configured:
```
@ConditionalOnProperty(prefix = "json.schema.validation", name = "schemaProvider", havingValue = "custom")
@Component
public class CustomJsonSchemaProvider implements JsonSchemaProvider {
JsonSchema loadSchema(String url) {
// Create and return a JSON schema...
}void handleValidationMessages(Collection validationMessages) {
// Handle validation messages...
}
}
```Configure the schema provider in `application.properties`:
```
json.schema.validation.schemaProvider=custom
```## Example project
Head over to [http://github.com/JanLoebel/json-schema-validation-starter-example](http://github.com/JanLoebel/json-schema-validation-starter-example) to checkout the sample project.## Contribution
Please feel free to improve or modify the code and open a Pull-Request! Any contribution is welcome :)## License
MIT LicenseCopyright (c) 2019 Jan Löbel
See LICENSE file for details.