一 注解:
@SpringBootApplication:申明让spring boot自动给程序进行必要的配置,这个配置等同于:@Configuration ,@EnableAutoConfiguration 和 @ComponentScan 三个配置。
@RestController:用于标注控制层组件(如struts中的action),@ResponseBody和@Controller的合集。
@ControllerAdvice:用此注解去定义全局异常统一处理的类。
@ControllerAdvice 注解定义全局异常处理类
@ControllerAdvice
public class GlobalExceptionHandler {
}
@ExceptionHandler 注解声明异常处理方法
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler()
@ResponseBody
String handleException(Exception e){
return "Exception Deal! " + e.getMessage();
}
}
方法 handleException() 就会处理所有 Controller 层抛出的 Exception 及其子类的异常,这是最基本的用法了。
二:配置 或 需注意的场景
(1)注意缓存的用法 比如 @EnableCaching @CahePut @CacheEvict @Cacheable
(2) 注意设置接口响应超时时间 : 在application.properties 文件中,添加 spring.mvc.async.request-timeout=20000,意思是设置超时时间为20000ms即20s,