一,参数绑定异常,场景:对一些参数做限定,要求必须输入。
不做处理的情况下,返回的信息凌乱,需要按我们满意的格式返回给前端。
可以看到控制台,报的是绑定异常
Resolved [org.springframework.validation.BindException:
开始捕获异常。
@RestControllerAdvice
public class GlobalExceptionHandle {
@ExceptionHandler(BindException.class)
public R bindExceptionHandler(BindException e){
List<ObjectError> allErrors = e.getAllErrors();
//处理异常信息集合
List<String> errros = new ArrayList<>();
for (ObjectError allError : allErrors) {
String msg = allError.getDefaultMessage();
errros.add(msg);
}
return R.fail(Codes.PARAM_ERROR.getCode(),errros.toString());
}
//shiro自定义异常
@ExceptionHandler(AuthenticationException.class)
public R authenticationException(AuthenticationException e){
if (e instanceof LockedAccountException) {
return R.fail(SystemConstant.FREEZE_USER);
}
if (e instanceof IncorrectCredentialsException) {
return R.fail(SystemConstant.ERROR_PASSWORD);
}
if (e instanceof UnknownAccountException){
return R.fail(SystemConstant.NO_USER);
}
if (e instanceof IncorrectCredentialsException){
return R.fail(SystemConstant.NO_CREDENTIALS);
}
if (e instanceof LockedAccountException){
return R.fail(SystemConstant.FREEZE_USER);
}
return R.fail(SystemConstant.SYSTEMS);
}
}
public class SystemConstant {
public static final String ADD_FAIL = "添加失败";
public static final String UPDATE_FAIL = "修改失败";
public static final String DATA_NON = "数据不存在";
public static final String DELETE_FAIL = "删除失败";
public static final String HIDE_FAIL = "隐藏/展示失败";
public static final String FREEZE_FAIL = "冻结用户失败";
public static final String FREEZE_USER = "用户已冻结,请联系管理员!!";
public static final String ERROR_PASSWORD = "密码错误";
public static final String NO_USER = "用户不存在";
public static final String NO_CREDENTIALS = "用户凭证不正确";
public static final String SYSTEMS = "服务器异常";
/**失效重新登录 511*/
public static final Integer SC_JEECG_RE_LOGIN=511;
/**初始默认密码*/
public static final String S_USER_PWD="123456";
}
@Data
@NoArgsConstructor
@ApiModel("请求响应对象")
public class R<T> implements Serializable {
public static final String SUCCESS = "200";
public static final String FAIL = "500";
@ApiModelProperty("响应码")
private String code;
@ApiModelProperty("响应信息")
private String msg;
@ApiModelProperty("数据")
private T data;
public static <T> R<T> ok() {
return restResult(null, SUCCESS, "操作成功");
}
public static <T> R<T> ok(String total,T data) {
return restResult(data, SUCCESS, "操作成功");
}
public static <T> R<T> ok(String code,String msg) {
return restResult(null,code, "操作成功");
}
public static <T> R<T> ok(String msg) {
return restResult(null, null, msg);
}
public static <T> R<T> ok(T data) {
return restResult(data, SUCCESS,"操作成功");
}
public static <T> R<T> fail(String msg) {
return restResult(null, FAIL, msg);
}
public static <T> R<T> fail(T data) {
return restResult(data, FAIL, "操作失败");
}
public static <T> R<T> fail(T data,String msg) {
return restResult(data, FAIL, msg);
}
public static <T> R<T> fail(String msg, T data) {
return restResult(data, FAIL, msg);
}
public static <T> R<T> fail(String code, String msg) {
return restResult(null, code, msg);
}
private static <T> R<T> restResult(T data, String code, String msg) {
R<T> r = new R<>();
r.setCode(code);
r.setData(data);
r.setMsg(msg);
return r;
}
/**
* 无权限访问返回结果
*/
public static R<Object> noauth(String msg) {
return fail(String.valueOf(CommonConstant.SC_JEECG_NO_AUTHZ), msg);
}
/**
* 登录信息失效返回结果
*/
public static R<Object> reLogin(String msg) {
return fail(String.valueOf(CommonConstant.SC_JEECG_RE_LOGIN), msg);
}
}
@Getter
public enum Codes {
SUCC(200, "成功"),
SERVER_ERROR(500, "服务器异常,请稍后再试!"),
PARAM_ERROR(400, "参数校验异常!"),
AUTH_ERROR(401, "身份验证失败!"),
TOKEN_ERROR(402, "令牌验证失败!"),
TOKEN_REFRESH_ERROR(403, "令牌刷新失败!"),
ACCESS_ERROR(405, "权限不足!"),
CODE_ERROR(406, "验证码错误");
public Integer code;
public String msg;
Codes(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
}