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;
}
}