코드리뷰
만약 내가 user에게 id, name, email, password를 받는다고 치자. 그러면 User Entity에서 멤버 변수로 받게된다.
public class User {
private Long id;
private String name;
private String email;
private String password;
.
.
.
}
@GetMapping("/user/{id}")
@ResponseStatus(HttpStatusOK)
public User myPage(@PathVariable("id") Long id) {
return userService.myPage(id);
}
이런식으로 Entity와 Controller가 만들어지는데, 이때 Controller는 클라이언트 요청에 대한 응답으로 User를 넘겨주게된다. 그런데 이때 다음과 같은 문제점이 생긴다.
password
데이터까지 응답으로 보내준다.public class UserResponseDto {
private String name;
private String email;
}
@Getmapping("/user/{id}")
@ResponseStatus(Httpstatus.OK)
public UserResponseDto mypage(@PathVariable("id") Long id) {
return userService.myPage(id);
}
이렇게 DTO를 사용하면 요청에 맞는 정보만 응답할 수 있다.
DTO는 클라이언트 요청에 포함된 데이터를 담아 서버측에 전달하고, 서버측의 응답 데이터를 담아 클라이언트에 전달하는 계층잔 전달자 역할을 한다
➲ 참고 자료 : DTO에 대하여