public static CommentResponseDto toDto(Comment comment){
return new CommentResponseDto(
comment.getId(),
comment.getTodo().getId(),
comment.getContent(),
comment.getMember().getId(),
comment.getMember().getUsername(),
comment.getCreatedAt());
}
μ΄λ²μ jpaλ₯Ό μ΄μ©ν μΌμ κ΄λ¦¬ νλ‘κ·Έλ¨μ λ§λ€λ©΄μ responseDtoμ λ€μκ³Ό κ°μ΄ μ μ λ©μλλ₯Ό μμ±νμ¬ κ°λ°μ νλ€.
κ·Έλ°λ° μ΄λ€ λΆμκ² static λ©μλ μ¬μ© μ΄μ κ° λκ°μ?
λΌλ μ§λ¬Έμ λ°μλ€.
κ·ΈλΉμ λλ λ©μλλ₯Ό μμ±νμ¬ μ¬μ©νλ©΄ μ½λκ° κ°κ²°ν΄μ§λ€
λΌκ³ λ§ μκ³ μμΈν μμ§ λͺ»νλ€.
λλ¬Έμ μ’ λ μμΈν 곡λΆν΄λ³΄κ³ μ νλ€.
μμ
public MemberResponseDto save(CreateMemberCommand command) {
if (memberRepository.findByEmail(command.getEmail()).isPresent()) {
throw new MemberException(MemberErrorCode.ALREADY_REGISTER_MEMBER);
}
Member member = new Member(command.getUsername(), command.getEmail(), encoder.encode(command.getPassword()));
memberRepository.save(member);
// β static λ©μλ μμ λ
return new MemberResponseDto(member.getId(), member.getUsername(), member.getEmail());
// β static λ©μλ μμ λ
return MemberResponseDto.toDto(member)
}
public MemberResponseDto save(CreateMemberCommand command) {
if (memberRepository.findByEmail(command.getEmail()).isPresent()) {
throw new MemberException(MemberErrorCode.ALREADY_REGISTER_MEMBER);
}
Member member = new Member(
command.getUsername(),
command.getEmail(),
encoder.encode(command.getPassword())
);
memberRepository.save(member);
// β static λ©μλ μμ λ
return MemberResponseDto.toDto(member)
}
public List<CommentResponseDto> findAll(){
return commentRepository.findAll()
.stream()
// β μ¬μ¬μ©κ°λ₯!
.map(CommentResponseDto :: toDto)
.toList();
}
@GetMapping("/{id}")
public ResponseEntity<CommentResponseDto> findById(@PathVariable Long id){
Comment comment = commentService.findById(id);
return new ResponseEntity<>(
// β CommentResponseDto κ°μ²΄ μμ±ν΄μ μ¬μ©!
new CommentResponseDto(
comment.getId(),
comment.getTodo().getId(),
comment.getContent(),
comment.getMember().getId(),
comment.getMember().getUsername(),
comment.getCreatedAt()), HttpStatus.OK);
}
μ¬κΈ°κΉμ§ μμ보λ λ¬Έλ κΆκΈν¨μ΄ μκ²Όλ€.
λ΄κ° μ§κΈκΉμ§ μκΈ°λ‘λ μ μ λ©μλλ νλ‘μ νΈκ° μμλ¨κ³Ό λμμ λ©λͺ¨λ¦¬μ ν λΉλλ κ²μΌλ‘ μκ³ μλλ° κ·Έλ λ€λ©΄ responseDtoκ°λ null
μ΄ λ§λ κ² μλκΉ?
κ²°λ‘ λΆν° λ§νμλ©΄ NO
μλ€.
π μ΄μ
1. static λ©μλ
λ νλ‘κ·Έλ¨ μ€νκ³Ό λμμ λ©μλ μμμ λ±λ‘
2. μ΄ν λ΄λΆ λ‘μ§μ΄ νΈμΆλκΈ° μ κΉμ§ μ€νλμ§ μμ
β
κ²°λ‘
ν΄λΉ λ©μλκ° νΈμΆ λ μ€λΉ
κ° μλ£λμλ€λ μλ―Έ, μ€ν
λλ κ²μ μλλ€!
λ°λΌμ κ°μ΄ null
μ΄ λμ€μ§ μλλ€!
λ€μκ³Ό κ°μ΄ νΉμ κ°μ²΄μ κ΄κ³ μμ΄ λμν λ static
μΌλ‘ μ μΈ.
System.out.println("κ°μ²΄ μμ΄ νΈμΆ!!");
βοΈ κ°μ²΄ μμ± μμ΄ μ
λ ₯κ°μ λ³νν λ
βοΈ μ
λ ₯κ°μ μλ‘μ΄ κ°μ²΄λ‘ λ§λ€μ΄ λ°νν λ
public static CommentResponseDto toDto(Comment comment) {
return new CommentResponseDto(
comment.getId(),
comment.getTodo().getId(),
comment.getContent(),
comment.getMember().getId(),
comment.getMember().getUsername(),
comment.getCreatedAt()
);
}
βοΈ κ°μ²΄ μμ± μμ΄ μ
λ ₯κ°μ λ³νν λ
βοΈ μ¬λ¬ ν΄λμ€μμ μ¬μ©λ κ°λ₯μ±μ΄ μμλ
@ToString
@Getter
@AllArgsConstructor
public static class ExceptionResponse{
private CommentErrorCode errorCode;
private String message;
}
λͺ¨λ λ©μλμ staticμ λ¨μ©νλ건 λΉμ°ν μλλ€!
κ·Έλ λ€λ©΄ μ΄λ¨ λ static
λ©μλλ₯Ό μ¬μ©νλ©΄ μλ κΉ?
public class Caculator {
private int count = 0;
public static void add(int num1, int num2) {
count = num1+num2; // β μΈμ€ν΄μ€ λ³μ μ¬μ© λΆκ°!
}
}
νλμ μ½λλ₯Ό μμ±νλλΌλ μ’ λ μμΈν 곡λΆλ₯Ό ν νμμ±μ΄ μλ€.
staticμ μ μ¬μ©νλ©΄ λ³΄λ€ μ’μ κ°λ°μ ν μ μμ§λ§ λ무 λ¨μ©νλ©΄ μλ¬ νν + λ©λͺ¨λ¦¬ λλΉκ° λ°μνλ€. μ μκ°νκ³ μ¬μ©νμ!