Save time using Lombok + MapStruct
Write code is great, but sometimes is better generate code than type them. For this article I’ll present some options, such as Lombok and MapStruct.
As I said before, sometimes writing a repeated piece of code is not a good idea, since this is a DRY (Don’t Repeat Yourself).
After the arrival of the Java Annotations changed the game.
Lombok uses the Java Annotations feature to generate code for us ❤️, not all code but the repeated code.
On the other hand we have the MapStruct to map objects, in this article to map the entity to client response.
1.1. Add the dependency
1.2. At the code
Add the annotation @Slf4j and the object log will be available for use. Look at line 3, when the annotation was declared and line 20, where the object is available for use.
2021-06-19 21:44:01.475 INFO 4278 --- [nio-8092-exec-1] com.costa.luiz.lombok.CountryController : Received 3 countries
Writes for you all the following annotation at once: Setter/Getter/ToString/EqualsAndHashCode/RequiredArgsConstructor
Useful for JPA entities like the entity Country.
Builder it is. Avoid generating again the Builder when an attribute is modified. I’ll modify the class above to show the annotation, and add the @NoArgsConstructor and @AllArgsConstructor too, since it is required for JPA.
To use MapStruct, at least two steps are required.
2.1 Add the dependency and add it to the build section.
- Build section. To make Lombok and MapStruct work together is requires the code between the commented excerpts.
<!-- START - To work together-->
<!-- END - To work together-->
2.2. At the code
Declare an interface annotated with
@Mapper, an instance, line 4, and the method to map the objects, line 6.
This will generate the class below. ⚠️ The class shouldn’t be modified.
@Bean for CountryMapper
After the configuration is time to take advantage on Rest Controller
The class below shows a test for the Mapper
After some classes this is the final relationship between the classes
To run this example in your local environment you need to have:
- Java 11+
Code by configuration is a good practice to tackle repeated tasks, although for complex projects and many Annotation Processors can be hard, but easier than code everything 😉.
Project to store the youtube channel code. Contribute to luizgustavocosta/16-bits-spring development by creating an…
XDoclet: Attribute-Oriented Programming - Welcome
XDoclet is an open source code generation engine. It enables Attribute-Oriented Programming for java. In short, this…
Apache Ant is a Java library and command-line tool whose mission is to drive processes described in build files as…
Annotations, a form of metadata, provide data about a program that is not part of the program itself. Annotations have…
Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java. Never…
MapStruct and Lombok not working together
Tech Stack being used : Java 8 MapStruct : 1.2.0.Final Lombok: 1.16.18 IDE: IntelliJ - Lombok Plugin already installed…
AssertJ - fluent assertions java library
entry point for all assertThat methods and utility methods (e.g. entry) import static…