创建自定义异常类:
public class TokenCheckException extends RuntimeException{
private String msg;
private String code;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public TokenCheckException(String msg, String code) {
this.msg = msg;
this.code = code;
}
}
异常处理类:
@RestControllerAdvice
public class GlobalExceptionHandler {
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
@ExceptionHandler(TokenCheckException.class)
public AjaxResult handleTokenCheckException(TokenCheckException e, HttpServletRequest request) {
String requestURI = request.getRequestURI();
log.error("请求地址'{}',权限校验失败'{}'", requestURI, e.getMessage());
return AjaxResult.error(e.getCode(),e.getMsg());
}
}
/**
* 操作消息提醒
*
* @author ruoyi
*/
public class AjaxResult extends HashMap<String, Object>
{
private static final long serialVersionUID = 1L;
/** 状态码 */
public static final String CODE_TAG = "code";
/** 返回内容 */
public static final String MSG_TAG = "msg";
/** 数据对象 */
public static final String DATA_TAG = "data";
/**
* 初始化一个新创建的 AjaxResult 对象,使其表示一个空消息。
*/
public AjaxResult()
{
}
/**
* 初始化一个新创建的 AjaxResult 对象
*
* @param code 状态码
* @param msg 返回内容
*/
public AjaxResult(String code, String msg)
{
super.put(CODE_TAG, code);
super.put(MSG_TAG, msg);
}
/**
* 返回成功消息
*
* @return 成功消息
*/
public static AjaxResult success(String code, String msg)
{
return new AjaxResult(code, msg);
}
public static AjaxResult error(String code, String msg)
{
return new AjaxResult(code, msg);
}
}
版权声明:本文为qq_42425515原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。