jyy
2021-01-25 949efdc17fa833f6511c0e5902057f3be0fe72c4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.matrix.core.exception;
 
import com.matrix.core.pojo.AjaxResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
 
import javax.validation.ValidationException;
 
/**
 * @author wzy
 * @date 2020-05-08 15:40
 **/
@RestControllerAdvice
public class GlobalExceptionHandler {
 
    /**
     * 方法参数校验
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = {MethodArgumentNotValidException.class})
    public AjaxResult handleException(MethodArgumentNotValidException e) {
        FieldError fieldError = e.getBindingResult().getFieldError();
        if (fieldError != null) {
            return AjaxResult.buildFailInstance(fieldError.getDefaultMessage());
        } else {
            return AjaxResult.buildFailInstance("参数校验失败");
        }
    }
 
}