温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringBoot处理全局统一异常的方法和区别

发布时间:2021-09-13 11:00:46 来源:亿速云 阅读:148 作者:chen 栏目:编程语言

这篇文章主要讲解了“SpringBoot处理全局统一异常的方法和区别”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“SpringBoot处理全局统一异常的方法和区别”吧!

前言

在后端发生异常或者是请求出错时,前端通常显示如下

Whitelabel Error PageThis application has no explicit mapping for /error, so you are seeing this as a fallback.

Fri Jun 07 15:38:07 CST 2019There was an unexpected error (type=Not Found, status=404).No message available

对于用户来说非常不友好。

本文主要讲解如何在SpringBoot应用中使用统一异常处理。

实现方式

第一种:使用@ControllerAdvice和@ExceptionHandler注解  第二种: 使用ErrorController类来实现。

第一种:使用@ControllerAdvice和@ExceptionHandler注解

@Slf4j@ControllerAdvicepublic class GlobalExceptionHandler { @ResponseBody @ExceptionHandler(NullPointerException.class) public BaseResult globalException(HttpServletResponse response,NullPointerException ex){ log.info("GlobalExceptionHandler...");log.info("错误代码:" + response.getStatus());BaseResult result = new WebResult(WebResult.RESULT_FAIL,"request error:"+response.getStatus()     ,"GlobalExceptionHandler:"+ex.getMessage()); return result;}}

注解@ControllerAdvice表示这是一个控制器增强类,当控制器发生异常且符合类中定义的拦截异常类,将会被拦截。

可以定义拦截的控制器所在的包路径

@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@Componentpublic @interface ControllerAdvice { @AliasFor("basePackages") String[] value() default {}; @AliasFor("value") String[] basePackages() default {}; Class<?>[] basePackageClasses() default {}; Class<?>[] assignableTypes() default {}; Class<? extends Annotation>[] annotations() default {};}

注解ExceptionHandler定义拦截的异常类

@Target({ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface ExceptionHandler { Class<? extends Throwable>[] value() default {};}

第二种: 使用ErrorController类来实现。

系统默认的错误处理类为BasicErrorController,将会显示如上的错误页面。

这里编写一个自己的错误处理类,上面默认的处理类将不会起作用。

getErrorPath()返回的路径服务器将会重定向到该路径对应的处理类,本例中为error方法。

@Slf4j@RestControllerpublic class HttpErrorController implements ErrorController { private final static String ERROR_PATH = "/error"; @ResponseBody @RequestMapping(path = ERROR_PATH ) public BaseResult error(HttpServletRequest request, HttpServletResponse response){ log.info("访问/error" + " 错误代码:" + response.getStatus()); BaseResult result = new WebResult(WebResult.RESULT_FAIL,"HttpErrorController error:"+response.getStatus());return result; } @Override public String getErrorPath() { return ERROR_PATH; }}

测试

以上定义了一个统一的返回类BaseResult,方便前端进行处理。

package com.microblog.common.result;import java.io.Serializable;public class BaseResult implements Serializable { private static final long serialVersionUID = 1L; public static final int RESULT_FAIL = 0; public static final int RESULT_SUCCESS = 1; //返回代码 private Integer code; //返回消息 private String message; //返回对象 private Object data; public BaseResult(Integer code, String message) {  this.code = code;  this.message = message; } public BaseResult(Integer code, String message, Object object) {  this.code = code;  this.message = message;  this.data = object; } public Integer getCode() {  return code; } public void setCode(Integer code) {  this.code = code; } public String getMessage() {  return message; } public void setMessage(String message) {  this.message = message; } public Object getData() {  return data; } public void setData(Object data) {  this.data = data; }}

编写一个测试控制器

@Slf4j@RestController@RequestMapping("/user")public class TestController { @RequestMapping("/info1") public String test(){  log.info("/user/info1");  throw new NullPointerException("TestController have exception"); }}

1.发出一个错误的请求,也就是没有对应的处理类。

从返回可以看到是由HttpErrorController类处理

{"code":0,"message":"HttpErrorController error:404","data":null}

2.发出一个正常的请求(TestController的test()处理),处理类中抛出空异样

从返回中可以看出是由GlobalExceptionHandler类处理

{"code":0,"message":"request error:200","data":"GlobalExceptionHandler:TestController have exception"}

区别

1.注解@ControllerAdvice方式只能处理控制器抛出的异常。此时请求已经进入控制器中。

2.类ErrorController方式可以处理所有的异常,包括未进入控制器的错误,比如404,401等错误

3.如果应用中两者共同存在,则@ControllerAdvice方式处理控制器抛出的异常,类ErrorController方式未进入控制器的异常。

4.@ControllerAdvice方式可以定义多个拦截方法,拦截不同的异常类,并且可以获取抛出的异常信息,自由度更大。

感谢各位的阅读,以上就是“SpringBoot处理全局统一异常的方法和区别”的内容了,经过本文的学习后,相信大家对SpringBoot处理全局统一异常的方法和区别这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI