Note that, we will use just raw SQL and JDBC, via the JdbcTemplate, without using any ORMs (no JPA/Hibernate, etc). But this time we will use “dynamic data” provided by database tables (MariaDB) via a simple repository. In the next part (Part 2), we will proceed with an example (Case 2) creating a custom validation for a group (a combination) of 2 JSON fields (“master-details”). We will actually, validate an integer, which should be one of the values in a set of predefined integer values (in an array of integers). In this part (Part 1), we will see an example (Case 1) of how to create a custom validation with fixed/static data. Now it’s time to proceed to our implementation cases. □ you want to go deeper on creating custom constraints, you can visit the official link here. The last one is this that does the real job, it returns a boolean, and it should always return: true (for successful validation) or false (for unsuccessful). The implementation of the ConstraintValidator interface includes the implementation of the 2 default methods it defines: the initialize() and the isValid() methods that should be overridden. The ConstraintValidator interface accepts 2 parameters: the annotation interface and the type (object) of the field or the parameter that it should attached to. The validator implementation class, which usually by convention has a similar to the interface name, and should implement the Jakarta Bean Validation ConstraintValidator interface. the default message that will be returned in case of unsuccessful validation.the value (or the values) that can/should be passed with the annotation.the target (as we said FIELD, PARAMETER, CLASS, etc.).In the annotation interface we can define also some other parameters: in a field or a bean or DTO, or in a request query parameter). The name of the annotation interface defines the annotation itself, that we can use for our validation (e.g. The annotation type is defined using the keyword. The annotation validation interface (The constraint annotation) Be preparedĭefining a custom constraint validation, mainly, includes the creation of a custom validation interface for the annotation, and the validator implementation class which actually implements the validation. Those constraints cover the categories:Įspecially, for built-in field validation constraints (all field constraints are specified in the Jakarta Bean Validation API) you can find the full list here. The Hibernate validator offers us with a quite long list of built-in validation constraints, defined by the Jakarta Bean Validation specification, as well as of other additional constraints. So, in our project pom.xml file we have to have included it, like that: Here we are going to use the Hibernate Validator engine dependency (which is also a compiled dependency when we use the spring-boot-starter-validation). If you wish you can access this post, which provides a very basic introduction on how to start working with Spring Boot, JDBC, and MariaDB database in order to build a fundamental REST API.įor your convenience GitHub repos are provided, so they can be used, as starting points for each of the case examples of this post. It is supposed that you have some familiarity with Spring Boot development, REST APIs, Maven dependency management and POM files, JSON, and other related subjects (Spring annotations, DTOs, application properties, how to connect to a database using JDBC, JdbcTemplate, etc.). Similarly, we can think about how we can validate, ingredients against a Product, a specific Sub-Category against the main Category it belongs to, and so on. If the city does not match any of the cities in the array, then it does not belong to that country, and thus the request (e.g. Think about that, like the case we have to check if a city in one JSON field, is the one that really belongs to the cities’ array of that country. In this post, we focus on a common case, when we have to check if a value in a JSON field, passed in the body of a POST request, is one of the values provided in an array we define. The possibilities offered, go beyond the rich set of “ready-made” annotations and allow us to define our very own validation checks that can also concern a combination of fields (JSON keys). So, in a REST API application, validation of data, in a JSON body, could not be missed. Spring Boot is a full-fledged framework with a plethora of modules and features. Case 1 – integer custom validation, that should match one of the values in a given array Intro
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |