In a microservice architecture, services may accept several, if not many, of the same inputs. This pattern can easily lead to code duplication and redundancy between services. In an effort to mitigate these drawbacks and keep service code focused, we can devise a robust solution involving several APIs provided by Spring and Java.
The following tutorial will assume some working knowledge of Java and Spring Boot, but will cater to a range of skill levels. Regardless, it never hurts to see another developer’s code!
Our solution will involve combining the Java and Spring Boot APIs, ConstraintValidator
and ResponseEntityExceptionHandler
, respectively.
Hibernate Validator, enhanced as part of JSR 380, is a specification of the Java API for standard Bean validation. In the context of Spring Boot applications, you may have used this without thinking twice. Examples include:
@NotNull
@Min
@Max
@Pattern
@Past
@Email
@PositiveOrZero
#java #programming #spring #microservices