http 接口层:统一异常处理工具类
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @description:
* @author: juxiaoxiong
* @date: 2019/12/25 09:54
**/
@ControllerAdvice
public class GlobalExceptionHandler {
private static final Logger logger = LogManager.getLogger(GlobalExceptionHandler.class);
/**
* 全局异常捕捉处理
* @param ex
* @return
*/
@ResponseBody
@ExceptionHandler(value = Exception.class)
public Response errorHandler(Exception ex) {
Response response = new Response<>();
response.setCode(ResponseResultEnum.OPERATE_FAILED.toCode());
response.setErrorDesc(ResponseResultEnum.OPERATE_FAILED.toDescription());
response.setMsg(ex.getMessage());
logger.error("全局异常捕捉处理:" + ex);
return response;
}
/**
* 拦截捕捉自定义异常 ServiceException.class
* @param ex
* @return
*/
@ResponseBody
@ExceptionHandler(value = WjActivityException.class)
public Response myErrorHandler(WjActivityException ex) {
Response response = new Response<>();
response.setCode(ex.getCode());
response.setMsg(ex.getMessage());
logger.error("拦截捕捉自定义异常:" + ex);
return response;
}
}