spring mvc controller中的错误封装
spring mvc controller中的异常封装
一直以来都在用spring mvc做mvc框架,我使用的不是基于注解的,还是使用的基于xml的,在controller里一般都会加上一个异常捕捉,能分析出来的异常,提示出具体信息,不能预料的异常,返回一个统一的异常提示信息,未封装前的代码为:
public ModelAndView addBigDeal(HttpServletRequest request, HttpServletResponse response) throws Exception { JSONObject jsonObject = new JSONObject(); try { String sessionId = WebUtils.getStringValue(request, "sessionId", true); String pl_id = WebUtils.getStringValue(request, "pl_id", true); String vsr_id = WebUtils.getStringValue(request, "vsr_id", true); String cmnts = WebUtils.getStringValue(request, "cmnts", false); if (!StringUtils.isBlank(cmnts)) { cmnts = new String(Base64Utils.decode(cmnts), "UTF-8"); } JSONObject result = new JSONObject(); result.put("dataId", this.storeVsrService.addBigDeal(pl_id, vsr_id, cmnts)); jsonObject.put("data", result); jsonObject.put("status", CommonUtils.getSubStatus(" add bigDeal successfully!")); } catch (GenericException e) { jsonObject.put("status", CommonUtils.getSubStatus(false, "000001", e.getMsg())); jsonObject.put("data", ""); logger.error("error !", e); } catch (Exception e) { jsonObject.put("status", CommonUtils.getSubStatus(false, "000001", "网络或其他错误,请联系管理员!")); jsonObject.put("data", ""); logger.error("error !", e); } response.getWriter().write(jsonObject.toString()); return null; }
GenericException为自定义异常的父类,自定义的异常都要继承该类
上面代码的缺点:每写一个方法时,都要重复的写这一段:
try { } catch (GenericException e) { jsonObject.put("status", CommonUtils.getSubStatus(false, "000001", e.getMsg())); jsonObject.put("data", ""); logger.error("error !", e); } catch (Exception e) { jsonObject.put("status", CommonUtils.getSubStatus(false, "000001", "网络或其他错误,请联系管理员!")); jsonObject.put("data", ""); logger.error("error !", e); }
因为每一个controller都会继承MultiActionController,现在在每一个controller和自己定义的controller里抽象一层,因为所有controller里的方法入口
都是MultiActionController里的handleRequestInternal方法,所以重写该方法,把异常捕捉放到这个统一的入口和出口里,
新增加的类为BaseController,封装后的代码如下:
package com.intel.store.controller; import com.intel.store.common.CommonUtils; import com.intel.store.exception.GenericException; import org.codehaus.jettison.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.multiaction.MultiActionController; import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * mvc controller类的基类,用于一般controller的继承, * 把异常控制模块提取到基类, * 使开发更加简洁,快速 * Created with IntelliJ IDEA. * User: malone * Date: 14-3-17 * Time: 上午10:21 * To change this template use File | Settings | File Templates. */ public class BaseController<T extends BaseController<T>> extends MultiActionController { private Class<T> subclass; private Logger logger = LoggerFactory.getLogger(subclass); @Override protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { JSONObject jsonObject = new JSONObject(); try { String methodName = this.getMethodNameResolver().getHandlerMethodName(request); Object obj = invokeNamedMethod(methodName, request, response); System.out.println(obj.getClass()); return invokeNamedMethod(methodName, request, response); } catch (NoSuchRequestHandlingMethodException ex) { return handleNoSuchRequestHandlingMethod(ex, request, response); } catch (GenericException e) { jsonObject.put("status", CommonUtils.getSubStatus(false, "000001", e.getMsg())); jsonObject.put("data", ""); logger.error(e.getMsg(), e); response.getWriter().write(jsonObject.toString()); return null; } catch (Exception e) { jsonObject.put("status", CommonUtils.getSubStatus(false, "000001", "网络或其他错误,请联系管理员!")); jsonObject.put("data", ""); logger.error("error !", e); response.getWriter().write(jsonObject.toString()); return null; } } }
这是我的一些想法和做法,欢迎大家指正!