方法一:设置只能解决本类的异常处理方式:
使用 @ExceptionHandler
只要报错就跳转到设置好的error页面 (我这里已经写了省去前后缀的代码)
@ExceptionHandler
public ModelAndView error(Exception ex) {
ex.printStackTrace();
ModelAndView mv = new ModelAndView();
mv.addObject("msg", ex.getMessage());
mv.setViewName("error");
return mv;
}
方法二:全局跳转,让类继承HandlerExceptionResolver 接口
@Controller
public class ExceptionResolover implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse,
Object o, Exception e) {
ModelAndView mv = new ModelAndView();
mv.addObject("msg","异常的判断");
mv.setViewName("");
if (e instanceof NullPointerException ){
mv.setViewName("nullError");//跳转到设置好的nullError页面
} else if (e instanceof ArithmeticException) {
mv.setViewName("numerror");//跳转到设置好的numerror页面
}else {
mv.setViewName("error");//跳转到设置好的error页面
}
return mv;
}
}
方法三:使用@ControllerAdvice和 @ExceptionHandler 联合使用
@ControllerAdvice
public class MyControllerAdvice {
//将所有的异常搞到这里来
@ExceptionHandler
public ModelAndView test01(Exception e) {
e.printStackTrace();
ModelAndView mv = new ModelAndView();
mv.addObject("msg", e.getMessage());
mv.setViewName("error");//输出到设置好的error.jsp页面中
return mv;
}
//下面举三个例子,进行单独的异常处理方式
//设置专门的异常 只有出现ArithmeticException 异常才会跳转到固定的页面
@ExceptionHandler(value = ArithmeticException.class)
public ModelAndView test02(Exception e) {
e.printStackTrace();
ModelAndView mv = new ModelAndView();
mv.addObject("msg", e.getMessage());
mv.setViewName("numerror");
return mv;
}
@ExceptionHandler(value = NullPointerException.class)
public ModelAndView test03(Exception e) {
e.printStackTrace();
ModelAndView mv = new ModelAndView();
mv.addObject("msg", e.getMessage());
mv.setViewName("nullError");
return mv;
}
@ExceptionHandler(value = ArrayIndexOutOfBoundsException.class)
public ModelAndView test04(Exception e) {
e.printStackTrace();
ModelAndView mv = new ModelAndView();
mv.addObject("msg", e.getMessage());
mv.setViewName("othererror");
return mv;
}
}
自定义异常的方法
//自己设置的异常处理方法
public class BizException extends Exception {
private int code;
public BizException(int code, String msg) {
super(msg);
this.code = code;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
}
设置好了以后在有需要抛出异常的地方将BizException带入
@RestController
public class MyRestControllerAdvice {
@ExceptionHandler
public Object test01(Exception e) throws RuntimeException {
ResponeDTO dto = new ResponeDTO();
if (e instanceof BizException) {
//将异常e的类型强转
BizException BizException = (BizException) e;
dto.setCode(BizException.getCode());
} else {
dto.setCode(999);
}
dto.setMessage(e.getMessage());
return dto;
}
}
@GetMapping("/product02")
public ResponeDTO res(@RequestParam(value = "") String name) throws Exception {
Product product = Product.builder().price(BigDecimal.valueOf(10.0)).qty(100).name("苹果").build();
//这里定义的 code message 和object类型的data
ResponeDTO responeDTO = new ResponeDTO();
responeDTO.setCode(0);
responeDTO.setData(product);
if (product.equals("河北彩花")) {
//为什么要这样写呢?
throw new BizException(808,"不要在看了");
}
return responeDTO;
}