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. […]

How To Copy The Current Directory’s Path To The Clipboard In A Single Terminal Command In Unix

When working from the terminal, we might need from time to time to copy the current path to the clipboard, for example to paste it in a text file. In this post we’ll show how to copy the current directory’s path to the clipboard in a single terminal command in a Unix system. Specifically, we’ll see the necessary commands when working either with: Git Bash for Windows MacOS The quickest way to get the current […]

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 […]

Better productivity with IntelliJ IDEA thanks to keyboard shortcuts

Today I’ll show you a little trick to get better productivity with IntelliJ IDEA, one of the most popular IDEs for Java development, thanks to keyboard shortcuts. Describing all of IntelliJ IDEA’s countless features is of course beyond the scope of a simple blog post. Many of those features are quickly accessible via keyboard shortcuts. We’ll see how to create a custom keyboard shortcut when none is available for a given action. In the following […]

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 […]

More productive with Git easily: create Aliases and Functions

When using the command line, we programmers end up typing certain commands very frequently. That’s where aliases come in handy. Today we’ll see how to easily become more productive with Git by creating custom aliases and functions. The following examples are made in Git Bash for Windows, but with small changes they apply to Unix systems as well. Keep it short and quick with aliases Probably the most common command when working with Git is […]

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 […]

Decorator pattern in a Spring Boot project

Anyone who has worked with Hibernate has already faced the LazyInitializationException. It occurs when trying to access an association that hasn’t been fetched along with an entity. To avoid this, the required association(s) must be fetched within the same database transaction. Fetching several to-one associations can be achieved in one single JPQL query via several JOIN FETCH clauses or by defining an appropriate EntityGraph. For to-many associations neither of those two approaches is possible: Hibernate […]