@ExceptionHandler(IllegalArgumentException.class)
public ResponseEntity<ExceptionResponseDto> postPatchHandle(IllegalArgumentException e){
HttpStatus httpStatus = HttpStatus.BAD_REQUEST;
ExceptionResponseDto response = new ExceptionResponseDto(httpStatus, e.getMessage());
return new ResponseEntity<>(response, httpStatus);
}
ExceptionHandler가 속해있는 controller에서 Handler에 명시된 예외 발생시 동작한다.
ResponseEntity는 response의 header, body, status를 설정할 수 있다.
전역 controller에서 예외처리를 하고 싶다면 @ControllerAdvice 같은 키워드를 공부해보자.
public class PostUpdateDto extends PostUserDto {
private String username;
private String password;
private Optional<String> title;
private Optional<String> contents;
}
public void update(PostUpdateDto requestDto){
if(Optional.ofNullable(requestDto.getTitle()).isPresent()) setTitle(requestDto.getTitle().get());
if(Optional.ofNullable(requestDto.getContents()).isPresent()) setContents(requestDto.getContents().get());
}
private static final Optional<?> EMPTY = new Optional<>(null);
public static <T> Optional<T> ofNullable(T value) {
return value == null ? (Optional<T>) EMPTY
: new Optional<>(value);
}
public boolean isPresent() {
return value != null;
}
Optional.ofNullable은 Optional로 value를 Wrapping하고 isPresent는 boolean을 반환한다.
java의 Optional chaining은 어떨까?
typescript의 Partial<T>
같은게 없을까?
lombok은 상속에 어떻게 적용될까?