________________初学springboot13

resource/src/webapp下的文件可以直接访问,以下为四个静态资源的路径,可以通过配置项修改

"classpath:/META-INF/resources/", "classpath:/resources/",

"classpath:/static/", "classpath:/public/"

spring boot 中Servlet的api 

 springboot 中拦截器

1、写一个拦截器实现HandlerInterceptor接口

2、写一个类继承WebMvcConfigurerAdapter接口,重写addIntercepters方法,并调用registry.addInterceptor 加入刚才的拦截器

preHandle  controller执行之前调用

 postHandle  controller执行之后,页面渲染之前调用 

 afterCompletion  页面渲染之后调用,一般用于资源清理操作

springboot中的异常处理

禁用系统默认的异常处理(exclude=ErrorMcvAutoCon(exclude=ErrorMcvAutoConfiguration.class))

自定义异常处理

方法一、

配置一个ErrorPageRegistrar的实现类,添加自定义异常处理逻辑

@Component

public class CommonErroPageRegistry implements ErrorPageRegistrar {

@Override

public void registerErrorPages(ErrorPageRegistry registry) {

ErrorPage e404=new ErrorPage(HttpStatus.NOT_FOUND, "/404.html");

ErrorPage e500=new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html");

ErrorPage esy=new ErrorPage(HttpStatus.EXPECTATION_FAILED, "/sy.html");

registry.addErrorPages(e404,e500,esy);

}

}

方法二、

配置抛出异常,捕获异常

@GetMapping("/user/error1")

public String list1() throws ClassNotFoundException{

throw new ClassNotFoundException("类异常,找不到,抱歉");

}

@ExceptionHandler(value=FileNotFoundException.class)

public String error(){

return "<h1>file not found</h1>";

 全局异常处理

1、写一个类加上@ControllerAdvice注解

2、写一个异常处理方法,方法上需要加上@ExceptionHandler(value=Exception.class) 然后在这个类里面处理异常

 

posted @ 2018-07-27 11:21  637  阅读(88)  评论(0编辑  收藏  举报