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()); + } +}