SpringMVC之异常处理

SpringMVC之异常处理

异常分为编译时异常和运行时异常,编译时异常我们trycatch捕获,捕获后自行处理,而运行时异常是不可预期的,就需要规范编码来避免,在SpringMVC中,不管是编译异常还是运行时异常,都可以最终由SpringMVC提供的异常处理器进行统一管理,这样就可以避免随时随地捕获异常的繁琐性。

三种处理方式

1.简单异常处理器:使用Spring MVC内置的异常处理器处理:SimpleMappingExceptionResolver

java 复制代码
@Component
public class MysimpleMappingExceton  {
    @Bean
    public SimpleMappingExceptionResolver simpleMappingExceptionResolver(){
        SimpleMappingExceptionResolver simpleMappingExceptionResolver = new SimpleMappingExceptionResolver();
        //默认错误
        simpleMappingExceptionResolver.setDefaultErrorView("default.html");
        Properties properties = new Properties();
        properties.setProperty("java.lang.ArithmeticExceotion","erro1.html");
        properties.setProperty("java.io.FileNotFoundException","erro2.html");
        simpleMappingExceptionResolver.setExceptionMappings(properties);
        return simpleMappingExceptionResolver;
    }
}

2.自定义异常处理器:实现HandlerExceptionResolver接口,自定义异常进行处理

java 复制代码
@Component
public class MyHandlerExceptionResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/default.html");
        return modelAndView;
    }
}

3.使用@ControllerAdvice@ExceptionHandler实现全局异常

java 复制代码
@ControllerAdvice
public class GloExceotion {
    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public Result runtimeException(){
        Result result = new Result(200,"错误",new Object());
        return result;
    }
    @ExceptionHandler(FileNotFoundException.class)
    public ModelAndView fileNotException(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/erro2.html");
        return modelAndView;
    }
    @ExceptionHandler(Exception.class)
    public ModelAndView Exception(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/default.html");
        return modelAndView;
    }
}

例子

java 复制代码
@RestController
public class ExceptionController {
    @RequestMapping("/e1")
    public String e1 (){
        int a=10/0;
        return "ruuning exception";
    }
    @RequestMapping("/e2")
    public String e2() throws FileNotFoundException {
        FileInputStream fileInputStream = new FileInputStream("file:/barch:/");
        return "ruuning exception";
    }
    @RequestMapping("/e3")
    public String e3()  {
        int [] array ={1,2};
        System.out.println(array[5]);
        return "ruuning exception";
    }
}
相关推荐
H Journey几秒前
web开发学习:html、css、js
前端·css·html·js
hunterandroid6 分钟前
Compose 性能优化:从卡顿到丝滑的实战经验
前端
hunterandroid10 分钟前
Kotlin Coroutines 在 Android 项目中的落地实战
前端
不一样的少年_18 分钟前
不用框架,手搓 AI Agent:(一) 先让它跑起来
前端·后端·agent
味悲18 分钟前
浏览器解析机制与XSS的15种编码绕过
前端·xss
小牛itbull27 分钟前
【译】为什么我使用 React 重构了 WordPress?
前端·react.js·重构
用户0595401744628 分钟前
AI Agent 记忆存储踩坑实录:这个问题让我排查了 3 天,最终用 pytest + Docker 实现秒级回归
前端·css
今日无bug32 分钟前
Emmet 语法速成指南
前端·css·html
亦暖筑序35 分钟前
AgentScope-Java 入门:用 SSE 展示评审任务进度
java·agent·ai编程
Hello.Reader35 分钟前
Tailwind CSS v4.3 从入门到实战Vite 安装、响应式布局、暗黑模式与主题配置
前端·css