EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Spring Tutorial Spring Boot Lombok
Secondary Sidebar
Spring Tutorial
  • Spring Boot
    • What is Spring Boot
    • Spring Boot flyway
    • Spring Boot framework
    • Spring Boot Logback
    • Spring Boot actuator endpoints
    • Spring Boot gRPC
    • Spring Boot jdbctemplate example
    • Spring Boot ehcache
    • Spring Boot Architecture
    • Spring Boot Port
    • Introduction of spring boot
    • Spring Boot ide
    • Spring Boot Netty
    • Spring Boot ORM
    • Spring Boot Versions
    • Spring Boot JUnit
    • Spring Boot Keycloak
    • Spring Boot gradle
    • Spring Boot Lombok
    • Spring Boot autowired
    • Spring Boot bean
    • Spring Boot hibernate
    • Spring Boot integration test
    • Spring Boot jdbc
    • Spring Boot MongoDB
    • Spring Boot postgresql
    • Spring Boot rest
    • Spring Boot swagger
    • Spring Boot thymeleaf
    • Spring Boot Unit Test
    • Spring Boot Webflux
    • Spring Boot webclient
    • Spring Boot kubernetes
    • Spring Boot Properties
    • Spring Boot Validation
    • Spring Boot Feature
    • Spring Boot Application
    • Spring Boot email
    • Spring Boot MVC
    • Spring Boot Exception Handling
    • Spring Boot Starter Parent
    • Spring Boot Docker
    • Spring Boot Logging
    • Spring Boot Query
    • Spring Boot Multiple Data Sources
    • Spring Boot Basic Authentication
    • Spring Boot Test
    • Spring Boot jwt
    • Spring Boot Liquibase
    • Spring Boot Prometheus
    • Spring Boot debug
    • Spring Boot GraalVM
    • Spring Boot Batch
    • Spring Boot controller
    • Spring Boot CLI
    • Spring Boot file upload
    • Spring Boot interceptor
    • Spring Boot Service
    • Spring Boot Configuration
    • Spring Boot Datasource Configuration
    • Spring Boot Annotations
    • Spring Boot Starter We
    • Spring Boot Actuator
    • Spring Boot DevTools
    • Spring Boot Repository
    • Spring Boot Dependencies
    • Spring Boot Path Variable
    • Spring Boot Microservices
    • Spring Boot Run Command
    • Spring Boot application.properties
    • Spring Boot Transaction Management
    • Spring Boot Banner
    • Spring Boot JPA
    • Spring Boot Change Port
    • Spring Boot RestTemplate
    • Spring Boot cors
    • Spring Boot HTTPS
    • Spring Boot OAuth2
    • Spring Boot Profiles
    • Spring Boot Interview Questions
    • Spring Boot filter
    • Spring boot logging level
    • Spring Boot Cache
    • Spring Boot Advantages
    • Spring Boot Scheduler
    • Spring Boot Initializr
    • Spring Boot Maven
    • Spring Boot Admin
    • Spring Boot Tomcat
    • Spring Boot WebSocket
    • Spring Boot Executable Jar
    • Spring Boot CommandLineRunner
    • Spring Boot DataSource
    • Spring Batch Scheduler
    • Spring Batch Example
    • Spring Batch Tasklet
    • Spring Batch Admin
    • Spring Batch
    • Spring Boot Qualifier
    • Spring Boot War
    • Spring Boot Test Configuration
  • Spring
    • What is Spring Framework?
    • Spring Architecture
    • What is Spring Integration?
    • IoC Containers
    • What is AOP?
    • Spring Modules
    • Spring Batch Processing
    • Spring Batch Partitioner
    • Spring Batch Job
    • Spring AOP
    • Spring Expression Language
    • Dependency Injection in Spring
    • Spring Batch Architecture
    • Spring framework Interview Questions
  • Spring Cloud Basics
    • What is Spring Cloud
    • Spring Cloud Contract
    • Spring Cloud Components
    • Spring Cloud Version
    • Spring Cloud Data Flow
    • Spring cloud stream
    • Spring Cloud Dependencies
    • Spring cloud microservices
    • spring cloud gateway
    • Spring Cloud Config
    • Spring Cloud Kubernetes
    • Spring Cloud Sleuth

Related Courses

Spring Boot Certification Course

Spring Framework Course Training

All in One Data Science Course

Spring Boot Lombok

Spring Boot Lombok

Introduction to Spring Boot Lombok

Spring boot Lombok is the tool of the java library that was used to generate code for minimizing the unused code. This library is replacing the unused code by using annotation. It will replace a number of annotations like the setter and getter method, equals, hashcode, constructor, etc. To develop a project using it, we need to add Lombok dependency in the pom.xml file of our application.

What is Spring Boot Lombok?

  • Spring boot Lombok is nothing but a small library that was used to reduce the unused code from the java applications.
  • It is automatically generating the getter and setter method for the class object by using Lombok annotation.
  • Using it, we can hook in by using the API of the annotation processor. We can pass raw source code to this for new code generation before the java compiler continues its operation.
  • We can view compiled class files of the project in the target/classes folder. We can use this in Maven and Gradle IDE.

