spring boot 全局异常处理

在SimpleController中创建两个会抛出异常的方法

/**

* 使用 try catch 进行异常处理

* @return

*/

@RequestMapping(value = "/test1")

public String test1() {

try {

int i = 1 / 0 ;

} catch (Exception e) {

}

return "Hello World!" ;

}

 

/**

* 不进行异常处理

* @return

*/

@RequestMapping(value = "/test2")

public String test2() {

int i = 1 / 0 ;

return "Hello World!" ;

}

 

此时访问http://localhost:8081/test1 时 结果正常 但是访问http://localhost:8081/test2 就会出现异常页面 我们可以创建一个全局异常处理的Controller 对没有处理异常的controller进行处理

 

/**

* @ControllerAdvice 处理Controller 所有没有被try catch 包裹的注解

*/

@ControllerAdvice

public class GlobalExceptionHandler {

 

@ExceptionHandler(Exception.class)

@ResponseBody

public Map<String,Object> handlerException() {

Map<String,Object> map = new HashMap<String, Object>() ;

 

map.put("code","500") ;

map.put("msg","系统繁忙,稍后再试") ;

 

return map ;

}

}

对于业务异常 我们也可以进行统一的处理

/**

* 业务异常实体

*/

public class BusinessException extends Exception {

 

//异常处理编码

private Integer code ;

//异常处理信息

private String msg ;

//具体描述

private String desc ;

 

public BusinessException() {

super();

}

 

public BusinessException(String msg) {

this.msg = msg ;

}

 

public BusinessException(Integer code,String msg,String desc) {

this.code = code ;

this.msg = msg ;

this.desc = desc ;

}

getter / setting 省略

}

 

在GlobalExceptionHandler中编写处理业务异常的方法

@ExceptionHandler(BusinessException.class)

@ResponseBody

public Map<String,Object> handleBusinessException(BusinessException e) {

Map<String,Object> map = new HashMap<String, Object>() ;

map.put("code",e.getCode()) ;

map.put("msg",e.getMsg()) ;

map.put("desc",e.getDesc()) ;

return map ;

}

 

在SimpleController中抛出异常

@RequestMapping(value = "/test3")

public String test3() throws BusinessException {

throw new BusinessException(500,"系统出错","系统出错,请稍后访问!") ;

}

 

完整的全局异常处理类

/**

* @ControllerAdvice 处理Controller 所有没有被try catch 包裹的注解

*/

@ControllerAdvice

public class GlobalExceptionHandler {

 

/** logger */

private static final Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);

 

@ExceptionHandler(Exception.class)

@ResponseBody

public Map<String,Object> handlerException(Exception e) {

Map<String,Object> map = new HashMap<String, Object>() ;

 

map.put("code","500") ;

map.put("msg","系统出错,请稍后访问!") ;

 

LOGGER.error("Exception",e);

 

return map ;

}

 

@ExceptionHandler(BusinessException.class)

@ResponseBody

public Map<String,Object> handlerBusinessException(BusinessException e) {

Map<String,Object> map = new HashMap<String, Object>() ;

map.put("code",e.getCode()) ;

map.put("msg",e.getMsg()) ;

map.put("desc",e.getDesc()) ;

 

LOGGER.error("BusinessException",e.getDesc());

return map ;

}

 

@ExceptionHandler(SQLException.class)

@ResponseBody

public Map<String,Object> handlerSQLException(SQLException e) {

Map<String,Object> map = new HashMap<String, Object>() ;

 

map.put("code","500") ;

map.put("msg","系统出错,请稍后访问!") ;

 

LOGGER.error("SQLException",e);

 

return map ;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值