From 0420227ccc5869983608fb2e3230ffa24251749d Mon Sep 17 00:00:00 2001 From: shikong <919411476@qq.com> Date: Sat, 2 Sep 2023 17:35:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E5=85=A8=E5=B1=80?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gb28181/advice/ExceptionAdvice.java | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 api/src/main/java/cn/skcks/docking/gb28181/advice/ExceptionAdvice.java diff --git a/api/src/main/java/cn/skcks/docking/gb28181/advice/ExceptionAdvice.java b/api/src/main/java/cn/skcks/docking/gb28181/advice/ExceptionAdvice.java new file mode 100644 index 0000000..d1b5a7e --- /dev/null +++ b/api/src/main/java/cn/skcks/docking/gb28181/advice/ExceptionAdvice.java @@ -0,0 +1,80 @@ +package cn.skcks.docking.gb28181.advice; + +import cn.skcks.docking.gb28181.common.json.JsonResponse; +import jakarta.validation.ConstraintViolationException; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.converter.HttpMessageNotReadableException; +import org.springframework.validation.BindException; +import org.springframework.web.HttpMediaTypeNotAcceptableException; +import org.springframework.web.HttpMediaTypeNotSupportedException; +import org.springframework.web.HttpRequestMethodNotSupportedException; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.MissingServletRequestParameterException; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +import java.util.Objects; + +/** + * 全局异常处理类 + * + * @author Shikong + */ +@Slf4j +@RestControllerAdvice +public class ExceptionAdvice { + + @ExceptionHandler(MissingServletRequestParameterException.class) + public JsonResponse missingServletRequestParameterException(MissingServletRequestParameterException e) { + return JsonResponse.error(e.getMessage()); + } + + @ExceptionHandler(HttpRequestMethodNotSupportedException.class) + public JsonResponse httpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e){ + return JsonResponse.error(e.getMessage()); + } + + @ExceptionHandler(HttpMediaTypeNotSupportedException.class) + public JsonResponse unsupportedMediaTypeException(Exception e) { + e.printStackTrace(); + return JsonResponse.error(e.getMessage()); + } + + @ExceptionHandler(HttpMediaTypeNotAcceptableException.class) + public JsonResponse httpMediaTypeNotAcceptableException(HttpMediaTypeNotAcceptableException e){ + return JsonResponse.error(e.getMessage()); + } + + @ExceptionHandler(RuntimeException.class) + public JsonResponse runtimeException(RuntimeException e) { + e.printStackTrace(); + return JsonResponse.error(e.getMessage()); + } + + @ExceptionHandler(BindException.class) + public JsonResponse handleValidationBindException(BindException e) { + return JsonResponse.error(Objects.requireNonNull(e.getBindingResult().getFieldError()).getDefaultMessage()); + } + + @ExceptionHandler(MethodArgumentNotValidException.class) + public JsonResponse handleMethodArgumentNotValidException(MethodArgumentNotValidException e) { + return JsonResponse.error(Objects.requireNonNull(e.getBindingResult().getFieldError()).getDefaultMessage()); + } + + @ExceptionHandler(ConstraintViolationException.class) + public JsonResponse handleConstraintViolationException(ConstraintViolationException e) { + return JsonResponse.error(Objects.requireNonNull(e.getMessage())); + } + + @ExceptionHandler(HttpMessageNotReadableException.class) + public JsonResponse handleHttpMessageNotReadableException(HttpMessageNotReadableException e){ + log.warn("{}", e.getMessage()); + return JsonResponse.error("参数异常"); + } + + @ExceptionHandler(Exception.class) + public JsonResponse exception(Exception e) { + e.printStackTrace(); + return JsonResponse.error(e.getMessage()); + } +}