Spring mvc 异常处理 例子

摘要: 异常处理,在程序开发过程中,是个很重要的部分,能处理的异常我们自己处理,否则抛出来,如果用spring mvc 最终是抛到 controller 层。所以 spring mvc 的异常处理统一在controller 处理。在 spring 3.0 以上,提供了  @ExceptionHandler  这个class 去处理异常. 下面记录 spring mvc 异常处理常用方法

异常处理,在程序开发过程中,是个很重要的部分,能处理的异常我们自己处理,否则抛出来,如果用spring mvc 最终是抛到 controller 层。所以 spring mvc 的异常处理统一在controller 处理。在 spring 3.0 以上,提供了 @ExceptionHandler 这个class 去处理异常. 下面记录 spring mvc 异常处理常用方法.

1. 定义自己的业务异常处理类,比如

public class CustomGenericException extends RuntimeException {
 
	private static final long serialVersionUID = 1L;
 
	private String errCode;
	private String errMsg;
 
	public String getErrCode() {
		return errCode;
	}
 
	public void setErrCode(String errCode) {
		this.errCode = errCode;
	}
 
	public String getErrMsg() {
		return errMsg;
	}
 
	public void setErrMsg(String errMsg) {
		this.errMsg = errMsg;
	}
 
	public CustomGenericException(String errCode, String errMsg) {
		this.errCode = errCode;
		this.errMsg = errMsg;
	}
 
}


2. 在 Controller 层中 利用 @ExceptionHandler 加入相应的业务异常 各自 怎么处理的逻辑. 例子如下:
import java.io.IOException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
 
import com.mkyong.web.exception.CustomGenericException;
 
@Controller
public class MainController {
 
	@RequestMapping(value = "/{type:.+}", method = RequestMethod.GET)
	public ModelAndView getPages(@PathVariable("type") String type)
		throws Exception {
 
	  if ("error".equals(type)) {
		// go handleCustomException
		throw new CustomGenericException("E888", "This is custom message");
	  } else if ("io-error".equals(type)) {
		// go handleAllException
		throw new IOException();
	  } else {
		return new ModelAndView("index").addObject("msg", type);
	  }
 
	}
 
        // 这里处理自定义的业务异常,当有 CustomGenericException 抛出时会执行.
	@ExceptionHandler(CustomGenericException.class)
	public ModelAndView handleCustomException(CustomGenericException ex) {
 
		ModelAndView model = new ModelAndView("error/generic_error");
		model.addObject("errCode", ex.getErrCode());
		model.addObject("errMsg", ex.getErrMsg());
 
		return model;
 
	}
  
        // 处理所有异常,自定义异常之外的异常都执行到这里.
	@ExceptionHandler(Exception.class)
	public ModelAndView handleAllException(Exception ex) {
 
		ModelAndView model = new ModelAndView("error/generic_error");
		model.addObject("errMsg", "this is Exception.class");
 
		return model;
 
	}
 
}


3. 定义错误处理页面, 显示出错的信息,这里就不怎么写出代码了,应该很简单的。
但是用上面的方法做出来的,只是针对一个 Controller 做出的异常处理, 但事实上在程序开发过程中,有很多个Controller , 有什么办法, 让这些异常处理的方法适用于所有 Controller 呢, 至少有两种方法。

方法一,将这些处理异常的方法 放到一个 BaseController 中,其他 Controller 从这个BaseController 中继承过来就可以了。

方法二,利用 Spring 提供的 @ControllerAdvice 实现,如下所示:
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
import com.mkyong.web.exception.CustomGenericException;
 
@ControllerAdvice
public class GlobalExceptionController {
 
	@ExceptionHandler(CustomGenericException.class)
	public ModelAndView handleCustomException(CustomGenericException ex) {
 
		ModelAndView model = new ModelAndView("error/generic_error");
		model.addObject("errCode", ex.getErrCode());
		model.addObject("errMsg", ex.getErrMsg());
 
		return model;
 
	}
 
	@ExceptionHandler(Exception.class)
	public ModelAndView handleAllException(Exception ex) {
 
		ModelAndView model = new ModelAndView("error/generic_error");
		model.addObject("errMsg", "this is Exception.class");
 
		return model;
 
	}
 
}


这样就能对所有的 Controller 应用这些异常处理了.

上一篇: 简单方法合并两个java list
下一篇: ie下用脚本提交表单后,不能自动提示的问题
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章或者资源来源于互联网, 有时候很难判断是否侵权, 若有侵权, 请联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布无版权争议的 文章/资源.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号