guoshilong
2023-11-13 68f80a7451a126335153ec00bb6cb520a5ae1f8d
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());
    }
}