首页 > 其他 > 详细

异常统一处理类

时间:2017-03-22 12:57:25      阅读:256      评论:0      收藏:0      [点我收藏+]
import org.apache.commons.lang3.StringUtils;
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 DefaultExceptionHandler {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(DefaultExceptionHandler.class);
    
    @ExceptionHandler({ Exception.class })
    @ResponseBody
    public String processServiceException(Exception e) {
        if (null != e && StringUtils.isNotBlank(e.getMessage())) {
            if (e instanceof ServiceException) {/* 业务异常,不打印日志 */
                LOGGER.debug(e.getMessage(), e);
                ServiceException ex = (ServiceException) e;
                return MessageHandleUtil.buildResult(ex.getCode(), ex.getMessage());
            } else {
                LOGGER.error(e.getMessage(), e);
                return MessageHandleUtil.buildResult(ReturnCode.FAILURE, ReturnMessage.FAILURE);
            }
        }
        LOGGER.error(e.getMessage(), e);
        return MessageHandleUtil.buildServiceErrorResult();
    }
}

 

异常统一处理类

原文:http://www.cnblogs.com/hellocyc/p/6599058.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!