异常处理时很重要的,分为业务出错,用户试错,系统出错
目录
步骤
1.自定义项目系统级异常
package com.liyang.exception;
/**
* @version 1.0
* @auter liyang
*/
public class SystemException extends RuntimeException{
private Integer code;
public SystemException(Integer code) {
this.code = code;
}
public SystemException(Integer code,String message) {
super(message);
this.code = code;
}
public SystemException(Integer code,String message, Throwable cause) {
super(message, cause);
this.code = code;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
}
2.自定义项目业务级异常
package com.liyang.exception;
/**
* @version 1.0
* @auter liyang
*/
public class BusnessSystemException extends RuntimeException{
private Integer code;
public BusnessSystemException(Integer code) {
this.code = code;
}
public BusnessSystemException(Integer code, String message) {
super(message);
this.code = code;
}
public BusnessSystemException(Integer code, String message, Throwable cause) {
super(message, cause);
this.code = code;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
}
3.设置异常编码
package com.liyang.controller;
/**
* @version 1.0
* @auter liyang
*/
public class Code {
public static final Integer SAVE_OK=20011;
public static final Integer DELETE_OK=20021;
public static final Integer UPDATA_OK=20031;
public static final Integer GET_OK=20041;
public static final Integer SAVE_ERR=20010;
public static final Integer DELETE_ERR=20020;
public static final Integer UPDATA_ERR=20030;
public static final Integer GET_ERR=20040;
public static final Integer SYSTEM_ERR=50001;
public static final Integer SYSTEM_TIMEOUT_ERR=50002;
public static final Integer BUSNEISS_ERR=60002;
public static final Integer SYSTEM_UNKNOW_ERR=59999;
}
4.触发自定义异常
package com.liyang.service.impl;
import com.liyang.controller.Code;
import com.liyang.dao.BookDao;
import com.liyang.domain.Book;
import com.liyang.exception.BusnessSystemException;
import com.liyang.exception.SystemException;
import com.liyang.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookDao bookDao;
public boolean save(Book book) {
bookDao.save(book);
return true;
}
public boolean update(Book book) {
bookDao.update(book);
return true;
}
public boolean delete(Integer id) {
bookDao.delete(id);
return true;
}
public Book getById(Integer id) {
//将可能出现的异常进行包装,转换成自定义异常
if(id==1){
throw new BusnessSystemException(Code.BUSNEISS_ERR,"请不要用你的技术挑战我的耐心");
}
try{
int i=1/0;
}catch (Exception e){
throw new SystemException(Code.SYSTEM_TIMEOUT_ERR,"服务器超时,请重试!");
}
return bookDao.getById(id);
}
public List<Book> getAll() {
return bookDao.getAll();
}
}
5.拦截并处理异常
package com.liyang.controller;
import com.liyang.exception.BusnessSystemException;
import com.liyang.exception.SystemException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
* @version 1.0
* @auter liyang
*/
@RestControllerAdvice
public class ProjectExceptionAdvice {
@ExceptionHandler(SystemException.class)
public Result doSystemException(SystemException ex){
//发日志
//发邮件给开发人员
//发短信给运维人员
return new Result(ex.getCode(),null,ex.getMessage());
}
@ExceptionHandler(BusnessSystemException.class)
public Result doBusnessSystemException(BusnessSystemException ex){
//发日志
//发邮件给开发人员
//发短信给运维人员
return new Result(ex.getCode(),null,ex.getMessage());
}
@ExceptionHandler(Exception.class)
public Result doException(Exception ex){
return new Result(Code.SYSTEM_UNKNOW_ERR,null,"系统繁忙,请稍后再试");
}
}
6.异常处理器效果对比