aix
2024-08-08 39ffdf5754b4ebca682bd3c925f7a500e8a1bbda
src/main/java/com/dji/sample/component/GlobalExceptionHandler.java
@@ -1,6 +1,8 @@
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;
import org.springframework.web.bind.annotation.ResponseBody;
@@ -28,6 +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, BindException.class})
    public ResponseResult methodArgumentNotValidExceptionHandler(BindException e) {
        e.printStackTrace();
        return ResponseResult.error(e.getFieldError().getField() + e.getFieldError().getDefaultMessage());
    }
}