@ContrllerAdvice全局异常

@ControllerAdvice,是Spring3.2提供的新注解,它是一个Controller增强器,可对controller中被 @RequestMapping注解的方法加一些逻辑处理。最常用的就是异常处理,需要搭配@ExceptionHandler使用

   

@ExceptionHandler 注解用来指明异常的处理类型,即如果指定为 NullpointerException,则数组越界异常就不会进到这个方法中来

   

   

   

@ControllerAdvice使用方法

   

首先在SpringBoot中导入thymeleaf的依赖

   

<dependency>

<groupId>org.springframework.bootgroupId>

<artifactId>spring-boot-starter-thymeleafartifactId>

dependency>

   

其次在SpringBoot项目的templates文件夹中创建一个thymeleaf的html

   

@ContrllerAdvice全局异常_第1张图片

导入上图红框的依赖就可以使用thymeleaf的语法了!

   

@ContrllerAdvice全局异常_第2张图片

你可能感兴趣的:(@ContrllerAdvice全局异常)