1.在exception包下添加XDException.java
package com.xiaoguan.exception;
public class XDException extends RuntimeException{
private Integer code;
private String msg;
public XDException(Integer code,String msg){
this.code = code;
this.msg = msg;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
2.在exception包下添加CustomException.java
package com.xiaoguan.exception;
import com.xiaoguan.untils.JsonData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
@ControllerAdvice
public class CustomExceptionHandler {
private final static Logger logger = LoggerFactory.getLogger(CustomExceptionHandler.class);
@ExceptionHandler(value = Exception.class)
@ResponseBody
public JsonData handel(Exception e){
logger.error("[ 系统异常 ]{}",e);
if( e instanceof XDException){
XDException xdException = (XDException) e;
return JsonData.buildError(xdException.getCode(),xdException.getMsg());
}else {
return JsonData.buildError("全局异常,未知错误");
}
}
}
3.在VideoController里面的banner_list接口中添加测试
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210314092245894.png#pic_center)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210314092308848.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzNjI2MjE1,size_16,color_FFFFFF,t_70#pic_center)