没有合适的资源?快使用搜索试试~ 我知道了~
首页详解SpringCloud Finchley Gateway 统一异常处理
详解SpringCloud Finchley Gateway 统一异常处理
1.3k 浏览量
更新于2023-05-27
评论
收藏 106KB PDF 举报
主要介绍了详解SpringCloud Finchley Gateway 统一异常处理,非常具有实用价值,需要的朋友可以参考下
资源详情
资源评论
资源推荐

详解详解SpringCloud Finchley Gateway 统一异常处理统一异常处理
主要介绍了详解SpringCloud Finchley Gateway 统一异常处理,非常具有实用价值,需要的朋友可以参考下
SpringCloud Finchley Gateway 统一异常处理
全文搜索[@@]搜索重点内容标记
1 . 问题:使用SpringCloud Gateway时,会出现各种系统级异常,默认返回HTML.
2 . Finchley版本的Gateway,使用WebFlux形式作为底层框架,而不是Servlet容器,所以常规的异常处理无法使用
翻阅源码,默认是使用DefaultErrorWebExceptionHandler这个类实现结构如下:
可以实现参考DefaultErrorWebExceptionHandler和AbstractErrorWebExceptionHandler自定义实现ErrorWebExceptionHandler,然后,注册为
Bean到Spring容器中即可(Bean Name:"errorWebExceptionHandler")
具体实现代码如下:
package pro.chenggang.example.spring.cloud.gateway.support;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
import org.springframework.cloud.gateway.support.NotFoundException;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.codec.HttpMessageReader;
import org.springframework.http.codec.HttpMessageWriter;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.util.Assert;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.reactive.result.view.ViewResolver;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @classDesc: 统一异常处理,参考{@link org.springframework.web.server.AbstractErrorWebExceptionHandler}修改
* @author: chenggang
* @createTime: 2018/10/30











安全验证
文档复制为VIP权益,开通VIP直接复制

评论0