014-Spring Boot web【三】拦截器HandlerInterceptor、异常处理页面,全局异常处理ControllerAdvice

1、拦截器HandlerInterceptor

1.一、HandlerInterceptor接口说明

  preHandle,congtroller执行前,若是返回false请求终端html

  postHandle,controller执行以后,页面渲染前java

  afterCompletion,整个请求结束后,页面也渲染完毕,通常是资源清理操做web

  同时提供异步拦截器AsyncHandlerInterceptorspring

1.二、拦截器使用步骤

  1》写一个拦截器,实现HandlerInterceptor 接口springboot

  2》写一个类,继承WebMvcConfigurerAdapter抽象类,而后重写addInterceptors方法,把上一步的拦截器加入registry.addInterceptor(new LogHandlerInterceptor());app

1.三、示例

  启动类异步

package com.lhx.spring.springboot_web; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
View Code

  创建UserControlleride

package com.lhx.spring.springboot_web; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controller public class UserController { @GetMapping(value = "/user/home") public String home() { System.out.println("--------user home--------"); return "user home"; } }
View Code

  增长拦截器LogHandlerInterceptor post

package com.lhx.spring.springboot_web; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; public class LogHandlerInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("----------preHandle----------"+handler.getClass()); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("----------postHandle----------"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("----------afterCompletion----------"); } }
View Code

  增长配置类WebConfiguration 编码

package com.lhx.spring.springboot_web; import org.springframework.boot.SpringBootConfiguration; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @SpringBootConfiguration public class WebConfiguration extends WebMvcConfigurerAdapter { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new LogHandlerInterceptor()); } }
View Code

2、异常处理

2.一、编码抛出异常代码

@GetMapping(value = "/user/help") @ResponseBody public String help() { throw new IllegalArgumentException("args is empty"); } 
View Code

2.二、分析代码

  查看:org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration,发现默认已经被springboot定制。

  去除springboot默认异常显示,只需在启动类添加:@SpringBootApplication(exclude=ErrorMvcAutoConfiguration.class)便可。则返回错误就会变成Tomcat提供的

2.三、添加自定义错误页

  1》去除springboot默认的:@SpringBootApplication(exclude=ErrorMvcAutoConfiguration.class)

  2》在src/main/resources下增长public,在pulic中增长404.html,500.html

  3》增长CommonErrorPageRegistry实现ErrorPageRegistrar增长@Component注解,能够针对错误码处理或者具体异常处理

package com.lhx.spring.springboot_web; import org.springframework.boot.web.servlet.ErrorPage; import org.springframework.boot.web.servlet.ErrorPageRegistrar; import org.springframework.boot.web.servlet.ErrorPageRegistry; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; @Component public class CommonErrorPageRegistry 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 args = new ErrorPage(IllegalArgumentException.class, "/args.html"); registry.addErrorPages(e404); registry.addErrorPages(e500); registry.addErrorPages(args); } }
View Code

    以上将404,500逻辑添加

 3、全局异常处理

3.一、局部异常处理

  当前Controller使用,能够在当前Controller中使用ExceptionHandler注解

以下代码

package com.lhx.spring.springboot_web; import java.io.FileNotFoundException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class BookController { @ExceptionHandler(value = Exception.class) public String error(Exception e) { return "fount exception:"+e.getMessage(); } @GetMapping("book/error1") public String error1() throws FileNotFoundException { throw new FileNotFoundException("book not found"); } @GetMapping("book/error2") public String error2() throws ClassNotFoundException { throw new ClassNotFoundException("class not found"); } }
View Code

  其中:@ExceptionHandler(value = Exception.class) 表明全部;也可指代具体异常,如文件没有发现@ExceptionHandler(value = FileNotException.class)

3.二、全局异常处理

  1》写一个GlobalExceptionHandler异常处理类,而且使用@ControllerAdvice注解

  2》写一个方法,须要添加@ExceptionHandler(value = Exception.class)注解,在方法中编写具体代码逻辑

package com.lhx.spring.springboot_web; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; @ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(value = Exception.class) @ResponseBody public String errorHandler(Exception e) { return "global error:" + e.getClass().getName(); } }
View Code

  能够有多个不一样的异常处理。

3.三、就近原则

  局部异常优先级高于全局异常处理

一个比较通用的写法

@ControllerAdvice @ResponseBody public class ResponseExceptionHandler { Logger logger=LoggerFactory.getLogger(ResponseExceptionHandler.class); @ExceptionHandler(ResponseException.class) public ResponseEntity<ResponseResult> handleException(Exception e) { logger.error("请求异常信息",e); ResponseException cex = (ResponseException) e; return new ResponseEntity(ResponseResult.error(cex.getResponseCode()), HttpStatus.OK); } }
相关文章
相关标签/搜索