1.编写自己捕获异常的包装类
标注绿色的是自己最近写的包装类
BusinessExceptionNew 类
package com.aostar.trade.common.ExceptionHandler;
import lombok.Data;
/**
* @author jay
* @Description: 自定义业务异常类
* @date 2021/6/23 14:58
*/
@Data
public class BusinessExceptionNew extends RuntimeException {
/**
* 错误编码
*/
private String code;
public BusinessExceptionNew() {
super();
}
public BusinessExceptionNew(String message) {
super(message);
}
public BusinessExceptionNew(String code, String message) {
super(message);
this.code = code;
}
public BusinessExceptionNew(Throwable cause) {
super(cause);
}
public BusinessExceptionNew(String message, Throwable cause) {
super(message, cause);
}
public BusinessExceptionNew(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
@Override
public String getMessage() {
return super.getMessage();
}
@Override
public String toString() {
return this.code + ":" + this.getMessage();
}
}
SystemExceptionNew类
package com.aostar.trade.common.ExceptionHandler;
import lombok.Data;
/**
* @author jay
* @Description: 自定义业务异常类
* @date 2021/6/23 14:58
*/
@Data
public class SystemExceptionNew extends RuntimeException {
/**
* 错误编码
*/
private String code;
public SystemExceptionNew() {
super();
}
public SystemExceptionNew(String message) {
super(message);
}
public SystemExceptionNew(String code, String message) {
super(message);
this.code = code;
}
public SystemExceptionNew(Throwable cause) {
super(cause);
}
public SystemExceptionNew(String message, Throwable cause) {
super(message, cause);
}
public SystemExceptionNew(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
@Override
public String getMessage() {
return super.getMessage();
}
@Override
public String toString() {
return this.code + ":" + this.getMessage();
}
}
ExceptionAdvice 类
package com.aostar.trade.common.ExceptionHandler;
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;
import org.springframework.web.bind.annotation.RestController;
/**
* @author jay
* @Description:
* @date 2021/6/23 14:58
*/
@RestController
@ControllerAdvice
public class ExceptionAdvice {
public static Logger logger = LoggerFactory.getLogger(ExceptionAdvice.class);
@ResponseBody
@ExceptionHandler(SystemExceptionNew.class)
public ResultVO handleException(Exception e) {
ResultVO result = new ResultVO();
if (e instanceof BusinessException) {
e = (BusinessException) e;
result.setCode(((BusinessException) e).getCode());
}
result.setMessage("系统异常信息:"+e.getMessage());
return result;
}
@ExceptionHandler(RuntimeException.class)
@ResponseBody
public ResultVO handleException(RuntimeException e) {
ResultVO result = new ResultVO();
result.setStatus("500");
result.setMessage("运行异常:"+e.getMessage());
return result;
}
@ExceptionHandler(BusinessExceptionNew.class)
@ResponseBody
public ResultVO doBusinessException(Exception e) {
ResultVO result = new ResultVO();
result.setStatus("500");
result.setMessage("业务异常:"+e.getMessage());
return result;
}
}
返回前端的实体类
package com.aostar.trade.common.ExceptionHandler;
/**
* @author jayd
* @Description:
* @date 2021/6/23 14:58
*/
public class ResultVO {
private String Code;
private String Message;
private String Status;
public String getCode() {
return Code;
}
public void setCode(String code) {
Code = code;
}
public String getMessage() {
return Message;
}
public void setMessage(String message) {
Message = message;
}
public String getStatus() {
return Status;
}
public void setStatus(String status) {
Status = status;
}
}
2.使用方式
代码中throw new 的时候 如果有异常就会将异常信息以ResultVO返回给前端 service层写
3.前台接收异常信息 并进行展示
重点是几个注解的使用
1.Spring通过@ExceptionHandler来拦截系统运行时抛出的相应异常。其有效作用域是其所处的Controller,即它声明的异常处理方法无法拦截、处理其他Controller类中抛出的异常。
2.在类上使用@ControllerAdvice(控制器增强。该注解可以把其声明的类中使用@ExceptionHandler、@InitBinder、@ModelAttribute注解的方法应用到所有的 @RequestMapping注解的方法)声明一个拦截全局异常的@ExceptionHandler。