서비스 계층에서 데이터 액세스 계층과 연동해 비즈니스 로직을 처리하기 위해 필요한 데이터를 담는 클래스
Member class
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@NoArgsConstructor // 파라미터가 없는 기본 생성자를 자동 생성
@AllArgsConstructor // 모든 멤버 변수를 파라미터로 갖는 생성자를 자동 생성
public class Member {
private long memberId;
private String email;
private String name;
private String phone;
}
@RestController
@RequestMapping("/v3/members")
@Validated
public class MemberController {
private MemberService memberService;
// (1)
public MemberController(MemberService memberService) {
this.memberService = memberService;
}
@PostMapping
public ResponseEntity postMember(@Valid @RequestBody MemberPostDto memberDto) {
// (2)
Member member = new Member();
member.setEmail(memberDto.getEmail());
member.setName(memberDto.getName());
member.setPhone(memberDto.getPhone());
// (3)
Member response = memberService.createMember(member);
return new ResponseEntity<>(response, HttpStatus.CREATED);
}
@PatchMapping("/{member-id}")
public ResponseEntity patchMember(@PathVariable("member-id") @Positive long memberId,
@Valid @RequestBody MemberPatchDto memberPatchDto) {
memberPatchDto.setMemberId(memberId);
// (4)
Member member = new Member();
member.setMemberId(memberPatchDto.getMemberId());
member.setName(memberPatchDto.getName());
member.setPhone(memberPatchDto.getPhone());
// (5)
Member response = memberService.updateMember(member);
return new ResponseEntity<>(response, HttpStatus.OK);
}
@GetMapping("/{member-id}")
public ResponseEntity getMember(@PathVariable("member-id") @Positive long memberId) {
System.out.println("# memberId: " + memberId);
// (6)
Member response = memberService.findMember(memberId);
return new ResponseEntity<>(response, HttpStatus.OK);
}
@GetMapping
public ResponseEntity getMembers() {
System.out.println("# get Members");
// (7)
List<Member> response = memberService.findMembers();
return new ResponseEntity<>(response, HttpStatus.OK);
}
@DeleteMapping("/{member-id}")
public ResponseEntity deleteMember(@PathVariable("member-id") @Positive long memberId) {
System.out.println("# deleted memberId: " + memberId);
// (8)
memberService.deleteMember(memberId);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}
@Service
public class MemberService {
public Member createMember(Member member) {
Member createdMember = member;
return createdMember;
}
public Member updateMember(Member member) {
Member updatedMember = member;
return updatedMember;
}
public Member findMember(long memberId) {
Member member = new Member(memberId, "jungseo@gmail.com", "jungseo", "010-1234-5678");
return member;
}
public List<Member> findMembers() {
List<Member> members = List.of(
new Member(1, "jungseo@gmail.com", "jungseo", "010-1234-5678"),
new Member(2, "hgd@gmail.com", "길동", "010-1111-5555")
);
return members;
}
public void deleteMember(long memberId) {
}
}
현재 문제점
매퍼(Mapper)를 사용하여 해결 가능
DTO 클래스와 Entity 클래스를 서로 변환
응답 데이터 DTO class
@Getter
@AllArgsConstructor
public class MemberResponseDto {
private long memberId;
private String email;
private String name;
private String phone;
}
// (1)
@Component
public class MemberMapper {
// (2) MemberPostDto를 Member로 변환
public Member memberPostDtoToMember(MemberPostDto memberPostDto) {
return new Member(0L,
memberPostDto.getEmail(),
memberPostDto.getName(),
memberPostDto.getPhone());
}
// (3) MemberPatchDto를 Member로 변환
public Member memberPatchDtoToMember(MemberPatchDto memberPatchDto) {
return new Member(
memberPatchDto.getMemberId(),
null,
memberPatchDto.getName(),
memberPatchDto.getPhone());
}
// (4) Member를 MemberResponseDto로 변환
public MemberResponseDto memberToMemberResponseDto(Member member) {
return new MemberResponseDto(
member.getMemberId(),
member.getEmail(),
member.getName(),
member.getPhone()
);
}
}
스프링 빈으로 등록하기 위해 @Component 애너테이션 사용
Mapper class를 적용한 MemberController class
@RestController
@RequestMapping("/v4/members")
@Validated
public class MemberController {
private MemberService memberService;
private MemberMapper mapper;
// (1) DI
public MemberController(MemberService memberService, MemberMapper mapper) {
this.memberService = memberService;
this.mapper = mapper;
}
@PostMapping
public ResponseEntity postMember(@Valid @RequestBody MemberPostDto memberPostDto) {
// (2) MemberPostDto를 Member로 변환
Member member = mapper.memberPostDtoToMember(memberPostDto);
Member response = memberService.createMember(member);
// (3) Member를 MemberResponseDto로 변환
return new ResponseEntity<>(mapper.memberToMemberResponseDto(response), HttpStatus.CREATED);
}
@PatchMapping("/{member-id}")
public ResponseEntity patchMember(@PathVariable("member-id") @Positive long memberId,
@Valid @RequestBody MemberPatchDto memberPatchDto) {
memberPatchDto.setMemberId(memberId);
// (4) MemberPatchDto를 Member로 변환
Member response =
memberService.updateMember(mapper.memberPatchDtoToMember(memberPatchDto));
// (5) Member를 MemberResponseDto로 변환
return new ResponseEntity<>(mapper.memberToMemberResponseDto(response), HttpStatus.OK);
}
@GetMapping("/{member-id}")
public ResponseEntity getMember(@PathVariable("member-id") @Positive long memberId) {
Member response = memberService.findMember(memberId);
// (6) Member를 MemberResponseDto로 변환
return new ResponseEntity<>(mapper.memberToMemberResponseDto(response), HttpStatus.OK);
}
@GetMapping
public ResponseEntity getMembers() {
List<Member> members = memberService.findMembers();
// (7) List<Member>를 MemberResponseDto로 변환
List<MemberResponseDto> response = members.stream()
.map(member -> mapper.memberToMemberResponseDto(member))
.collect(Collectors.toList());
return new ResponseEntity<>(response, HttpStatus.OK);
}
@DeleteMapping("/{member-id}")
public ResponseEntity deleteMember(@PathVariable("member-id") @Positive long memberId) {
System.out.println("# deleted memberId: " + memberId);
memberService.deleteMember(memberId);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}
@Component
@Mapper(componentModel = "spring")
public interface MemberMapper {
Member memberPostDtoToMember(MemberPostDto memberPostDto);
Member memberPatchDtoToMember(MemberPatchDto memberPatchDto);
MemberResponseDto memberToMemberResponseDto(Member member);
}