springmvc中的异常处理器

目录

一,通过实现HandlerExceptionResolver接口完成异常处理;(注意:此接口需导入包:spring-webmvc-5.0.8.RELEASE.jar)

1,异常处理器原理;

2,使用异常处理器捕获运行时异常;

3,使用异常处理器捕获自定义异常;

二,通过注解完成全局异常处理;(推荐使用注解式)这里出了一点bug,没运行成功!但这种继承BaseController的方式也可实现全局异常处理,也推荐


 

一,通过实现HandlerExceptionResolver接口完成异常处理;(注意:此接口需导入包:spring-webmvc-5.0.8.RELEASE.jar)

1,异常处理器原理;

(异常处理器原理图)

其他代码同:https://blog.csdn.net/qq_40323256/article/details/92174265

不同处如下:

MyException.java:

package com.lijiang.exception;

public class MyException extends Exception {

	private String msg;

	public String getMsg() {
		return msg;
	}

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

	public MyException(String msg) {
		super();
		this.msg = msg;
	}
	
}

MyHandlerExceptionResolver.java:

package com.lijiang.exception;

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

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

public class MyHandlerExceptionResolver implements HandlerExceptionResolver {

	@Override
	public ModelAndView resolveException(
			HttpServletRequest request, 
			HttpServletResponse response,
			Object obj,
			Exception e) {
	ModelAndView mav=new ModelAndView();
	
	String errorMsg="";
	
	if(e instanceof MyException){
//		执行自定义异常处理
		errorMsg="自定义异常:"+((MyException)e).getMsg()+" "+obj;
	}else{
//		运行时异常
		errorMsg="运行时异常:"+e.getMessage()+" "+obj;
	}
	
	mav.addObject("error", errorMsg);
	mav.setViewName("error");
		return mav;
	}

}

 springmvc.xml:

<!-- 		配置异常处理器 -->
		<bean class="com.lijiang.exception.MyHandlerExceptionResolver"></bean>

error.jsp: 

<body>
<h1>${error }</h1>
</body>

2,使用异常处理器捕获运行时异常;

	@Override
	public List<ItemInfo> selectAll(ItemInfo itemInfo) throws MyException {
		
//		运行时异常
		int i=1/0;
		
//		自定义异常
//		if(true){
//			throw new MyException("列表没有获取到!!");
//		}
		
		return itemMapper.selectAll(itemInfo);
	}

运行结果:

 

3,使用异常处理器捕获自定义异常;

	@Override
	public List<ItemInfo> selectAll(ItemInfo itemInfo) throws MyException {
		
//		运行时异常
//		int i=1/0;
		
//		自定义异常
		if(true){
			throw new MyException("列表没有获取到!!");
		}
		
		return itemMapper.selectAll(itemInfo);
	}

运行结果:

二,通过注解完成全局异常处理;(推荐使用注解式这里出了一点bug,没运行成功!但这种继承BaseController的方式也可实现全局异常处理,也推荐

1,通过@ControllerAdvice注解使@ExceptionHandler异常处理注解应用到所有使用@RequestMapping的方法上;

2,处理运行时异常:直接打印错误信息;

3,处理自定义异常:将错误信息输出到error页面上;

MyGlobalExceptionHandler.java:

package com.lijiang.exception;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class MyGlobalExceptionHandler {

//	处理运行时异常
	@ExceptionHandler(RuntimeException.class)
	@ResponseBody
	String runtimeHandler(RuntimeException e){
		return e.getMessage();
	}
	
//	处理自定义异常 MyException
	@ExceptionHandler(MyException.class)
	public String myException(MyException e,Model model){
		model.addAttribute("error", e.getMsg()+" "+e.getStackTrace()[0]);
		return "error";
	}
}

其他不变,记得注释掉刚刚的springmvc.xml里配置的异常处理器:<bean class="com.lijiang.exception.MyHandlerExceptionResolver"></bean>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值