From 8d9a2d656e4ae007590c622e5f7c228adacdca49 Mon Sep 17 00:00:00 2001
From: rain <167982779@qq.com>
Date: Fri, 14 Jun 2024 10:11:36 +0800
Subject: [PATCH] 统一风格

---
 src/main/java/com/dji/sample/component/GlobalExceptionHandler.java |   10 ++++++----
 1 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/src/main/java/com/dji/sample/component/GlobalExceptionHandler.java b/src/main/java/com/dji/sample/component/GlobalExceptionHandler.java
index b0a9fbf..c590caf 100644
--- a/src/main/java/com/dji/sample/component/GlobalExceptionHandler.java
+++ b/src/main/java/com/dji/sample/component/GlobalExceptionHandler.java
@@ -1,6 +1,7 @@
 package com.dji.sample.component;
 
 import com.dji.sample.common.model.ResponseResult;
+import org.springframework.validation.BindException;
 import org.springframework.web.bind.MethodArgumentNotValidException;
 import org.springframework.web.bind.annotation.ControllerAdvice;
 import org.springframework.web.bind.annotation.ExceptionHandler;
@@ -29,12 +30,13 @@
     @ExceptionHandler(NullPointerException.class)
     public ResponseResult nullPointerExceptionHandler(NullPointerException e) {
         e.printStackTrace();
-        return ResponseResult.error("A null object appeared.");
+        return ResponseResult.error("出现空对象");
     }
 
-    @ExceptionHandler(MethodArgumentNotValidException.class)
-    public ResponseResult methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
-        return ResponseResult.error(e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
+    @ExceptionHandler({MethodArgumentNotValidException.class, BindException.class})
+    public ResponseResult methodArgumentNotValidExceptionHandler(BindException e) {
+        e.printStackTrace();
+        return ResponseResult.error(e.getFieldError().getField() + e.getFieldError().getDefaultMessage());
     }
 
 }

--
Gitblit v1.9.3