본문 바로가기

Java/Spring Boot

SpringBoot-008-lombok 이용하기

0. lombok이란?

- Model(DTO, VO, Domain) Object 생성시 property(멤버필드)에 대한 getter/setter/ToString과 같이 property에 주입하는 생성자를 annotation을 통해 코드를 줄여주는 라이브러리

 

1. lombok 설치

https://projectlombok.org/download 에서 lombok.jar 다운로드

- cmd실행 - lombok.jar 파일이 있는 경로이동 - cmd창에 java -jar lombok.jar 입력 - specify location에서 eclipse.exe 선택 - install/update 클릭

- gradle.build에 다음을 입력

1
2
3
4
dependencies {
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

 

2. lombok 이용

- src/main.java-com.apitest.model-SearchParamVO.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
import lombok.Data;
 
@Data
public class SearchParamVo {
    // AllArgsConstructor: parameter가 있는 생성자 생성
 
    String userId;
    String password;
    String email;
 
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

- src/main.java-com.apitest.controller-SearchParamController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.apitest.controller;
 
 
 
@RestController
@RequestMapping(path="/api")
public class SearchParamController {
 
    @RequestMapping(path="/lomboktest", method=RequestMethod.GET)
    public SearchParamVo lombokTest(SearchParamVo vo) {
        
        System.out.println(vo.getUserId());
        
        return vo;
    }
    
    @RequestMapping(path="/lomboktest2", method=RequestMethod.GET)
    public SearchParam lombokTest2(SearchParam vo) {
        
        System.out.println(vo.getUserId());
        
        return vo;
    }
 
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

- 잘 실행됨 ㅎㅎ

반응형