1. 程式人生 > >HandlerExceptionResolver異常處理(業務異常)

HandlerExceptionResolver異常處理(業務異常)

1.建立業務異常類

/**
 * 業務異常型別
 * @author kp.li
 *
 */
public class BussinessException extends Exception {
	/**
	 * 
	 */
	private static final long serialVersionUID = -5201877185169815637L;
	public BussinessException(){}	
	public BussinessException(String code,String msg){
		this.code = code;
		this.msg = msg;
	}
	//錯誤碼
	private String code;
	//錯誤資訊
	private String msg;
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
}

2.業務異常處理類

package com.ctrip.common.base;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.support.spring.FastJsonJsonView;
import com.ctrip.common.constant.Constants;
import com.ctrip.common.util.BussinessException;
import com.ctrip.framework.clogging.agent.log.ILog;
import com.ctrip.framework.clogging.agent.log.LogManager;

public class SpringHandlerExceptionResolver implements HandlerExceptionResolver {
	ILog logger = LogManager.getLogger(SpringHandlerExceptionResolver.class);
	@Override
	public ModelAndView resolveException(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex) {
		 ModelAndView mv = new ModelAndView();    
         /*  使用FastJson提供的FastJsonJsonView檢視返回,不需要捕獲異常   */    
         FastJsonJsonView view = new FastJsonJsonView();    
         Map<String, Object> attributes = new HashMap<String, Object>();
         if(ex instanceof BussinessException){
        	 attributes.put(Constants.RET_CODE, ((BussinessException) ex).getCode());    
             attributes.put(Constants.RET_MSG, ((BussinessException) ex).getMsg()); 
             logger.info("發生業務異常,異常資訊,code:"+ ((BussinessException) ex).getCode()+",msg:"+((BussinessException) ex).getMsg());
         }else{
        	 attributes.put(Constants.RET_CODE, "111");    
             attributes.put(Constants.RET_MSG, "伺服器異常,請稍後再試"); 
             logger.error("系統異常,異常資訊:"+ex);
         }
         view.setAttributesMap(attributes);    
         mv.setView(view);     
         return mv;    
	}
}

3.註冊異常處理器

<!-- 註冊異常處理器 -->
<bean id="exceptionHandler" class="com.ctrip.common.base.SpringHandlerExceptionResolver" />  

4.測試controller

package com.ctrip.common.controller.wx;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.ctrip.common.util.BussinessException;

/**
 * @author kp.li
 *
 */
@Controller
public class TestExceptionHandler {

	@RequestMapping(value = "/exceptionTest", method = { RequestMethod.POST, RequestMethod.GET })
	public ModelAndView exceptionTest(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception{
		String username = request.getParameter("username");
		if(StringUtils.isBlank(username)){
			throw new BussinessException("0001","使用者名稱為空");
		}
		return null;
	}
}