异常处理---spring boot

Posted xingshouzhan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了异常处理---spring boot相关的知识,希望对你有一定的参考价值。

 

异常进行处理

@Controller
@RequestMapping("/exceptioncontroller")
public class ExceptionController {

@RequestMapping("/exc")
public String exc(){
int a = 1/0;
return null;
}
}

 

  1.直接定义error界面

 SpringBoot启动的WEB应用中,会自动的提供一个映射,URL是/error(即templates下面的error.html界面),处理了这个请求的是类型BasicErrorController,其中的处理逻辑是将异常信息分类封装到作用域中,并传递给视图逻辑’error’。

但是,springboot项目,无法直接访问templates下面的界面,需要配置thymeleaf

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>

在templates下面定义error.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>自定义异常页面</title>
</head>
<body>
自定义异常页面...
</body>
</html>

 

2.使用@ExceptionHandler,在controller层,错误特殊处理。注解的value属性是Class[]类型,代表该方法可以处理的异常种类

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
@Controller
@RequestMapping("/exceptioncontroller")
public class ExceptionController {

    @Autowired
    EeceptionService eceptionService;
@RequestMapping(
"/exc") public String exc(){ eceptionService.test(); return null; } @ExceptionHandler(value = Exception.class) @ResponseBody public String ExceptionHandlerThrow(){ return "epc"; } }
@Service
public class EeceptionService {

    public void test(){
        int a = 1/0;
    }
}

 

3.使用@ControllerAdvice与@ExceptionHandler共同作用

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
@Controller
@RequestMapping("/exceptioncontroller")
public class ExceptionController {

    @Autowired
    EeceptionService eceptionService;
    @RequestMapping("/exc")
    public String exc(){
        eceptionService.test();
        return null;
    }
}
@Service
public class EeceptionService {

    public void test(){
        int a = 1/0;
    }
}
@ControllerAdvice
public class EeceptionHandler {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public String ExceptionHandlerThrow(){
        return "ad_epc";
    }
}

 

4.实现ErrorController接口(是BasicErrorController的父接口)

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
@Controller
@RequestMapping("/exceptioncontroller")
public class ExceptionController {

    @Autowired
    EeceptionService eceptionService;

    @RequestMapping("/exc")
    public String exc(){
        eceptionService.test();
        return null;
    }
}
@Service
public class EeceptionService {

    public void test(){
        int a = 1/0;
    }
}
@Controller
public class ErrorsController  implements ErrorController {

    @Override
    public String getErrorPath() {
        return "/error";
    }

    @RequestMapping(value = "/error",  produces = {MediaType.APPLICATION_JSON_VALUE})
    @ResponseBody
    public String error(HttpServletRequest request) {
        return "adfswe";
    }
}

 

5.配置SimpleMappingExceptionResolver,通过设置Properties定义返回界面

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>
@Controller
@RequestMapping("/exceptioncontroller")
public class ExceptionController {

    @Autowired
    EeceptionService eceptionService;

    @RequestMapping("/exc")
    public void exc(){
        eceptionService.test();
        //return "redirect:templates/error.htm";
    }
}
@Service
public class EeceptionService {

    public void test(){
        int a = 1/0;
    }
}
@Configuration
public class GlobalExceptionConfiguration {
    @Bean
    public SimpleMappingExceptionResolver getHandlerExceptionResolver(){
        SimpleMappingExceptionResolver bean =
                new SimpleMappingExceptionResolver();
        Properties mappings = new Properties();
        mappings.setProperty("java.lang.NullPointerException", "configNullPointerException");
        mappings.setProperty("java.lang.ArithmeticException", "configArithmeticException");
        bean.setExceptionMappings(mappings);
        return bean;
    }
}

templates下面新增configArithmeticException.html和configNullPointerException.html界面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>configNullPointerException..</title>
</head>
<body>
configNullPointerException..
</body>
</html>

 

 

6.继承

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>
@Configuration
public class MyHandlerExceptionResolver implements HandlerExceptionResolver {

    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, 
            Object handler,
            Exception ex) {
        
        // 异常信息收集
        // String message = ex.getMessage();
        // request.setAttribute("message", message);
        
        ModelAndView mv = new ModelAndView();
        // 异常信息收集
        mv.addObject("message", "出错了,请联系管理员, 错误内容是: " + ex.getMessage());
        mv.setViewName("error");
        
        return mv;
    }
}
@Controller
@RequestMapping("/exceptioncontroller")
public class ExceptionController {

    @Autowired
    EeceptionService eceptionService;

    @RequestMapping("/exc")
    public void exc(){
        eceptionService.test();
        //return "redirect:templates/error.htm";
    }
}
@Service
public class EeceptionService {

    public void test(){
        int a = 1/0;
    }
}

和上面一样,在自定义error界面

 

 

优先级: 当前控制器中定义的@ExceptionHandler方法 -> @ControllerAdvice类中定义的@ExceptionHandler方法 ->ErrorController中定义的服务方法(error.html默认异常页面)

 

一般情况下,先在方法里面进行处理;如果controller有特殊处理,使用@ExceptionHandler;其实一般情况下定义全局的异常控制,即使用@ControllerAdvice与@ExceptionHandler共同作用;继承ErrorController处理,其实这里就可以进行自定义error返回界面。

 

这里我使用的有些是@ResponseBody,就是使用的springMVC。相信若是转成返回界面,或者返回自定义界面,就不是什么难事了。

 

以上是关于异常处理---spring boot的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot? 统一异常处理

如何使用 Spring Boot 对 WebFlux 进行异常处理?

Spring Boot 服务间通信异常处理

spring boot 覆盖默认的 REST 异常处理程序

spring boot 覆盖默认的 REST 异常处理程序

Spring Boot:AOP&日志操作&异常处理