문자메일 2023. 1. 15. 02:58

패키지 구성 기본 뼈대

- dto

-- <entity 명칭>

--- request

--- response

 

 

각 Entity 별로 사용하는 request와 response dto 객체를 위 정의한 경로에 만들어서 관리

 

기본적으로 DTO Request 객체는 아래와 같이 별 다른 메서드 없이 간결하게 작성하여 사용

@AllArgsConstructor
@NoArgsConstructor
@Getter
public class SampleEntityRequest {

    private Long id;

    private String name;
}

 

 

기본적으로 DTO Response 객체는 편의상 Entity 인스턴스를 인자로 받아서 Response DTO 객체로 반환하는 static Method를 많이 구현하여 사용하는 경향 자주 보임

@Data
@AllArgsConstructor
public class SampleEntityResponse {

    private Long id;
    private String name;

    public static SampleEntityResponse fromSampleEntity(SampleEntity sampleEntity){
        return new SampleEntityResponse(
                sampleEntity.getSampleEntityId(),
                sampleEntity.getName()
        );
    }

}