Last updated: January 01, 2021
Using Lombok in a Spring Boot project
Lombok is no ordinary Java library - Lombok provides annotations which generate Java code on the fly. This can drastically shorten the source code which has to be written.
To use Lombok, first the dependency must be included in the
pom.xml. Here the annotation processor is especially important, so that Lombok
can intervene directly in the compilation process. Bootify adds these dependencies
automatically to the project if Lombok is enabled.
Lombok dependencies in the build.gradle
Now Lombok must be activated explicitly in the IDE. In IntelliJ you have to install a plugin for that.
Installation of the Lombok plugin in IntelliJ under "Settings → Plugins", Search for "Lombok"
Furthermore, annotation proceccing must be activated. This is already included in the dependencies, but is only used by gradle and maven there. IntelliJ uses its own build process, which has to be adapted separately.
Activate Annotation Processing under Settings → Build, Execution, Deployment → Compiler → Annotation Processors
The most useful helpers
Here are a few examples of these great annotations, which I always use in projects if possible.
As the names suggest, these annotations create getters and setters for all fields
With @Slf4j a static field "log" is added to the class without extra coding
@SneakyThrows is hiding exceptions from the developer
With @SneakyThrows Lombok puts a
try-catch clause around the method and wraps each potential
exception into a RuntimeException. So the developer doesn't have to do this manually - in most
cases a global exception handling is sufficient.
Other very practical annotations are
more information can be found in the following sources.