欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Cloud Gateway全局異常處理的方法詳解

 更新時(shí)間:2018年10月11日 11:21:26   作者:猿天地  
這篇文章主要給大家介紹了關(guān)于Spring Cloud Gateway全局異常處理的相關(guān)資料,需要的朋友可以參考下

前言

Spring Cloud Gateway是Spring官方基于Spring 5.0,Spring Boot 2.0和Project Reactor等技術(shù)開發(fā)的網(wǎng)關(guān),Spring Cloud Gateway旨在為微服務(wù)架構(gòu)提供一種簡(jiǎn)單而有效的統(tǒng)一的API路由管理方式。Spring Cloud Gateway作為Spring Cloud生態(tài)系中的網(wǎng)關(guān),目標(biāo)是替代Netflix ZUUL,其不僅提供統(tǒng)一的路由方式,并且基于Filter鏈的方式提供了網(wǎng)關(guān)基本的功能,例如:安全,監(jiān)控/埋點(diǎn),和限流等。

Spring Cloud Gateway 的特征:

  • 基于 Spring Framework 5,Project Reactor 和 Spring Boot 2.0動(dòng)態(tài)路由
  • Predicates 和 Filters 作用于特定路由
  • 集成 Hystrix 斷路器
  • 集成 Spring Cloud DiscoveryClient
  • 易于編寫的 Predicates 和 Filters
  • 限流
  • 路徑重寫

Spring Cloud Gateway中的全局異常處理不能直接用@ControllerAdvice來(lái)處理,通過(guò)跟蹤異常信息的拋出,找到對(duì)應(yīng)的源碼,自定義一些處理邏輯來(lái)符合業(yè)務(wù)的需求。

網(wǎng)關(guān)都是給接口做代理轉(zhuǎn)發(fā)的,后端對(duì)應(yīng)的都是REST API,返回?cái)?shù)據(jù)格式都是JSON。如果不做處理,當(dāng)發(fā)生異常時(shí),Gateway默認(rèn)給出的錯(cuò)誤信息是頁(yè)面,不方便前端進(jìn)行異常處理。

需要對(duì)異常信息進(jìn)行處理,返回JSON格式的數(shù)據(jù)給客戶端。下面先看實(shí)現(xiàn)的代碼,后面再跟大家講下需要注意的地方。
自定義異常處理邏輯:

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.boot.web.reactive.error.ErrorAttributes;
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>異常時(shí)用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);
 }

 /**
 * 獲取異常屬性
 */
 @Override
 protected Map<String, Object> getErrorAttributes(ServerRequest request, boolean includeStackTrace) {
 int code = 500;
 Throwable error = super.getError(request);
 if (error instanceof org.springframework.cloud.gateway.support.NotFoundException) {
 code = 404;
 }
 return response(code, this.buildMessage(request, error));
 }

 /**
 * 指定響應(yīng)處理方法為JSON處理的方法
 * @param errorAttributes
 */
 @Override
 protected RouterFunction<ServerResponse> getRoutingFunction(ErrorAttributes errorAttributes) {
 return RouterFunctions.route(RequestPredicates.all(), this::renderErrorResponse);
 }

 /**
 * 根據(jù)code獲取對(duì)應(yīng)的HttpStatus
 * @param errorAttributes
 */
 @Override
 protected HttpStatus getHttpStatus(Map<String, Object> errorAttributes) {
 int statusCode = (int) errorAttributes.get("code");
 return HttpStatus.valueOf(statusCode);
 }

 /**
 * 構(gòu)建異常信息
 * @param request
 * @param ex
 * @return
 */
 private String buildMessage(ServerRequest request, Throwable ex) {
 StringBuilder message = new StringBuilder("Failed to handle request [");
 message.append(request.methodName());
 message.append(" ");
 message.append(request.uri());
 message.append("]");
 if (ex != null) {
 message.append(": ");
 message.append(ex.getMessage());
 }
 return message.toString();
 }

 /**
 * 構(gòu)建返回的JSON數(shù)據(jù)格式
 * @param status 狀態(tài)碼
 * @param errorMessage 異常信息
 * @return
 */
 public static Map<String, Object> response(int status, String errorMessage) {
 Map<String, Object> map = new HashMap<>();
 map.put("code", status);
 map.put("message", errorMessage);
 map.put("data", null);
 return map;
 }

}

覆蓋默認(rèn)的配置:

package com.cxytiandi.gateway.exception;

import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.web.ResourceProperties;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.reactive.error.ErrorAttributes;
import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.web.reactive.result.view.ViewResolver;

/**
 * 覆蓋默認(rèn)的異常處理
 * 
 * @author yinjihuan
 *
 */
@Configuration
@EnableConfigurationProperties({ServerProperties.class, ResourceProperties.class})
public class ErrorHandlerConfiguration {

 private final ServerProperties serverProperties;

 private final ApplicationContext applicationContext;

 private final ResourceProperties resourceProperties;

 private final List<ViewResolver> viewResolvers;

 private final ServerCodecConfigurer serverCodecConfigurer;

 public ErrorHandlerConfiguration(ServerProperties serverProperties,
          ResourceProperties resourceProperties,
          ObjectProvider<List<ViewResolver>> viewResolversProvider,
          ServerCodecConfigurer serverCodecConfigurer,
          ApplicationContext applicationContext) {
  this.serverProperties = serverProperties;
  this.applicationContext = applicationContext;
  this.resourceProperties = resourceProperties;
  this.viewResolvers = viewResolversProvider.getIfAvailable(Collections::emptyList);
  this.serverCodecConfigurer = serverCodecConfigurer;
 }

