所有要处理的一层都通过
@ExceptionHandler(CustomException.class)在句话捕获,。其中CustomException为自定义异常 也可以是系统异常比如 SQLException
import com.pugongying.ec.bean.ResponseData;
import com.pugongying.ec.bean.domain.ResultJson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageConversionException;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.sql.SQLException;
import java.util.List;
/**
* @author Joetao
* 异常处理类
* controller层异常无法捕获处理,需要自己处理
* Created at 2018/8/27.
*/
@RestControllerAdvice
@Slf4j
public class DefaultExceptionHandler {
/**
* 处理所有自定义异常
* @param e
* @return
*/
@ExceptionHandler(CustomException.class)
public ResultJson handleCustomException(CustomException e){
log.error(e.getResultJson().toString());
return e.getResultJson();
}
@ExceptionHandler(SQLException.class)
public ResponseData handleSQLException(SQLException e){
log.error(String.valueOf(e.getErrorCode()));
if(e.getErrorCode()==1452){
ResponseData responseData=new ResponseData("数据库操作异常","关联的id不存在,"+e.getMessage());
return responseData;
}
log.error(e.getMessage());
ResponseData responseData=new ResponseData("数据库操作异常",e);
return responseData;
}
/* *//**
* 处理参数校验异常
* @param e
* @return
*//*
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseData handleMethodArgumentNotValidException(MethodArgumentNotValidException e){
log.error(e.getBindingResult().getFieldError().getField() + e.getBindingResult().getFieldError().getDefaultMessage());
return ResponseData.fail(CodeEnum.BAD_REQUEST, e.getBindingResult().getFieldError().getDefaultMessage());
}*/
/**
* 校验错误拦截处理
*
* @param exception 错误信息集合
* @return 错误信息
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseData validationBodyException(MethodArgumentNotValidException exception) {
BindingResult result = exception.getBindingResult();
String message = "";
if (result.hasErrors()) {
List<ObjectError> errors = result.getAllErrors();
if (errors != null) {
errors.forEach(p -> {
FieldError fieldError = (FieldError) p;
log.error("Data check failure : object{" + fieldError.getObjectName() + "},field{" + fieldError.getField() +
"},errorMessage{" + fieldError.getDefaultMessage() + "}");
});
if (errors.size() > 0) {
FieldError fieldError = (FieldError) errors.get(0);
message = fieldError.getDefaultMessage();
}
}
}
ResponseData responseData=new ResponseData("\"\".equals(message) ? \"请填写正确信息\" : message");
return responseData;
}
@ExceptionHandler(BindException.class)
public Object validExceptionHandler(BindException exception){
BindingResult result = exception.getBindingResult();
if (result.hasErrors()) {
List<ObjectError> errors = result.getAllErrors();
errors.forEach(p -> {
FieldError fieldError = (FieldError) p;
log.error("Data check failure : object={}, field={}, errorMessage={}", fieldError.getObjectName(), fieldError.getField(), fieldError.getDefaultMessage());
});
ResponseData responseData=new ResponseData( result.getFieldError() == null ? "请求参数有误" : "参数异常:"+result.getFieldError().getDefaultMessage());
return responseData;
}
//其他错误
ResponseData responseData=new ResponseData("其他错误",result);
return responseData;
}
/**
* 参数类型转换错误
*
* @param exception 错误
* @return 错误信息
*/
@ExceptionHandler(HttpMessageConversionException.class)
public ResponseData parameterTypeException(HttpMessageConversionException exception) {
log.error(exception.getCause().getLocalizedMessage());
ResponseData responseData=new ResponseData("类型转换错误");
return responseData;
}
/**
* 参数类型转换错误
*
* @param exception 错误
* @return 错误信息
*/
/* @ExceptionHandler(Exception.class)
public ResponseData parameterTypeException(Exception exception) {
exception.printStackTrace();
return ResponseData.fail("类型转换错误");
}*/
}
网友评论