zoukankan      html  css  js  c++  java
  • Spring Boot 全局异常配置

    Spring Boot 全局异常配置,处理异常控制器需要和发生异常的方法在一个类中。使用 ControllerAdvice 注解

    package com.li.controller;
    
    import org.springframework.stereotype.Controller;
    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.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @Controller
    public class ExceptionHandlerController {
        @RequestMapping("/exceptionTest")
        @ResponseBody
        public String test() throws Exception{
            System.out.println("测试");
            int i=1/0;
            return Integer.toString(i);
        }
        @ControllerAdvice
        class HandlerException{
            @ExceptionHandler(Exception.class)
            @ResponseBody
            public String defultExcepitonHandler(Exception e) {
        //        return "{"error":"error"}";
                return "error";
            }
        }
    }

    https://blog.csdn.net/qq_34083066/article/details/79424142

  • 相关阅读:
    10_树基础部分
    11_多线程
    14_JUC
    Servlet代码实例
    关于JSP引用资源路径
    Struts2代码实例
    hadoop三大核心组件介绍
    presto自定义函数开发
    git使用
    hive优化总结
  • 原文地址:https://www.cnblogs.com/liyafei/p/9179225.html
Copyright © 2011-2022 走看看