ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Spring Boot] REST API 만들기(15) - Lombok 적용
    Spring Boot/2.4.x - REST API 만들기 2020. 6. 3. 17:55
    반응형

    Lombok은 DTO, VO, Entity 관련 클래스 생성 시 Getter, Setter, ToString 메서드 등 반복되는 코드를 어노테이션으로 대체하여 컴파일 시에 코드를 생성해주는 자바 라이브러리입니다.

     

    1. 의존성 추가

    pom.xml에 Lombok에 대한 의존성을 추가하세요.

    pom.xml

    더보기
    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
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.4.8</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.api</groupId>
        <artifactId>board</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>board</name>
        <description>Demo project for Spring Boot</description>
        <properties>
            <java.version>1.8</java.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
     
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            
            <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-configuration-processor -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
                <optional>true</optional>
            </dependency>
            
            <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>2.1.4</version>
            </dependency>
             
            <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
            
            <!-- https://mvnrepository.com/artifact/org.bgee.log4jdbc-log4j2/log4jdbc-log4j2-jdbc4.1 -->
            <dependency>
                <groupId>org.bgee.log4jdbc-log4j2</groupId>
                <artifactId>log4jdbc-log4j2-jdbc4.1</artifactId>
                <version>1.16</version>
            </dependency>
            
            <!-- JUnit4 사용하기 위해서 Vintage Engine 모듈을 제외 -->
            <dependency>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.hamcrest</groupId>
                        <artifactId>hamcrest-core</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            
            <!-- https://mvnrepository.com/artifact/org.springframework/spring-oxm -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-oxm</artifactId>
            </dependency>
            
            <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.module/jackson-module-jaxb-annotations -->
            <dependency>
                <groupId>com.fasterxml.jackson.module</groupId>
                <artifactId>jackson-module-jaxb-annotations</artifactId>
            </dependency>
            
            <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>2.9.2</version>
            </dependency>
            
            <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>2.9.2</version>
            </dependency>
            
            <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
            </dependency>
            
        </dependencies>
     
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
     
    </project>
    cs

     

    2. Lombok 설치

    명령 프롬포트 창에서 명령어 "java -jar lombok-1.18.12.jar"를 입력하면, Installer가 실행됩니다.

     

    Installer 실행된 후, IDE를 찾지 못한 경우에는 "Specify location..."을 클릭하여 사용하는 IDE를 추가해주세요. IDE를 선택한 후 Install/Update를 클릭하면 Lombok 설치가 완료됩니다.  

     

     설치가 완료되면 IDE를 재시작 해주세요.

     

    SpringToolSuite4.ini 또는 eclipase.ini 파일을 확인하면 lombok가 추가된 것을 확인할 수 있습니다.

     

    3. Lombok 어노테이션

    @ToString 모든 필드를 출력하는 toString() 메소드를 생성합니다. @ToString(exclude={“제외값”})으로 제외시키고 싶은 값을 설정할 수 있습니다.

    @EqualsAndHashCode equals(), hashCode() 메소드를 생성합니다.

    @Getter , @Setter getter() setter() 메소드를 생성합니다.

    @RequiredArgsConstructor 모든 멤버 변수를 초기화시키는 생성자를 생성합니다.

    @Data 다음 어노테이션을 모두 한번에 처리합니다. 

    @ToString @EqualsAndHashCode @Getter(모든 필드) @Setter(모든 필드 non-final) @RequiredArgsConstructor 

    Lombok Documentation : https://projectlombok.org/features/all

     

    4. Lombok 적용

    Board.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
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    package com.api.board.domain;
     
    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.XmlType;
     
    import io.swagger.annotations.ApiModel;
    import io.swagger.annotations.ApiModelProperty;
    import lombok.Getter;
    import lombok.Setter;
    import lombok.ToString;
     
    @ApiModel(value = "게시글 정보 : Board", description = "게시글 정보")
    @XmlRootElement(name = "board")
    @XmlType(propOrder = {"board_seq""board_re_ref""board_re_lev""board_re_seq""board_writer""board_subject""board_content""board_hits""del_yn""ins_user_id""ins_date""upd_user_id""upd_date"})
    @Getter @Setter @ToString
    public class Board {
     
        @ApiModelProperty(value = "게시글 번호")
        int board_seq;
        @ApiModelProperty(value = "게시글 그룹 번호")
        int board_re_ref;
        @ApiModelProperty(value = "게시글 답변 글의 깊이")
        int board_re_lev;
        @ApiModelProperty(value = "게시글 답변 글의 순서")
        int board_re_seq;
        @ApiModelProperty(value = "게시글 작성자")
        String board_writer;
        @ApiModelProperty(value = "게시글 제목")
        String board_subject;
        @ApiModelProperty(value = "게시글 내용")
        String board_content;
        @ApiModelProperty(value = "게시글 조회수")
        int board_hits;
        @ApiModelProperty(value = "게시글 삭제 유무")
        String del_yn;
        @ApiModelProperty(value = "게시글 입력자 ID")
        String ins_user_id;
        @ApiModelProperty(value = "게시글 입력 일시")
        String ins_date;
        @ApiModelProperty(value = "게시글 수정자 ID")
        String upd_user_id;
        @ApiModelProperty(value = "게시글 수정 일시")
        String upd_date;
    }
    cs

     

    Boards.java

    더보기
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    package com.api.board.domain;
     
    import java.util.List;
     
    import javax.xml.bind.annotation.XmlRootElement;
     
    import io.swagger.annotations.ApiModel;
    import io.swagger.annotations.ApiModelProperty;
    import lombok.Getter;
    import lombok.Setter;
    import lombok.ToString;
     
    @ApiModel(value = "게시글 목록 : Boards", description = "게시글 목록")
    @XmlRootElement(name = "boards")
    @Getter @Setter @ToString
    public class Boards {
     
        @ApiModelProperty(value = "게시글 목록")
        private List<Board> boards;
    }
    cs

     

    @Getter, @Setter 어노테이션 적용 후 Outline을 확인하면 getter/setter가 추가된 것을 확인할 수 있습니다.

     

    소스 코드는 Github Repository - https://github.com/tychejin1218/api-board_v1 (branch : section15) 를 참조하세요.
    Github에서 프로젝트 가져오기 - 
    https://tychejin.tistory.com/33

     

    반응형

    댓글

Designed by Tistory.