异常处理器
默认处理异常的方式 |
---|
1、自定义异常处理器
1.1 实现步骤
- 自定义异常
- 自定义异常处理器【实现HandlerExceptionResolver】
- 把异常处理器注入到SpringMVC容器中
- 测试
1.2 具体实现
- 自定义异常
package com.qf.java2007.ex;
/**
* @author ghy
* @version 1.0
*/
public class MoneyException extends RuntimeException {
public MoneyException() {
super();
}
public MoneyException(String message) {
super(message);
}
public MoneyException(String message, Throwable cause) {
super(message, cause);
}
public MoneyException(Throwable cause) {
super(cause);
}
protected MoneyException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
-
自定义异常处理器【实现HandlerExceptionResolver】
-
把异常处理器注入到SpringMVC容器中
package com.qf.java2007.exhandler;
import com.qf.java2007.ex.MoneyException;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* HandlerExceptionResolver 异常处理器的接口
* @author ghy
* @version 1.0
*/
@Component
public class MyExceptionHandler implements HandlerExceptionResolver {
/**
* 能够处理controller抛出所有异常
* @param request
* @param response
* @param handler
* @param ex
* @return
*/
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
System.out.println("Handler出异常了,我来处理..............." + ex.getClass());
ModelAndView mav = new ModelAndView();
if(ex instanceof MoneyException) {
mav.setViewName("error1");
mav.addObject("errorMsg", ex.getMessage());
} else {
mav.setViewName("error2");
mav.addObject("errorMsg", "服务器正忙,请稍后重试");
}
return mav;
}
}
- 测试
- handler
package com.qf.java2007.controller;
import com.qf.java2007.ex.MoneyException;
import com.qf.java2007.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
/**
* @author ghy
* @version 1.0
*/
@Controller
public class Demo4Controller {
@RequestMapping(value = "/test31")
public String test31(int num) {
if(num == 1) {
//出自定义异常
throw new MoneyException("钱不够");
} else if(num == 2) {
int i=1/0;
}
return "success";
}
}
- 效果
测试效果 |
---|
2、@ControllerAdvice + @ExceptionHandler
- 自定义异常就是MoneyException
- controller
package com.qf.java2007.controller;
import com.qf.java2007.entity.ResultVO;
import com.qf.java2007.ex.MoneyException;
import com.qf.java2007.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
/**
* @author ghy
* @version 1.0
*/
@Controller
public class Demo4Controller {
@RequestMapping(value = "/test31")
public String test31(int num) {
if(num == 1) {
//出自定义异常
throw new MoneyException("钱不够");
} else if(num == 2) {
int i=1/0;
} else if(num == 3) {
String str = "hello";
int a = Integer.valueOf(str);
}
return "success";
}
@RequestMapping("/test32")
@ResponseBody
public ResultVO test32(){
int i = 1/0;
return new ResultVO(true, "正常", "一个正常的响应");
}
}
- 异常处理器2
package com.qf.java2007.exhandler;
import com.qf.java2007.entity.ResultVO;
import com.qf.java2007.ex.MoneyException;
import com.qf.java2007.pojo.User;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import java.util.Date;
/**
* 根据异常类型指定具体方法去执行
* @author ghy
* @version 1.0
*/
@Component
@ControllerAdvice
public class MyControllerAdvice {
/**
* 出MoneyException,就会执行如下方法
* @param ex
* @return
*/
@ExceptionHandler(MoneyException.class)
public ModelAndView catchException1(Exception ex){
ModelAndView mav = new ModelAndView();
mav.addObject("errorMsg", ex.getMessage());
mav.setViewName("error1");
return mav;
}
/**
* 出ArithmeticException,就会执行如下方法
* @param ex
* @return
*/
@ExceptionHandler(ArithmeticException.class)
@ResponseBody
public ResultVO catchException2(Exception ex){
return new ResultVO(false, ex.getMessage(), "出异常了,已经被处理");
}
/**
* 如果没有能够处理异常的方法,那么就走如下方法【最大的】
* @param ex
* @return
*/
@ExceptionHandler(Exception.class)
public ModelAndView catchException3(Exception ex){
ModelAndView mav = new ModelAndView();
mav.addObject("errorMsg", ex.getMessage());
mav.setViewName("error2");
return mav;
}
}