티스토리 뷰
728x90
1. 직전 포스팅은 각각 Controller에 대한 예외처리만 가능하였다.
2. 전역적으로 예외처리를 하려면 별도의 클래스를 생성한다.
2-0 클래스에 @ControllerAdvice를 붙여 Controller에 대한 AOP처리임을 명시한다.
2-1 여기로 모든 예외 처리 코드들 모두 옮겨와서 정리한다.
3. 주의할 점은 이 전역 예외 처리는 @Controller를 수식한 컨트롤러에서 발생한 예외만 처리한다는 점이다.
package pe.pilseong.restbasic.rest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
@ControllerAdvice
public class StudentExceptionHandler {
@ExceptionHandler
public ResponseEntity<StudentErrorResponse> errorHandling(StudentNotFoundException e) {
StudentErrorResponse response = new StudentErrorResponse();
response.setStatusCode(HttpStatus.NOT_FOUND.value());
response.setMessage(e.getMessage());
response.setTimestamp(System.currentTimeMillis());
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
}
@ExceptionHandler
public ResponseEntity<StudentErrorResponse> errorHandling(Exception e) {
StudentErrorResponse response = new StudentErrorResponse();
response.setStatusCode(HttpStatus.BAD_REQUEST.value());
response.setMessage(e.getMessage());
response.setTimestamp(System.currentTimeMillis());
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}
}
728x90
'Spring > Spring REST' 카테고리의 다른 글
Spring : RestTemplate with Java Config - CRUD 클라이언트 구현 (0) | 2020.05.22 |
---|---|
Spring : REST + Hibernate with Java Config - CRUD 서비스 서버 구현 (0) | 2020.05.22 |
Spring : REST 컨트롤러 예외처리 (0) | 2020.05.21 |
Spring : REST - @PathVariable 사용하기 (0) | 2020.05.21 |
Spring : REST - Collection 객체 전송하기 (0) | 2020.05.21 |
댓글
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
- 도커 개발환경 참고
- AWS ARN 구조
- Immuability에 관한 설명
- 자바스크립트 멀티 비동기 함수 호출 참고
- WSDL 참고
- SOAP 컨슈머 참고
- MySql dump 사용법
- AWS Lambda with Addon
- NFC 드라이버 linux 설치
- electron IPC
- mifare classic 강의
- go module 관련 상세한 정보
- C 메모리 찍어보기
- C++ Addon 마이그레이션
- JAX WS Header 관련 stackoverflow
- SOAP Custom Header 설정 참고
- SOAP Custom Header
- SOAP BindingProvider
- dispatcher 사용하여 설정
- vagrant kvm으로 사용하기
- git fork, pull request to the …
- vagrant libvirt bridge network
- python, js의 async, await의 차이
- go JSON struct 생성
- Netflix Kinesis 활용 분석
- docker credential problem
- private subnet에서 outbound IP 확…
- 안드로이드 coroutine
- kotlin with, apply, also 등
- 안드로이드 초기로딩이 안되는 경우
- navigation 데이터 보내기
- 레이스 컨디션 navController
- raylib
TAG
- 매핑
- RestTemplate
- 자바
- Spring Security
- one-to-one
- Angular
- spring boot
- Validation
- 설정
- Rest
- mapping
- form
- WebMvc
- 로그인
- one-to-many
- hibernate
- login
- 하이버네이트
- XML
- Security
- Many-To-Many
- crud
- 상속
- 스프링
- MYSQL
- 설정하기
- 외부파일
- Spring
- jsp
- 스프링부트
250x250