서비스 계층에서 예외는 모두 커스텀 예외 클래스로 exception 패키지 밑에 만들어 작성하도록 한다.
public User getUserById(Long id) {
return userRepository.findById(id)
.orElseThrow(() -> new EntityNotFoundException("해당 유저가 존재하지 않습니다. ID=" + id));
}
이후 EntityNotFoundException에서 RuntimeException을 상속하도록 한다.
package com.yesul.exception.handler;
public class EntityNotFoundException extends RuntimeException {
public EntityNotFoundException(String message) {
super(message);
}
}
이후 메서드 명 위쪽에는 해당하는 예외를 @ExceptionHandler밑에 지정
@ExceptionHandler(EntityNotFoundException.class)
public String handleEntityNotFound(EntityNotFoundException e,HttpServletRequest request) {
// 로그 남기기
log.warn("[EntityNotFoundException] {} {} - {}", request.getMethod(), request.getRequestURI(), e.getMessage());
return "404.html";
}
그리고 401,403,404,409,500 Error Page 중 해당하는 url로 리턴

단일 책임원칙에 기반해서 DTO를 Request 와 Response DTO로 분리
@Getter
@Builder
@NoArgsConstructor(access = AccessLevel.PROTECTED) --> 관련되어 찾아보시는걸 권장드립니다.
@AllArgsConstructor
@Schema(description = "커리어톡 게시글 생성 요청 DTO")
public class CreateCareertalkRequestDto {
private String category;
}
<aside> 💡
https://bombo96.tistory.com/66
</aside>