標籤:注入 mob 設定 and actor UI global model style
通常在開發過程中,會遇到很多異常,對於一些知道異常的原因,這時候想要返回給瀏覽器,就需要自訂系統的異常
1、Spring 注入異常處理類
<bean id ="commonExceptionHandler" class = "com..test.common.exception.handler.CommonExceptionHandler">
2、注入的異常處理類,主要是重寫resolveException方法
import java.io.IOException;import java.io.PrintWriter;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import net.sf.json.JSONObject;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.web.servlet.HandlerExceptionResolver;import org.springframework.web.servlet.ModelAndView;import com.test.common.constant.ERRORConstants;import com.test.common.constant.GlobalConstants;import com.test.common.dto.ResultDTO;import com.test.common.exception.ServiceException;import com.test.common.exception.SysException;import com.test.common.util.SpringContextHolder;import com.test.common.util.StringUtil;public class CommonExceptionHandler implements HandlerExceptionResolver { private static final Logger LOG = LoggerFactory.getLogger(CommonExceptionHandler.class); /** * @param request 參數 * @param response 參數 * @param obj 參數 * @param e 參數 * @return modelview */ public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object obj, Exception e) { 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 new ModelAndView(); }
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(SpringContextHolder.getMessage(ex.getCode(), null));} LOG.error(new StringBuilder().append(result.getCode()).append(result.getMessage()).toString(), ex); return result; } private ResultDTO handleServiceException(ServiceException ex) { ResultDTO result = new ResultDTO(); result.setCode(ex.getCode()); result.setMessage(SpringContextHolder.getMessage(ex.getCode(), null)); LOG.error(new StringBuilder().append(result.getCode()).append(result.getMessage()).toString()); return result; } protected void responseOutWithJson(HttpServletResponse response, Object responseObject) { JSONObject responseJSONObject = JSONObject.fromObject(responseObject); String jsonString = responseJSONObject.toString(); response.setCharacterEncoding(GlobalConstants.DEFAULT_ENCODING); response.setContentType("application/json; charset=utf-8"); PrintWriter out = null; try { out = response.getWriter(); out.append(jsonString); LOG.debug("返回是\n"); LOG.debug(jsonString); } catch (IOException e) { LOG.debug("Error responseOutWithJson"); } finally { if (out != null) { out.close(); } } }}
3、自訂系統異常類
package com.test.common.exception.base;public class BaseCommonException extends RuntimeException { private static final long serialVersionUID = 1L; private String code; private String msg;//用於存放異常資訊 /** * 建構函式 */ public BaseCommonException() { super(); } /** * * @param cause 參數 */ public BaseCommonException(Throwable cause) { super(cause); } /** * * @param code 參數 */ public BaseCommonException(String code) { this.setCode(code); } /** * * @param code 參數 * @param e 參數 */ public BaseCommonException(String code, Throwable e) { super(e); this.setCode(code); } /** * * @param code 參數 * @param e 參數 */ public BaseCommonException(String code, Throwable e,String msg) { super(e); this.setCode(code); this.setMsg(msg); } /** * * @return code */ public String getCode() { return code; } /** * * @param code 參數 */ public void setCode(String code) { this.code = code; } /** * * @return msg */ public String getMsg() { return msg; } /** * * @param msg 參數 */ public void setMsg(String msg) { this.msg = msg; } }
4、Service類
public boolean test() { String msg = ""; try { msg = "自訂資訊"; return true; } catch (Exception e) { throw new BaseCommonException("300",e, "失敗資訊:"+msg); } }
Java自訂異常資訊