SpringBoot配置全局自定义异常

不同于传统集中时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 MyExecptionHandler{
    
    @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

posted @ 2018-08-31 20:06  紫薇帝星的故事  阅读(592)  评论(0编辑  收藏  举报