亚洲免费在线视频-亚洲啊v-久久免费精品视频-国产精品va-看片地址-成人在线视频网

您的位置:首頁技術文章
文章詳情頁

Spring Cloud Gateway自定義異常處理Exception Handler的方法小結

瀏覽:52日期:2023-06-26 14:06:42

版本: Spring Cloud 2020.0.3

常見的方法有 實現自己的 DefaultErrorWebExceptionHandler 或 僅實現ErrorAttributes.

方法1: ErrorWebExceptionHandler (僅供示意)

自定義一個 GlobalErrorAttributes:

@Componentpublic class GlobalErrorAttributes extends DefaultErrorAttributes{ @Override public Map<String, Object> getErrorAttributes(ServerRequest request, ErrorAttributeOptions options) {Throwable error = super.getError(request);Map<String, Object> map = super.getErrorAttributes(request, options);map.put('status', HttpStatus.BAD_REQUEST.value());map.put('message', error.getMessage());return map; }}

實現一個

@Component@Order(-2)public class GlobalErrorWebExceptionHandler extends AbstractErrorWebExceptionHandler { public GlobalErrorWebExceptionHandler(GlobalErrorAttributes gea, ApplicationContext applicationContext, ServerCodecConfigurer serverCodecConfigurer) {super(gea, new WebProperties.Resources(), applicationContext);super.setMessageWriters(serverCodecConfigurer.getWriters());super.setMessageReaders(serverCodecConfigurer.getReaders()); } //渲染html或json @Override protected RouterFunction<ServerResponse> getRoutingFunction(final ErrorAttributes errorAttributes) {return RouterFunctions.route(RequestPredicates.all(), this::renderErrorResponse); } private Mono<ServerResponse> renderErrorResponse(final ServerRequest request) {final Map<String, Object> errorPropertiesMap = getErrorAttributes(request, ErrorAttributeOptions.defaults());return ServerResponse.status(HttpStatus.BAD_REQUEST).contentType(MediaType.APPLICATION_JSON).body(BodyInserters.fromValue(errorPropertiesMap)); }}方法2, 僅實現一個 ErrorAttributes, 以覆蓋默認的 DefaultErrorAttributes

//Spring 默認的就很好了.@Componentpublic class GatewayErrorAttributes extends DefaultErrorAttributes { private static final Logger logger = LoggerFactory.getLogger(GatewayErrorAttributes.class); @Override public Map<String, Object> getErrorAttributes(ServerRequest request, ErrorAttributeOptions options) {Throwable error = super.getError(request);Map<String, Object> errorAttributes = new HashMap<>(8);errorAttributes.put('message', error.getMessage());errorAttributes.put('method', request.methodName());errorAttributes.put('path', request.path());MergedAnnotation<ResponseStatus> responseStatusAnnotation = MergedAnnotations.from(error.getClass(), MergedAnnotations.SearchStrategy.TYPE_HIERARCHY).get(ResponseStatus.class);HttpStatus errorStatus = determineHttpStatus(error, responseStatusAnnotation);//必須設置, 否則會報錯, 因為 DefaultErrorWebExceptionHandler 的 renderErrorResponse 方法會獲取此屬性, 重新實現 DefaultErrorWebExceptionHandler也可.errorAttributes.put('status', errorStatus.value());errorAttributes.put('code', errorStatus.value());//html view用errorAttributes.put('timestamp', new Date());//html view 用errorAttributes.put('requestId', request.exchange().getRequest().getId());errorAttributes.put('error', errorStatus.getReasonPhrase());errorAttributes.put('exception', error.getClass().getName());return errorAttributes; } //從DefaultErrorWebExceptionHandler中復制過來的 private HttpStatus determineHttpStatus(Throwable error, MergedAnnotation<ResponseStatus> responseStatusAnnotation) {if (error instanceof ResponseStatusException) { return ((ResponseStatusException) error).getStatus();}return responseStatusAnnotation.getValue('code', HttpStatus.class).orElse(HttpStatus.INTERNAL_SERVER_ERROR); } }

這樣就可以了.

注意注意: 必須設置 errorAttributes.put('status', errorStatus.value()) , 否則會報錯, 因為 DefaultErrorWebExceptionHandler 的 renderErrorResponse 方法會獲取此屬性. 除非你自己像方法一一樣重新實現 DefaultErrorWebExceptionHandler.

然后在網關中訪問一個不存在的服務, 即可看到效果.

curl ’http://127.0.0.1:8900/fundmain22/abc/gogogo?id=1000’ --header ’Accept: application/json’{'exception':'org.springframework.web.server.ResponseStatusException','path':'/fundmain22/abc/gogogo','code':404,'method':'GET','requestId':'094e53e5-1','message':'404 NOT_FOUND','error':'Not Found','status':404,'timestamp':'2021-08-09T11:07:44.106+0000'}

感謝網絡上的各種文章...

到此這篇關于Spring Cloud Gateway自定義異常處理Exception Handler的文章就介紹到這了,更多相關Spring Cloud Gateway自定義異常處理內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
主站蜘蛛池模板: 欧美一区二区三区在线播放 | 精品国产日韩久久亚洲 | 国产99久久精品 | 亚洲综合国产 | 日韩在线高清视频 | 精品国产一区二区三区不卡 | 中文字幕在线观看亚洲日韩 | 无码精品一区二区三区免费视频 | 午夜成年人网站 | 伊人色在线观看 | 成年黄色网址 | 精品中文字幕一区在线 | 久久久久久久久久久大尺度免费视频 | 成年人视频免费网站 | 久久久久国产视频 | 伊人久色| 久久久一本精品99久久精品66 | 色偷偷亚洲第一成人综合网址 | 91在线免费观看网站 | 一级毛片成人免费看免费不卡 | 国产亚洲精品一区二区久久 | 男女免费观看在线爽爽爽视频 | 久久久久久亚洲精品不卡 | 久国产精品视频 | 日韩欧美国产精品第一页不卡 | 欧洲免费无线码二区5 | 久久欧美精品欧美久久欧美 | 色播亚洲视频在线观看 | 欧美午夜免费毛片a级 | 色爽爽爽爽爽爽爽爽 | 精品9e精品视频在线观看 | 欧美日韩一区二区中文字幕视频 | 亚州一二区| 男人又粗又硬桶女人免费 | 亚洲第一区视频在线观看 | 欧美日本韩国一区 | 1769视频在线观看国产 | 欧美激情成人网 | 日韩国产在线 | 欧美一级高清毛片aaa | 岛国在线免费观看 |