HandlerExceptionResolver异常处理(业务异常)

本文介绍了一种基于Java的业务异常处理方案,通过自定义异常类和异常处理器来统一处理业务异常,确保系统的稳定运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值