import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang.exception.ExceptionUtils; import org.apache.log4j.Logger; import com.alibaba.fastjson.JSON; import com.yundaex.common.basic.vo.ResponseVO; import com.yundaex.common.exception.DefaultResponseException; import com.yundaex.common.exception.ExceptionHelper; import com.yundaex.common.exception.WrappedRuntimeException; import com.yundaex.common.exception.WrappedWriteResponseException; public class ExceptionFilter implements Filter { private static final Logger logger = Logger.getLogger(ExceptionFilter.class); @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletResponse res = (HttpServletResponse) response; HttpServletRequest req = (HttpServletRequest) request; try { chain.doFilter(req, res); } catch (Exception ex) { response.setContentType("text/json; charset=utf-8"); response.setCharacterEncoding("UTF-8"); if(logger.isDebugEnabled()) { logger.debug("exception has been captured."); } String fullStackTrace = ExceptionUtils.getFullStackTrace(ex); logger.error(fullStackTrace); WrappedRuntimeException gre = ExceptionHelper.convert2GeneralResponseException(ex,DefaultResponseException.class); ResponseVO responseVO = ExceptionHelper.generateResponseVOFromGenerateResponseException(gre); String result = JSON.toJSONString(responseVO); if(logger.isDebugEnabled()) { logger.debug(result); } try{ byte[] bytes = result.getBytes("utf8"); ServletOutputStream outputStream = response.getOutputStream(); outputStream.write(bytes); } catch(Exception e){ WrappedRuntimeException e1 = new WrappedWriteResponseException("excpetion handler write response occurs error"); throw e1; } } } @Override public void destroy() { } }
/** * HTTP 请求响应对应的实体 * @author JiangWubo-kris * @version 2.0 * @date 2014-10-11 */ public class ResponseVO { /*** * 处理结果,TURE/FALSE */ private String result; /*** * 返回的数据 */ private Object data; /*** * 异常代码 */ private String errorCode; /*** * 描述信息 */ private Object remark; public String getResult() { return result; } public void setResult(String result) { this.result = result; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } public String getErrorCode() { return errorCode; } public void setErrorCode(String errorCode) { this.errorCode = errorCode; } @Override public String toString() { return "ResponseVO [result=" + result + ", data=" + data + ", errorCode=" + errorCode + ", remark=" + remark + "]"; } /** * @return the remark */ public Object getRemark() { return remark; } /** * @param remark the remark to set */ public void setRemark(Object remark) { this.remark = remark; } public static ResponseVO constructNullValue(){ ResponseVO responseVO = new ResponseVO(); responseVO.setResult("TRUE"); responseVO.setData(new Object()); return responseVO; } }
web.xml
<filter> <filter-name>exceptionFilter</filter-name> <filter-class>com.yundaex.common.exception.filter.ExceptionFilter</filter-class> </filter> <filter-mapping> <filter-name>exceptionFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
时间: 2024-10-02 10:46:58