修改异常处理方式4中的全局异常处理controller

SpringBoot: 15.异常处理方式5(通过实现HandlerExceptionResolver类)(转)-LMLPHP
package com.bjsxt.exception;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.Nullable;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Properties; /**
* Created by Administrator on 2019/2/14.
* 全局异常处理类,通过实现 HandlerExceptionResolver类做全局异常处理
* 优点:相对于方法4可以传递异常信息
*/ @Configuration
public class GlobalException implements HandlerExceptionResolver{ @Override
public ModelAndView resolveException(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse, @Nullable Object o, Exception e) { ModelAndView mv = new ModelAndView();
//判断不同异常类型,做不同视图跳转
if(e instanceof ArithmeticException){
mv.setViewName("error_arithmetic");
}
if(e instanceof NullPointerException){
mv.setViewName("error_nullPointer");
}
mv.addObject("msg", e.toString());
return mv;
}
}
SpringBoot: 15.异常处理方式5(通过实现HandlerExceptionResolver类)(转)-LMLPHP
05-28 19:12