1.当我们在写业务代码的时候,经常会用到异常处理,但是如果我们每次都用try catch来处理业务逻辑的话,就会看起来非常繁琐,代码冗余,不处理的话后台会报错,这时候就需要封装一下,然后处理代码的时候一行代码就可以返回给前台。
@RestControllerAdvice 这个注解的作用是拦截异常并统一处理
在spring 3.2中,新增了@ControllerAdvice 注解,可以用于定义@ExceptionHandler、@InitBinder、@ModelAttribute,并应用到所有@RequestMapping中。
1.创建 RRExceptionHandler,并添加 @ControllerAdvice注解。
/**
* @program: xiaowu
* @description: 异常处理
* @author: Wu
* @create: 2020-08-28 10:34
**/
@RestControllerAdvice
public class RRExceptionHandler {
private Loggerlogger = LoggerFactory.getLogger(getClass());
/**
* 处理自定义异常
*/
@ExceptionHandler(RRException.class)
public RhandleRRException(RRException e){
R r =new R();
r.put("code", e.getCode());
r.put("msg", e.getMessage());
return r;
}
@ExceptionHandler(NoHandlerFoundException.class)
public RhandlerNoFoundException(Exception e) {
logger.error(e.getMessage(), e);
return R.error(404, "路径不存在,请检查路径是否正确");
}
@ExceptionHandler(DuplicateKeyException.class)
public RhandleDuplicateKeyException(DuplicateKeyException e){
logger.error(e.getMessage(), e);
return R.error("数据库中已存在该记录");
}
@ExceptionHandler(AuthorizationException.class)
public RhandleAuthorizationException(AuthorizationException e){
logger.error(e.getMessage(), e);
return R.error("没有权限,请联系管理员授权");
}
@ExceptionHandler(Exception.class)
public RhandleException(Exception e){
logger.error(e.getMessage(), e);
return R.error();
}
}
2.创建RRException类,并继承RuntimeException类,并编写一些构造方法
package com.example.xiaowu.exception;
public class RRExceptionextends RuntimeException{
private static final long serialVersionUID =1L;
private Stringmsg;
private int code =500;
public RRException(String msg) {
super(msg);
this.msg = msg;
}
public RRException(String msg, Throwable e) {
super(msg, e);
this.msg = msg;
}
public RRException(String msg, int code) {
super(msg);
this.msg = msg;
this.code = code;
}
public RRException(String msg, int code, Throwable e) {
super(msg, e);
this.msg = msg;
this.code = code;
}
public StringgetMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
}
接口返回的实体封装 R
package com.example.xiaowu.utils;
import org.apache.http.HttpStatus;
import java.util.HashMap;
import java.util.Map;
/**
* @program: xiaowu
* @description: 返回数据
* @author: Wu
* @create: 2020-08-28 10:39
**/
public class Rextends HashMap {
private static final long serialVersionUID =1L;
public R() {
put("code", 0);
put("msg", "success");
}
public static Rerror() {
return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, "未知异常,请联系管理员");
}
public static Rerror(String msg) {
return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, msg);
}
public static Rerror(int code, String msg) {
R r =new R();
r.put("code", code);
r.put("msg", msg);
return r;
}
public static Rok(String msg) {
R r =new R();
r.put("msg", msg);
return r;
}
public static Rok(Map map) {
R r =new R();
r.putAll(map);
return r;
}
public static Rok() {
return new R();
}
public Rput(String key, Object value) {
super.put(key, value);
return this;
}
}
接口测试
返回值
跟前端约定好code,就可以友好操作了