SpringBoot配置全局自定义异常

2018-09-01 05:42:27来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

不同于传统集中时Springmvc 全局异常,具体查看前面的章节https://www.cnblogs.com/zwdx/p/8963311.html

对于springboot框架来讲,这里我就介绍一种  

1、ExceptionHandlerAdvice

import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 处理异常信息
 * @author wdh
 *
 */
@ControllerAdvice
@ResponseBody
public class ExceptionHandlerAdvice extends MyExecption{
    
    @ExceptionHandler(Exception.class)
    public ResultDTO handleException(Exception e,HttpServletResponse response) {
        return handler(e, response);
    }

}

由于是前后端分离,所以使用@ResponseBody返回json

2、MyExecptionHandler

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;import net.sf.json.JSONObject;
/**
 * 自定义异常处理
 * @author wdh
 *
 */
public class MyExecptionHandler{
    
    public ResultDTO handler(Exception e,HttpServletResponse response){
        ResultDTO result;
        if (e instanceof SysException) {
            result = handleSysException((SysException) e);
        } else if (e instanceof ServiceException) {
            result = handleServiceException((ServiceException) e);
        } else {
            result = handleSysException(new SysException(ERRORConstants.COMMON_SYSTEM_ERROR, e));
        }
        responseOutWithJson(response, result);
        return result;
    }

    private ResultDTO handleSysException(SysException ex) {
        ResultDTO result = new ResultDTO();
        result.setCode(ex.getCode());
        if(StringUtil.isNotEmpty(ex.getMsg())){
            result.setMessage(ex.getMsg());
        }else {
             result.setMessage(MessageEnums.fromcode(ex.getCode()));
        }
        return result;
    }

    private ResultDTO handleServiceException(ServiceException ex) {
        ResultDTO result = new ResultDTO();
        result.setCode(ex.getCode());
        result.setMessage(MessageEnums.fromcode(ex.getCode()));

        return result;
    }

    protected void responseOutWithJson(HttpServletResponse response, Object responseObject) {

        JSONObject responseJSONObject = JSONObject.fromObject(responseObject);

        String jsonString = responseJSONObject.toString();
        String code = responseJSONObject.getString("code");
        if (ERRORConstants.COMMON_EXCEL_FORMAT_ERROR.equals(code)) {
            response.setContentType("text/html;charset=UTF-8");
        } else {
            response.setContentType("application/json; charset=utf-8");
        }
        response.setCharacterEncoding(GlobalConstants.DEFAULT_ENCODING);

        PrintWriter out = null;
        try {
            out = response.getWriter();
            out.append(jsonString);

        } catch (IOException e) {

        } finally {
            if (out != null) {
                out.close();
            }
        }
    }
}

其中异常代码 ,为自己定义的GlobalConstants,MessageEnums

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Java全栈程序员之02:Ubuntu下Java环境安装、配置、测试

下一篇:JAVA 项目 调用外部的WebSerivce接口,入门实例 (一)