 @Bean
 @Order(Ordered.HIGHEST_PRECEDENCE)
 public ErrorWebExceptionHandler errorWebExceptionHandler(ErrorAttributes errorAttributes) {
  JsonExceptionHandler exceptionHandler = new JsonExceptionHandler(
    errorAttributes, 
    this.resourceProperties,
    this.serverProperties.getError(), 
    this.applicationContext);
  exceptionHandler.setViewResolvers(this.viewResolvers);
  exceptionHandler.setMessageWriters(this.serverCodecConfigurer.getWriters());
  exceptionHandler.setMessageReaders(this.serverCodecConfigurer.getReaders());
  return exceptionHandler;
 } 
}

注意點(diǎn)

異常時(shí)如何返回JSON而不是HTML?

在org.springframework.boot.autoconfigure.web.reactive.error.DefaultErrorWebExceptionHandler中的getRoutingFunction()方法就是控制返回格式的,原代碼如下:

@Override
protected RouterFunction<ServerResponse> getRoutingFunction(
 ErrorAttributes errorAttributes) {
  return RouterFunctions.route(acceptsTextHtml(), this::renderErrorView)
 .andRoute(RequestPredicates.all(), this::renderErrorResponse);
}

這邊優(yōu)先是用HTML來(lái)顯示的,想用JSON的改下就可以了,如下:

protected RouterFunction<ServerResponse> getRoutingFunction(ErrorAttributes errorAttributes) {
 return RouterFunctions.route(RequestPredicates.all(), this::renderErrorResponse);
}

getHttpStatus需要重寫

原始的方法是通過(guò)status來(lái)獲取對(duì)應(yīng)的HttpStatus的,代碼如下:

protected HttpStatus getHttpStatus(Map<String, Object> errorAttributes) {
 int statusCode = (int) errorAttributes.get("status");
 return HttpStatus.valueOf(statusCode);
}

如果我們定義的格式中沒(méi)有status字段的話,這么就會(huì)報(bào)錯(cuò),找不到對(duì)應(yīng)的響應(yīng)碼,要么返回?cái)?shù)據(jù)格式中增加status子段,要么重寫,我這邊返回的是code,所以要重寫,代碼如下:

@Override
protected HttpStatus getHttpStatus(Map<String, Object> errorAttributes) {
 int statusCode = (int) errorAttributes.get("code");
 return HttpStatus.valueOf(statusCode);
}

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

  • Spring注解與P/C命名空間超詳細(xì)解析

    Spring注解與P/C命名空間超詳細(xì)解析

    Spring注解方式減少了配置文件內(nèi)容,更加便于管理,并且使用注解可以大大提高了開發(fā)效率!注解本身是沒(méi)有功能的,和xml一樣,注解和xml都是一種元數(shù)據(jù),元數(shù)據(jù)即解釋數(shù)據(jù)的數(shù)據(jù),也就是所謂的配置
    2022-11-11
  • Java的MyBatis框架中MyBatis Generator代碼生成器的用法

    Java的MyBatis框架中MyBatis Generator代碼生成器的用法

    這篇文章主要介紹了Java的MyBatis框架中Mybatis Generator代碼生成器的用法,Mybatis Generator主要被用來(lái)生成繁瑣的配置文件來(lái)提高效率,需要的朋友可以參考下
    2016-04-04
  • Java 對(duì)稱加密幾種算法分別實(shí)現(xiàn)

    Java 對(duì)稱加密幾種算法分別實(shí)現(xiàn)

    這篇文章主要介紹了Java 對(duì)稱加密使用DES / 3DES / AES 這三種算法分別實(shí)現(xiàn)的相關(guān)資料,這里提供了實(shí)例代碼,需要的朋友可以參考下
    2017-01-01
  • 關(guān)于spring web-mvc衍生注解

    關(guān)于spring web-mvc衍生注解

    這篇文章主要介紹了關(guān)于spring web-mvc衍生注解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • Idea中如何查看SpringSecurity各Filter信息

    Idea中如何查看SpringSecurity各Filter信息

    這篇文章主要介紹了Idea中如何查看SpringSecurity各Filter信息,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • MyBatis創(chuàng)建存儲(chǔ)過(guò)程的實(shí)例代碼_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    MyBatis創(chuàng)建存儲(chǔ)過(guò)程的實(shí)例代碼_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    本節(jié)需要用到的有2部分,第一部分是如何在Derby中創(chuàng)建存儲(chǔ)過(guò)程,第二部分是如何在Mybatis中調(diào)用存儲(chǔ)過(guò)程,具體實(shí)例代碼大家參考下本文吧
    2017-09-09
  • Java 詳解包裝類Integer與int有哪些共通和不同

    Java 詳解包裝類Integer與int有哪些共通和不同

    這篇文章主要介紹的是 Java中int和Integer的區(qū)別,Java 是一種強(qiáng)數(shù)據(jù)類型的語(yǔ)言,因此所有的屬性必須有一個(gè)數(shù)據(jù)類型,下面文章基于Java詳細(xì)int和Integer有何區(qū)別,需要的朋友可以參考一下
    2022-04-04
  • Java多線程中sleep和wait區(qū)別

    Java多線程中sleep和wait區(qū)別

    本文主要介紹了Java多線程中sleep和wait區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • Java和C#下的參數(shù)驗(yàn)證方法

    Java和C#下的參數(shù)驗(yàn)證方法

    下面小編就為大家?guī)?lái)一篇Java和C#下的參數(shù)驗(yàn)證實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-09-09
  • Redis介紹和使用場(chǎng)景詳解

    Redis介紹和使用場(chǎng)景詳解

    這篇文章主要為大家詳細(xì)介紹了Redis介紹和使用場(chǎng)景,需要的朋友可以參考,具體內(nèi)容如下
    2018-04-04

最新評(píng)論