Using Project Lombok in your Spring Boot Project

How to cut down some of the boilerplate Java code in your next Spring Application

In this article, we get to take a look at an awesome little project called Project Lombok. If you have been following me for awhile now then you already know I am a huge fan of the Groovy Programming Language. It takes a lot of the things I don’t like in Java and makes them better. Like, way better! Groovy is also really GOOD at cutting down the amount of ceremonious code you have to write. 

If you are working on a Java project and are looking for a way to cut down some of the noise in your code I have a great solution for you. In this article, we are going to look at Project Lombok and dive into what it is and how to use it in your next Spring Boot Application. 

What is Project Lombok?

As I said in the introduction to this article, one of the biggest complaints against Java is how much noise can be found in a single class. Project Lombok saw this as a problem and aims to reduce the noise of some of the worst offenders by replacing them with a simple set of annotations. 

One of the worst offenders in Java are classes that have a ton of fields in them and consist of a getter & setter for each property. I will refer to these “types” of classes as data classes going forward.

IDEs have a come a long way and help us generate this code but every single time you make a change to that data class you will need to go back and regenerate that code or even worse, write it yourself. In this example below, straight from the documentation, all of the getters, setters and a to string method were created for us just by adding the @Data annotation to our Data class.

Project Lombok in Action

Project Lombok in Action

Pretty awesome right? Project Lombok consists of more than just this single annotation. If you want to learn about them please read through the documentation. For now, we are just going to run through a basic demo. 

Project Lombok Demo

Before Project Lombok

We are going to create a new Spring Boot application. If you want to grab the project source code you can do so here. You can select Lombok right from the core section of the Spring Initilizr. 

I am going to create a new user object with a bunch of fields. 

That looks like a pretty clean class, but we aren’t done yet. Now we need to create a getter & setter for each of those properties. We also would probably want to add a toString, equals and hashCode method to our class. If you didn’t know this already, you can generate these using cmd + n on the mac (cntrl+n on a PC).

Now our class is correct but that is a whole bunch of unwanted code. Not to mention what a maintenance nightmare this is. Every single time this class changes we need to update the getters, setters, and methods by generating them over or even worse, actually writing code. 

After Project Lombok

Now that we have Lombok in our application we can reduce this noise. Just add that @Data annotation to your class. This will create a getter and setter for each property defined in your class. It will also create a to string, equals and hash code method. You can get pretty granular control with some of the annotations so again I would refer you to the documentation

The great thing about Lombok is that it integrates well with the IDE. I am using IntelliJ but this will work in Eclipse as well. If you look over at your project structure you can see that all of these methods now exist without us having to write them. We also don’t have to guess as to whether it worked or not, we can clearly see that it did. Not to mention, look how clean our class looks! 

Conclusion

There is absolutely no reason you shouldn’t be using Project Lombok today. If it’s not a brand new project you can simply drop in the dependency and go. I hope you found this tutorial useful and I want to leave you with a question to hopefully kick start a discussion. 

Question: What are some of your favorite tools & libraries to help in the creation of better, cleaner code? 

Hi, I’m Dan Vega. I am a Software Engineer living just outside of one of my favorite places in the world, Cleveland Ohio. I am a self-taught programmer who firmly believes that you can accomplish anything in this life if you put your mind to it, roll up your sleeves and are willing to put in the work.

I have been writing software for over 17+ years now and I use my knowledge, passion, and influence to help other developers achieve their goals. I teach online and currently, have over 22,000 students.

When I am not writing software I have a number of things that really keep me busy. I love to read books, listen to podcasts and watch online courses. I have a real thirst for knowledge and it consumes me every single day of the week. I also love to run & lift heavy weights which keep me balanced throughout the week.

Please note: I reserve the right to delete comments that are offensive or off-topic.

  • Manvendra Singh

    Nice one. Thanks.

  • Joe Swansen

    Hey Dan,
    thanks for your blog and especially for this article.

  • nAtxO Pi

    Thanks Dan, great post!.. Let me ask you a question, which choice do you think is better for a SpringBoot project, Groovy or Lombok?