패키지 me.zhengjie.common.exception.handler; 수입 lombok.extern.slf4j.Slf4j; 수입 me.zhengjie.common.exception.BadRequestException; 수입 me.zhengjie.common.exception.EntityExistException; 수입 me.zhengjie.common.exception.EntityNotFoundException; 수입 org.springframework.http.HttpStatus; 수입 org.springframework.http.ResponseEntity; 수입 org.springframework.security.access.AccessDeniedException; 수입 org.springframework.web.bind.MethodArgumentNotValidException; 수입 org.springframework.web.bind.annotation.ExceptionHandler; 수입org.springframework.web.bind.annotation.RestControllerAdvice; 가져 오기 를 java.io.PrintWriter; 수입 java.io.StringWriter의; 수입 정적 org.springframework.http.HttpStatus.BAD_REQUEST; 수입 정적 org.springframework.http.HttpStatus.FORBIDDEN; 수입 정적 org.springframework.http.HttpStatus.NOT_FOUND; / ** * @author 지에 *의 @date 2018년 11월 23일 * / @ SLF4J @RestControllerAdvice 공공 클래스 GlobalExceptionHandler { / ** *处理所有不可知的异常 * 파라미터 : E *@return * / @ExceptionHandler (. 예외 클래스 ) 공개 ResponseEntity handleException (예외 E) { // 인쇄 스택 정보 log.error (getStackTrace ()에 (E)); ApiError의 ApiError도의 = 새로운 새 ApiError의 (BAD_REQUEST.value ()의 e.getMessage ( )); 반환 ) buildResponseEntity (ApiError도를] } / ** * 접근 예외 처리 인터페이스 AccessDeniedException가 없다 * @param의 E * @return * / . @ExceptionHandler (AccessDeniedException는 클래스 ) 공개HandleAccessDeniedException ResponseEntity (AccessDeniedException가 E이다) { // 인쇄 스택 정보 log.error (getStackTrace ()에 (E)); ApiError의 ApiError도의 = 새로운 새 ApiError의 (FORBIDDEN.value () e.getMessage ())에, 복귀 buildResponseEntity (ApiError도를) } / * * * 지정 예외 처리 * @param의 E * @return * / @ExceptionHandler (값 = badRequestException. 등급 ) 공개 ResponseEntity <ApiError도의> badRequestException (badRequestException E) { // 인쇄 스택 정보 log.error (getStackTrace ()에 (E)); ApiError의 ApiError도 = 새로운 ApiError의 (e.getStatus () e.getMessage ()); 반환 buildResponseEntity (ApiError도)를; } / ** *处理EntityExist * @param의 E * @return * / @ExceptionHandler (값 = EntityExistException. 등급 ) 공개 ResponseEntity <ApiError도> entityExistException (EntityExistException 전자) { // 打印堆栈信息 log.error (getStackTrace ()에 (E)) ; ApiError의 ApiError의 = 새ApiError의 (BAD_REQUEST.value () e.getMessage ()); 반환 buildResponseEntity (ApiError도)를; } / ** *处理EntityNotFound * @param의 E * @return * / @ExceptionHandler (값 = EntityNotFoundException. 등급 ) 공개 ResponseEntity <ApiError도> entityNotFoundException (EntityNotFoundException 전자) { // 打印堆栈信息 log.error (getStackTrace ()에 (E)) ; ApiError의 ApiError도 = 새로운 ApiError의 (NOT_FOUND.value () e.getMessage ()); 반환 buildResponseEntity (ApiError도)를; } / ** * 모든 처리 인터페이스 데이터 검증 예외 * @param의 E * @Returns * / @ExceptionHandler (MethodArgumentNotValidException. 등급 ) 공개 ResponseEntity <ApiError도의> handleMethodArgumentNotValidException (MethodArgumentNotValidException E) { // 인쇄 스택 정보 log.error (getStackTrace ()에 (E )); 문자열 [] STR = e.getBindingResult () getAllErrors () (0) .getCodes () GET [1.] .split ( "\\.".. ) StringBuffer를 MSG = 새로운 새 의 StringBuffer (STR의 [. 1] + ":" ), .. msg.append (e.getBindingResult () getAllErrors () GET ( 0) .getDefaultMessage ()); ApiError의 ApiError도 = 새로운 ApiError의 (BAD_REQUEST.value () msg.toString ()); 반환 buildResponseEntity (ApiError도)를; } / ** *统一返回 * @param ApiError의 * @return * / 개인 ResponseEntity <ApiError도> buildResponseEntity (ApiError의 ApiError도) { 돌아가 새로운 ResponseEntity을 (ApiError도, HttpStatus.valueOf (apiError.getStatus ())); } / ** *获取堆栈信息 * @param 던질 수 * @return * / 개인문자열 getStackTrace ()에 (Throwable를 throw 가능 객체) { StringWriter를 자상 = 새로운 StringWriter를 (); 의 PrintWriter PW = 새 의 PrintWriter (SW); 시도 { throwable.printStackTrace (PW에게); 반환 "\ n"+ ) (sw.toString 단계; } 마지막 { pw.close (); } } }
패키지 me.zhengjie.common.exception.handler; 수입 com.fasterxml.jackson.annotation.JsonFormat; 수입 lombok.Data; 수입 java.time.LocalDateTime; / ** * @author 지에 *의 @date 2018년 11월 23일 * / @Data의 클래스 ApiError의 { 개인 정수 상태; @JsonFormat (패턴 = "YYYY-MM-DD HH : MM : SS" ) 전용 LocalDateTime 타임 스탬프; 개인 문자열 메시지; 개인 ApiError도 () { 소인 = LocalDateTime.now (); } 공개ApiError도 (정수 상태, 문자열 메시지) { 이 (); 이 .STATUS = 상태; 이 .message = 메시지; } }