Spring Cloud Gateway全局异常处理的方法详解
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
Spring Cloud Gateway全局异常处理的方法详解 Spring Cloud Gateway作为Spring Cloud生态系中的网关,旨在为微服务架构提供一种简单而有效的统一的API路由管理方式。然而,在实际应用中,Spring Cloud Gateway中的全局异常处理不能直接用@ControllerAdvice来处理。因此,本文将详细介绍Spring Cloud Gateway全局异常处理的方法。 一、Spring Cloud Gateway简介 Spring Cloud Gateway是Spring官方基于Spring 5.0,Spring Boot 2.0和Project Reactor等技术开发的网关。它旨在为微服务架构提供一种简单而有效的统一的API路由管理方式。Spring Cloud Gateway作为Spring Cloud生态系中的网关,目标是替代Netflix ZUUL,其不仅提供统一的路由方式,并且基于Filter链的方式提供了网关基本的功能,例如:安全,监控/埋点,和限流等。 二、Spring Cloud Gateway的特征 1. 基于Spring Framework 5,Project Reactor和Spring Boot 2.0 2. 动态路由Predicates和Filters 3. 作用于特定路由 4. 集成Hystrix断路器 5. 集成Spring Cloud DiscoveryClient 6. 易于编写的Predicates和Filters 7. 限流路径重写 三、Spring Cloud Gateway中的全局异常处理 在Spring Cloud Gateway中,不能直接用@ControllerAdvice来处理全局异常。通过跟踪异常信息的抛出,找到对应的源码,自定义一些处理逻辑来符合业务的需求。网关都是给接口做代理转发的,后端对应的都是REST API,返回数据格式都是JSON。如果不做处理,当发生异常时,Gateway默认给出的错误信息是页面,不方便前端进行异常处理。因此,需要对异常信息进行处理,返回JSON格式的数据给客户端。 四、自定义异常处理逻辑 下面是一个自定义异常处理逻辑的示例代码: ```java package com.cxytiandi.gateway.exception; import java.util.HashMap; import java.util.Map; import org.springframework.boot.autoconfigure.web.ErrorProperties; import org.springframework.boot.autoconfigure.web.ResourceProperties; import org.springframework.boot.autoconfigure.web.reactive.error.DefaultErrorWebExceptionHandler; import org.springframework.context.ApplicationContext; import org.springframework.http.HttpStatus; import org.springframework.web.reactive.function.server.RequestPredicates; import org.springframework.web.reactive.function.server.RouterFunction; import org.springframework.web.reactive.function.server.RouterFunctions; import org.springframework.web.reactive.function.server.ServerRequest; import org.springframework.web.reactive.function.server.ServerResponse; / * 自定义异常处理 * * <p>异常时用JSON代替HTML异常信息<p> * * @author yinjihuan */ public class JsonExceptionHandler extends DefaultErrorWebExceptionHandler { public JsonExceptionHandler(ErrorAttributes errorAttributes, ResourceProperties resourceProperties, ErrorProperties errorProperties, ApplicationContext applicationContext) { super(errorAttributes, resourceProperties, errorProperties, applicationContext); } // ... } ``` 五、结论 本文详细介绍了Spring Cloud Gateway全局异常处理的方法,包括Spring Cloud Gateway的简介、特征、全局异常处理和自定义异常处理逻辑等。通过自定义异常处理逻辑,可以让Gateway返回JSON格式的数据给客户端,方便前端进行异常处理。
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
- 粉丝: 3
- 资源: 924
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)