Immutables: how to customize the generated classes

In a previous post we’ve seen a brief introduction to the Immutables library and its main features. With a few annotations we can automatically generate immutable classes out-of-the-box. Such immutable classes provide a whole range of useful characteristics that make them the ideal means to represent a resource’s state in an application. In this post we’ll see how to customize the classes generated by Immutables, with special regard to classes’ names and checks on data. […]

Introduction to the Immutables library

The Immutables library is a very powerful and interesting library, focused on easily creating immutable object classes with few annotations. In this introduction to the Immutables library we’ll show the basic features of this library and how to get the most out of them. The project We are going to create a simple maven project with just two dependencies: the immutables library junit5 (for testing purposes) We won’t use any frameworks in this demo. Just […]

Easily configure validators via properties in a Spring Boot project

The annotations from the Java Validation API provide a convenient and flexible tool to ensure the quality of an application’s input. Some of them have parameters that allow for the validation’s fine tuning. The main drawback is that the parameters’ values are resolved at compile time, that’s why they must be static final constants. We’ll show how we can overcome this limitation and configure validators via properties in a Spring Boot project. It would be […]

File download from a REST API with Spring Boot

When calling a REST endpoint the response is generally returned as JSON. What if we want to give the user the chance to download the same data as a file? We can build an endpoint that returns data as a downloadable file. The following example shows how easy it is to create an endpoint to download a file with Spring. The project We’ll build a Spring Boot project with spring-boot-starter-web as the only required dependency, […]

How to mock custom validators when unit testing

In the previous post we saw how dependencies in custom constraint validators can be mocked when we want to unit test our validation layer in a Spring Boot application. It all boils down to the creation of a custom ValidatorFactory that can use our custom ConstraintValidators, those will have their dependencies mocked and manually injected. But what if we want to mock an entire ConstraintValidator rather than just its dependencies? There are some situations where […]

How to mock dependencies when unit testing custom validators

The Java Validation API provides a very convenient API for input validation. It is part of the JavaEE spec, nevertheless it can be used within a Spring application too. Addressing the whole functioning of the Java Validation API is beyond the scope of this post, we will focus instead on how to unit test the validation layer. In this regard, we will show how to mock dependencies that might be injected in your custom constraint […]