How Does Lombok Works?

  • Basically, it is a processor of annotation which works at the time of code compilation; it will be adding code into our project classes.
  • As we know, the Java 5 version is added annotation processing. While using any type of annotation processor, the program compiler will be coming to a specific level when compiling the classes. It will give the specific processor control to process what will be done in the specified class.
  • For example, if we have added getter and setter annotation in a class by using Lombok, then at the time of compilation code compiler will give the annotation of the Lombok processor to add thing which was required for our project.
  • The below example shows how it works in the project is as follows. After creating a java file, it will convert code into a class file are as follows.

Example –

Studreq.java

@Getter
@Setter
Public class studreq
{
Private string studname;
Private string address;
}

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

  • After compiling the above code, it will create the below class files are as follows.

Example –

Studreq.class

Public class studreq
{
Private string studname;
Private string address;
Public studreq () {
}
Public string getStudname () {
Return this.studname;
}
Public string getAddress () {
Return this.address;
}
Public void setStudname (final string studname) {
This.studname = studname;
}
Public void setAddress (final string address) {
This.address = address;
}
}

As per the above example, we have seen how the spring boot Lombok annotation processor changes the code at compile time.

Spring boot Lombok – Project Structure

  • The below example shows the project structure of this application are as follows.
  • In the below project structure, we have defined the bean as per and type as a stud.
  • We are using the application name as Springbootlombok to define the project structure.

Springbootlombok

|

|— src

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,754 ratings)

|

main class

|

|—Java

|—- com.springboot.lombok

|— Controller

DefaultstudLoader

|— Config

BeanConfiguration

|— model

Stud

|—- Repository

StudRepository

|— Service

StudService

|—– resources

|——test

java

Classpath

Project

Pom.xml file

  • We are using its dependency to develop applications using Lombok.

Create Spring Boot Lombok Application

Below are examples shown to create spring boot; Lombok applications are as follows.

  • Create project template of this application by using spring initializer and give a name to project –

In the below step, we have provided project group name as com.example, artifact name as springbootlombok, project name as springbootlombok, and selected java version as 8.

Group – com.example
Artifact name – springbootlombok
Name – springbootlombok
Spring boot – 2.6.0
Project – Maven
Project Description - Project for springbootlombok
Java – 8
Dependencies – lombok, spring web
Package name - com.example.springbootlombok

Spring Boot Lombok Application Step 1

  • After generating project extract files and open this project by using spring tool suite –

After generating the project using the spring initializer in this step, we extract the jar file and open the project using the spring tool suite.

Spring Boot Lombok Application Step 2

  • After opening the project using the spring tool suite, check the project and its files –

In this step, we are checking all the project template files. We also need to check maven dependencies and system libraries.

Spring Boot Lombok Application Step 3

  • Add dependency packages –

In this step, we are adding the required dependency to our project.

Code –

<dependency>   -- Start of dependency tag.
<groupId>org.springframework.boot</groupId>   -- Start and end of groupId tag.
<artifactId>spring-boot-starter-web</artifactId>  -- Start and end of artifactId tag.
</dependency>    -- End of dependency tag.
<dependency>   -- Start of dependency tag.
<groupId>org.projectlombok</groupId>   -- Start and end of groupId tag.
<artifactId>lombok</artifactId>  -- Start and end of artifactId tag.
</dependency>    -- End of dependency tag.

Spring Boot Lombok Application Step 4

Spring Boot Lombok Example

The below step shows examples are as follows. We are using the project template of the spring boot Lombok application.

  • Create a class for application –

Code –

@Data
public class lombok {
private Long stud_id;
private String stud_address;
}

Spring Boot Lombok Example 1

  • Create a service class for this application –

Code –

@Component
public class studService {
public lombok getSubscriptionMessage (@NonNull String stud) {
lombok message = new lombok();
message.setStud_Id (Long.valueOf (1));
message.setstud_address ("Hello "+stud+", Welcome to school");
return message;
}
}

Spring Boot Lombok Example 2

  • Create controller class for this application –

Code –

@Slf4j
@RestController
public class studController {
@Autowired
private studService studservice;
private Object log;
@GetMapping("/student")
public lombok sayHello (@RequestParam String stud) {
log.info("test log: "+ stud);
lombok lom =  studService.getSubscriptionMessage (stud);
((Object) log).debug("test debug log : "+lom.toString());
return lom;
}
}

Example 3

  • Create a test controller for the application –

Code –

@SpringBootTest(webEnvironment = "")
public class Test {
@Autowired
private TestRestTemplate restTemplate;
@DisplayName ("/hello test ")
void testMessage () {
String stud = "ABC";
URI targetUrl= UriComponentsBuilder.fromUriString ("/test")
.queryParam ("user", stud)
.build()
.encode()
.toUri ();
lombok message = this.restTemplate.getForObject(targetUrl, lombok.class);
assertEquals ("Hello "+stud+", Welcome to school", message.getMessage());
}
}

Example 4

  • Run the application and check log messages

Example 5

Conclusion

Spring boot Lombok will pass the raw source code before the java compiler, so it will produce the properly compiled java code using the java compiler. It is the tool of the java library that was used to generate code to minimize the unused code.

Recommended Articles

This is a guide to Spring Boot Lombok. Here we discuss What is spring boot Lombok and the project structure, along with the examples and codes. You may also have a look at the following articles to learn more –

  1. Spring Boot Properties
  2. Spring Boot Logging
  3. Spring Boot Validation
  4. Spring Boot HTTPS
Popular Course in this category
Spring Boot Training Program (2 Courses, 3 Project)
  2 Online Courses |  3 Hands-on Projects |  22+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Spring Framework Training (4 Courses, 6 Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more