μ€νλ§ μ²« νλ‘μ νΈ SchedulerProject κ° λλ¬λ€.
μꡬμ¬ν μ€ νμ΄μ§λ€μ΄μ
κΈ°λ₯κ³Ό μ ν¨μ± κ²μ¬ κΈ°λ₯μ λλ΄ κ΅¬ννμ§ λͺ»νλ€.
κ°λ° λμ€ λ§νλ μκ°μ΄ κ³μν΄μ λ°μνκ³ κ·Έλλ§λ€ μΆκ° νμ΅κ³Ό μ¬κ°λ°μ λ°λ³΅νλ€ λ³΄λ
λ§κ°μκ°μ΄ κ°κΉμμ‘κ³ λͺ¨λ μꡬμ¬νμ ꡬννκΈ°λ νλ€λ€κ³ νλ¨νλ€.
κ·Έλλ 첫 μ€νλ§ νλ‘μ νΈμΈ λ§νΌ κ°λ³κ² νκ³ ν΄λ³΄κ³ λ§λ¬΄λ¦¬νκ³ μ νλ€.
Controller, Service, Repository λ₯Ό κ±°μΉλ λ°μ΄ν°μ νλ¦κ³Ό κ°κ°μ μ±
μμ λν΄ κΉμ΄ κ³ λ―Όν΄ λ³Ό μ μλ μκ°μ΄μλ€.
//Service Method
@Override
public ScheduleResponseDto findScheduleById(Long id) {
return scheduleRepository.findScheduleById(id)
.stream().findAny().orElseThrow(() -> new ScheduleNotFoundException("ν΄λΉ idμ μΌμΉνλ μΌμ μ΄ μμ΅λλ€."));
}
//Repository Method
@Override
public List<ScheduleResponseDto> findScheduleById(Long id) {
return jdbcTemplate.query("select * from schedule where scheduleId=? order by lastModifiedDate desc", scheduleMapper(), id);
}
Repository λ μ£Όμ΄μ§ idλ‘ μΏΌλ¦¬λ₯Ό μ€ννκ³ κ²°κ³Όλ₯Ό List<ScheduleResponseDto> ννλ‘ λ°ννλ€.
νλ‘μ νΈλ₯Ό μμν μ¦μμ 쿼리 κ²°κ³Όκ° λΉμ΄ μμ κ²½μ° Repository μμ μμΈλ₯Ό λ°μμν€λ κ²μ΄ μμ°μ€λ½λ€κ³ μκ°νλ€.
νμ§λ§ κ° κ³μΈ΅μ μ±
μμ λν΄ κ³ λ―Όνλ€ λ³΄λ μμΈ μ²λ¦¬μ μ±
μμ Service μ μλ€λ κ²°λ‘ μ λλ¬νλ€.
Repository λ μ μμ μΌλ‘ DB μ μ κ·Όνκ³ κ·Έ κ²°κ³Όλ‘ λΉ λ¦¬μ€νΈλ‘ λ°ννλ€.
μ΄λ λ¨μν ν΄λΉ 쑰건μ λ§λ λ°μ΄ν°κ° μλ€λ μλ―ΈμΌ λΏ, Repository λ μμ μ μ±
μμ λ€ν κ²μ΄λ€.
κ·Έλ¦¬κ³ μ΄ κ²°κ³Όλ₯Ό λ°κ³ λΉμ¦λμ€ λ‘μ§μ νλ¦μ λ°λΌ μμΈλ₯Ό λ°μμν¬μ§ κ·Έλλ‘ μ²λ¦¬ν μ§λ Service μ μν μΈ κ²μ΄λ€.
μ΄μ²λΌ κ³μΈ΅λ§λ€ μ±
μμ λͺ
νν λλλ μΌμ λ¨μν ꡬνλ³΄λ€ ν¨μ¬ μ΄λ ΅κ³ λ§μ κ³ λ―Όμ νμλ‘ νλ€.
νμ§λ§ κ·Έλ° κ΅¬μ‘°μ κ³ λ―Όκ³Ό λ°λ³΅μ΄ κ²°κ΅ λ λμ μ€κ³λ₯Ό λ§λ€μ΄κ°λ€λ κ±Έ μ΄λ² νλ‘μ νΈλ₯Ό ν΅ν΄ μ§μ λλ μ μμλ€.
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler
public ResponseEntity<ErrorResponse> handleUserNotFoundException(UserNotFoundException ex) {
ErrorResponse response = new ErrorResponse(ex.getMessage(), HttpStatus.NOT_FOUND.value());
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
}
@ExceptionHandler
public ResponseEntity<ErrorResponse> handleScheduleNotFoundException(ScheduleNotFoundException ex) {
ErrorResponse response = new ErrorResponse(ex.getMessage(), HttpStatus.NOT_FOUND.value());
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
}
//μΈμ¦μ€ν¨
@ExceptionHandler
public ResponseEntity<ErrorResponse> handlePasswordMismatchException(PasswordMismatchException ex) {
ErrorResponse response = new ErrorResponse(ex.getMessage(), HttpStatus.UNAUTHORIZED.value());
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(response);
}
//λ‘μ§μ μμΈκ° λ°μν μΌμ μμ§λ§ μμΈκ° ν°μ§κ²½μ° μλ²λ DBμ λ¬Έμ μΌ νλ₯ μ΄ μμ 500μλ¬ λ¦¬ν΄
@ExceptionHandler
public ResponseEntity<ErrorResponse> handleQueryFailedException(QueryFailedException ex) {
ErrorResponse response = new ErrorResponse(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR.value());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
}
}
@Getter
public class ErrorResponse {
private String message;
private int status;
private LocalDateTime timestamp;
public ErrorResponse(String message, int status) {
this.message = message;
this.status = status;
this.timestamp = LocalDateTime.now();
}
}
@ControllerAdvice μ΄λ
Έν
μ΄μ
μ μ¬μ©ν΄ μ μ μμΈ μ²λ¦¬ νΈλ€λ¬λ₯Ό ꡬννλ€.
κΈ°μ‘΄μ try-catch λ°©μμ΄ μλ 컨νΈλ‘€λ¬ λ¨μμ λ°μν μ μλ μμΈλ₯Ό μ€μ μ§μ€νμΌλ‘ μ²λ¦¬νκΈ° μν ꡬ쑰μ΄λ€.
λΆνμν μμΈ ν΄λμ€λ₯Ό 무λΆλ³νκ² λ§λ€μ§ μλλ‘ κ³ λ―Όνμ¬ νλ‘μ νΈμ κΌ νμνκ³ μκ°λλ μμΈλ€λ§ 컀μ€ν
μμΈλ‘ μ μνλ€.
κ° ExceptionHandler λ ν΄λΉ μμΈμν©μ λ§λ HTTP μνμ½λμ μλ¬ λ©μΈμ§λ₯Ό λ΄μ ErrorResponse λ₯Ό λ°ννλλ‘ μ€κ³νμλ€.
κΈ°λ³Έ μλ΅ μ 보λ§μΌλ‘λ 무μν λ°μν μ μλ λ€μν μμΈ μν©μ μΌκ΄λκ² κ΅¬λΆνκ³ μ²λ¦¬νκΈ° μ΄λ ΅λ€.
κ·Έλ κΈ° λλ¬Έμ μμΈλ₯Ό μ§μ μ μνκ³ νμν μ 보λ₯Ό λ΄μ 컀μ€ν
μμΈλ‘ μ²λ¦¬νλ κ²μ κ°λ°μκ° μ£Όλμ μΌλ‘ μμΈ νλ¦μ μ€κ³νλ μ€μν κΈ°μ μ΄λΌκ³ μκ°νλ€.
Scheduler νλ‘μ νΈ