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("参数校验失败"); 
 | 
        } 
 | 
    } 
 | 
  
 | 
} 
 |