1.데이터 및 구조 설계[컨트롤러 레이어]

dasd412·2022년 1월 29일
0

포트폴리오

목록 보기
8/41

DTO와 엔티티를 분리한 이유

[Spring] 엔티티(Entity) 또는 도메인 객체(Domain Object)와 DTO를 분리해야 하는 이유 - MangKyu's Diary (tistory.com)


POST와 PUT의 차이점

Http PUT 메서드는 요청 페이로드를 사용해 새로운 데이터를 생성하거나, 대상 리소스를 나타내는 데이터를 "대체"한다.

PUT 과 POST 의 차이점은 "멱등성"(동일한 요청을 한 번 보내는 것과 여러 번 연속으로 보내는 것의 응답이 똑같으면, "멱등성"을 만족)이다.

따라서 PUT 은 멱등성을 가지지만, POST 는 멱등성이 없다.


전역 예외 처리 핸들러

코드

@ControllerAdvice
//전역 예외 처리 핸들러
public class GlobalExceptionHandler {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler({IllegalArgumentException.class, IllegalStateException.class,
            TypeMismatchException.class, MissingServletRequestParameterException.class,
            JSONException.class})
    public ModelAndView handle400(Throwable throwable) {
        logger.error("Bad Request: {}", throwable.getMessage());
        return new ModelAndView("400");
    }

    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ExceptionHandler({NoHandlerFoundException.class, NoSuchElementException.class})
    public ModelAndView handle404() {
        logger.error("Not found in server");
        return new ModelAndView("404");
    }

    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public ModelAndView handle405(HttpRequestMethodNotSupportedException throwable) {
        logger.warn("Method not allowed : {}", throwable.getMethod());
        return new ModelAndView("405");
    }

    @ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
    @ExceptionHandler(HttpMediaTypeException.class)
    public ModelAndView handle415(HttpMediaTypeException throwable) {
        logger.warn("Unsupported Media Type : {}", throwable.getMessage());
        return new ModelAndView("415");
    }

    @ExceptionHandler(Exception.class)
    public ModelAndView handle500(Throwable throwable) {
        logger.error("Internal server error : {}", throwable.getMessage());
        return new ModelAndView("500");
    }
}
profile
아키텍쳐 설계와 테스트 코드에 관심이 많음.

0개의 댓글