配置出现异常 直接抛出异常
spring.mvc.throw-exception-if-no-handler-found=true
/**
* 异常监听类
* Created by liqun on 2018/4/19.
*/
@ControllerAdvice
public class ExceptionHandle {
private final static Logger LOGGER = LoggerFactory.getLogger(ExceptionHandle.class);
@ExceptionHandler(value = Exception.class)
@ResponseBody
public Result exceptionGet(Exception e){
//请求方式不正确
if(e instanceof HttpRequestMethodNotSupportedException){
return ResultUtil.error(ExceptionEnum.HTTP_METHOD);
}
//404
if(e instanceof NoHandlerFoundException){//404
return ResultUtil.error(ExceptionEnum.NO_METHOD_ERROR);
}
/**
* 如果抛出的自定义redis 异常
*/
if(e instanceof RedisException){
DescribeException myException = (DescribeException) e;
return ResultUtil.error(myException.getCode(),myException.getMessage());
}
/**
* 自定义错误异常
*/
if(e instanceof CustomException){
return ResultUtil.error(((CustomException)e).getCode(),((CustomException)e).getMessage());
}
/**
* 权限验证
*/
if(e instanceof AuthenticationException){
return ResultUtil.error(ExceptionEnum.SHIRO_LOGIN_ERROR);
}
if(e instanceof UnauthorizedException ){
return ResultUtil.error(ExceptionEnum.NO_AUTHORITY);
}
if(e instanceof TokenException){
return ResultUtil.error(ExceptionEnum.TOKENT_ERROR);
}
LOGGER.error("未知异常",e);
return ResultUtil.error(ExceptionEnum.UNKNOW_ERROR);
}
}