From 61d100fac008375b6ba08e28b6ca301a20471482 Mon Sep 17 00:00:00 2001 From: KilLze Date: Fri, 2 Jan 2026 11:09:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=A8=E5=B1=80=E5=BC=82=E5=B8=B8=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 6 ++++++ .../bao/dating/handler/GlobalExceptionHandler.java | 11 ++++++++--- src/main/resources/application.yml | 5 +++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index 318929c..6a56776 100644 --- a/pom.xml +++ b/pom.xml @@ -61,6 +61,12 @@ spring-boot-starter-test test + + + org.mockito + mockito-inline + test + diff --git a/src/main/java/com/bao/dating/handler/GlobalExceptionHandler.java b/src/main/java/com/bao/dating/handler/GlobalExceptionHandler.java index 4e1993d..dbea84f 100644 --- a/src/main/java/com/bao/dating/handler/GlobalExceptionHandler.java +++ b/src/main/java/com/bao/dating/handler/GlobalExceptionHandler.java @@ -4,7 +4,6 @@ import com.bao.dating.common.Result; import com.bao.dating.common.ResultCode; import lombok.extern.slf4j.Slf4j; import org.springframework.dao.DuplicateKeyException; -import org.springframework.http.HttpStatus; import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.web.HttpRequestMethodNotSupportedException; import org.springframework.web.bind.MethodArgumentNotValidException; @@ -16,6 +15,7 @@ import org.springframework.web.servlet.NoHandlerFoundException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.util.stream.Collectors; /** * 全局异常处理器 @@ -32,7 +32,12 @@ public class GlobalExceptionHandler { @ExceptionHandler(MethodArgumentNotValidException.class) public Result handleMethodArgumentNotValidException(MethodArgumentNotValidException e) { log.error("参数验证失败: {}", e.getMessage()); - return Result.error(ResultCode.PARAM_ERROR, e.getBindingResult().getFieldError().getDefaultMessage()); + String msg = e.getBindingResult() + .getFieldErrors() + .stream() + .map(error -> error.getField() + ":" + error.getDefaultMessage()) + .collect(Collectors.joining("; ")); + return Result.error(ResultCode.PARAM_ERROR, msg); } /** @@ -95,7 +100,7 @@ public class GlobalExceptionHandler { @ExceptionHandler(RuntimeException.class) public Result handleRuntimeException(RuntimeException e) { log.error("运行时异常: ", e); - return Result.error(ResultCode.SYSTEM_ERROR, e.getMessage()); + return Result.error(ResultCode.SYSTEM_ERROR, "系统异常,请稍后重试"); } /** diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index b190806..8009016 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -2,6 +2,11 @@ server: port: 8080 spring: + mvc: + throw-exception-if-no-handler-found: true + web: + resources: + add-mappings: false datasource: url: jdbc:mysql://110.42.41.177:3306/dating?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8 username: root