Home Lombok Lombok + Spring Boot Example

Lombok + Spring Boot Example

Project Lombok is a mature library that reduces boilerplate code. In this tutorial we will walk through a simple Lombok Spring Boot example application that integrates with Lombok to demonstrate how to start working with Spring Boot and Lombok together.

1. Maven dependencies for Spring Boot and Lombok

Following dependencies are used to work with Spring Boot and Lombok and for testing Spring components. Make sure you already installed lombok setup for your IDE. To Setup in Eclipse or in Spring Tool Suite refer to our Lombok Maven example setup with Eclipse.




		<!-- Jupiter API for writing tests -->

2. Project Structure

Spring Boot Lombok Example

2. Spring Components + Lombok

2.1. MessageDto class annotated with Lombok’s @Data annotation. Simply annotating a class with @Data causes Lombok to generate getters and setters for each of the nonstatic class fields and a class constructor, as well as the toString()equals(), and hashCode() methods. It also creates a constructor that accepts any final fields or those annotated with @NonNull as arguments.

Lombok @Data is equivalent to combination of Lombok’s @Getter + @Setter + @RequiredArgsConstructor + @ToString + @EqualsAndHashCode

@Data // lombok data annotation
public class MessageDto {
	private Long id;
	private String message;


2.2. Using MessageDto in MessageService.

public class MessageService {
	public MessageDto getSubscriptionMessage(@NonNull String user) {
		MessageDto message = new MessageDto();
		message.setMessage("Hello "+user+", Thanks for the subscription!");
		return message;

2.3. Creating log messages using Lombok in Spring Controller.

Used Lombok @Slf4j fro logging.

@Slf4j // lombok
public class HelloController {
	private MessageService messageService;
	public MessageDto sayHello(@RequestParam String user) {
		log.info("test info log: "+ user);
		MessageDto message =  messageService.getSubscriptionMessage(user);
		log.debug("test debug log : "+message.toString());
		return message;


2.4. Testing Spring Controller

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class Spring_boot_junit5_api_test {
	private TestRestTemplate restTemplate;

	 @DisplayName("/hello rest api test ")
	 void testMessage() {
		 String user = "Peter";
		 URI targetUrl= UriComponentsBuilder.fromUriString("/hello")                             
				    .queryParam("user", user)                               
	  MessageDto message = this.restTemplate.getForObject(targetUrl, MessageDto.class);
	  assertEquals("Hello "+user+", Thanks for the subscription!", message.getMessage());


2.5. Check log messages in Output Results

Test results were passed and following is console output and log info.

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 :: Spring Boot ::        (v2.2.2.RELEASE)

20:34:11.322 [main] INFO  c.j.demo.Spring_boot_junit5_api_test - Starting Spring_boot_junit5_api_test on ADMINA565 with PID 12988 (started by admin in \Workspaces\Lombok\spring-boot-lombok-example)
20:34:11.333 [main] DEBUG c.j.demo.Spring_boot_junit5_api_test - Running with Spring Boot v2.2.2.RELEASE, Spring v5.2.2.RELEASE
20:34:11.335 [main] INFO  c.j.demo.Spring_boot_junit5_api_test - No active profile set, falling back to default profiles: default
20:34:28.117 [main] INFO  c.j.demo.Spring_boot_junit5_api_test - Started Spring_boot_junit5_api_test in 17.961 seconds (JVM running for 21.668)
20:34:30.716 [http-nio-auto-1-exec-1] INFO  c.j.controller.HelloController - test info log: Peter
20:34:30.719 [http-nio-auto-1-exec-1] DEBUG c.j.controller.HelloController - test debug log : MessageDto(id=1, message=Hello Peter, Thanks for the subscription!)

3. Conclusion

In this article we walked through a simple spring boot example application, which helps to start working with Lombok and Spring boot together. You can refer Delombok Maven example to see how looks like lombok generated code for your Lomboked classes.

You can get source code at our github repository.

You might be interested in our other following Lombok Tutorials :

  1. Lombok @Getter @Setter and lazy getters
  2. Lombok @ToString
  3. Lombok @EqualsAndHashCode
  4. Lombok @AllArgsConstructor
  5. Lombok @NoArgsConstructor
  6. Lombok @RequiredArgsConstructor
  7. Lombok @NonNull
  8. Lombok @Value
Satish Varma
Satish Varmahttps://javabydeveloper.com
Satish is post graduated in master of computer applications and experienced software engineer with focus on Spring, JPA, REST, TDD and web development. Also founder of javabydeveloper.com. Follow him on LinkedIn or Twitter or Facebook


Please enter your comment!
Please enter your name here

Stay in Touch


Related Articles