经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring Boot » 查看文章
Spring Cloud Gateway全局异常处理的方法详解
来源:jb51  时间:2018/10/12 9:22:40  对本文有异议

前言

Spring Cloud Gateway是Spring官方基于Spring 5.0,Spring Boot 2.0和Project Reactor等技术开发的网关,Spring Cloud Gateway旨在为微服务架构提供一种简单而有效的统一的API路由管理方式。Spring Cloud Gateway作为Spring Cloud生态系中的网关,目标是替代Netflix ZUUL,其不仅提供统一的路由方式,并且基于Filter链的方式提供了网关基本的功能,例如:安全,监控/埋点,和限流等。

Spring Cloud Gateway 的特征:

  • 基于 Spring Framework 5,Project Reactor 和 Spring Boot 2.0动态路由

  • Predicates 和 Filters 作用于特定路由

  • 集成 Hystrix 断路器

  • 集成 Spring Cloud DiscoveryClient

  • 易于编写的 Predicates 和 Filters

  • 限流

  • 路径重写

Spring Cloud Gateway中的全局异常处理不能直接用@ControllerAdvice来处理,通过跟踪异常信息的抛出,找到对应的源码,自定义一些处理逻辑来符合业务的需求。

网关都是给接口做代理转发的,后端对应的都是REST API,返回数据格式都是JSON。如果不做处理,当发生异常时,Gateway默认给出的错误信息是页面,不方便前端进行异常处理。

需要对异常信息进行处理,返回JSON格式的数据给客户端。下面先看实现的代码,后面再跟大家讲下需要注意的地方。
自定义异常处理逻辑:

  1. package com.cxytiandi.gateway.exception;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. import org.springframework.boot.autoconfigure.web.ErrorProperties;
  7. import org.springframework.boot.autoconfigure.web.ResourceProperties;
  8. import org.springframework.boot.autoconfigure.web.reactive.error.DefaultErrorWebExceptionHandler;
  9. import org.springframework.boot.web.reactive.error.ErrorAttributes;
  10. import org.springframework.context.ApplicationContext;
  11. import org.springframework.http.HttpStatus;
  12. import org.springframework.web.reactive.function.server.RequestPredicates;
  13. import org.springframework.web.reactive.function.server.RouterFunction;
  14. import org.springframework.web.reactive.function.server.RouterFunctions;
  15. import org.springframework.web.reactive.function.server.ServerRequest;
  16. import org.springframework.web.reactive.function.server.ServerResponse;
  17.  
  18. /**
  19.  * 自定义异常处理
  20.  * 
  21.  * <p>异常时用JSON代替HTML异常信息<p>
  22.  * 
  23.  * @author yinjihuan
  24.  *
  25.  */
  26. public class JsonExceptionHandler extends DefaultErrorWebExceptionHandler {
  27.  
  28.  public JsonExceptionHandler(ErrorAttributes errorAttributes, ResourceProperties resourceProperties,
  29.  ErrorProperties errorProperties, ApplicationContext applicationContext) {
  30.  super(errorAttributes, resourceProperties, errorProperties, applicationContext);
  31.  }
  32.  
  33.  /**
  34.  * 获取异常属性
  35.  */
  36.  @Override
  37.  protected Map<String, Object> getErrorAttributes(ServerRequest request, boolean includeStackTrace) {
  38.  int code = 500;
  39.  Throwable error = super.getError(request);
  40.  if (error instanceof org.springframework.cloud.gateway.support.NotFoundException) {
  41.  code = 404;
  42.  }
  43.  return response(code, this.buildMessage(request, error));
  44.  }
  45.  
  46.  /**
  47.  * 指定响应处理方法为JSON处理的方法
  48.  * @param errorAttributes
  49.  */
  50.  @Override
  51.  protected RouterFunction<ServerResponse> getRoutingFunction(ErrorAttributes errorAttributes) {
  52.  return RouterFunctions.route(RequestPredicates.all(), this::renderErrorResponse);
  53.  }
  54.  
  55.  /**
  56.  * 根据code获取对应的HttpStatus
  57.  * @param errorAttributes
  58.  */
  59.  @Override
  60.  protected HttpStatus getHttpStatus(Map<String, Object> errorAttributes) {
  61.  int statusCode = (int) errorAttributes.get("code");
  62.  return HttpStatus.valueOf(statusCode);
  63.  }
  64.  
  65.  /**
  66.  * 构建异常信息
  67.  * @param request
  68.  * @param ex
  69.  * @return
  70.  */
  71.  private String buildMessage(ServerRequest request, Throwable ex) {
  72.  StringBuilder message = new StringBuilder("Failed to handle request [");
  73.  message.append(request.methodName());
  74.  message.append(" ");
  75.  message.append(request.uri());
  76.  message.append("]");
  77.  if (ex != null) {
  78.  message.append(": ");
  79.  message.append(ex.getMessage());
  80.  }
  81.  return message.toString();
  82.  }
  83.  
  84.  /**
  85.  * 构建返回的JSON数据格式
  86.  * @param status 状态码
  87.  * @param errorMessage 异常信息
  88.  * @return
  89.  */
  90.  public static Map<String, Object> response(int status, String errorMessage) {
  91.  Map<String, Object> map = new HashMap<>();
  92.  map.put("code", status);
  93.  map.put("message", errorMessage);
  94.  map.put("data", null);
  95.  return map;
  96.  }
  97.  
  98. }

覆盖默认的配置:

  1. package com.cxytiandi.gateway.exception;
  2.  
  3. import java.util.Collections;
  4. import java.util.List;
  5.  
  6. import org.springframework.beans.factory.ObjectProvider;
  7. import org.springframework.boot.autoconfigure.web.ResourceProperties;
  8. import org.springframework.boot.autoconfigure.web.ServerProperties;
  9. import org.springframework.boot.context.properties.EnableConfigurationProperties;
  10. import org.springframework.boot.web.reactive.error.ErrorAttributes;
  11. import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
  12. import org.springframework.context.ApplicationContext;
  13. import org.springframework.context.annotation.Bean;
  14. import org.springframework.context.annotation.Configuration;
  15. import org.springframework.core.Ordered;
  16. import org.springframework.core.annotation.Order;
  17. import org.springframework.http.codec.ServerCodecConfigurer;
  18. import org.springframework.web.reactive.result.view.ViewResolver;
  19.  
  20. /**
  21.  * 覆盖默认的异常处理
  22.  * 
  23.  * @author yinjihuan
  24.  *
  25.  */
  26. @Configuration
  27. @EnableConfigurationProperties({ServerProperties.class, ResourceProperties.class})
  28. public class ErrorHandlerConfiguration {
  29.  
  30.  private final ServerProperties serverProperties;
  31.  
  32.  private final ApplicationContext applicationContext;
  33.  
  34.  private final ResourceProperties resourceProperties;
  35.  
  36.  private final List<ViewResolver> viewResolvers;
  37.  
  38.  private final ServerCodecConfigurer serverCodecConfigurer;
  39.  
  40.  public ErrorHandlerConfiguration(ServerProperties serverProperties,
  41.           ResourceProperties resourceProperties,
  42.           ObjectProvider<List<ViewResolver>> viewResolversProvider,
  43.           ServerCodecConfigurer serverCodecConfigurer,
  44.           ApplicationContext applicationContext) {
  45.   this.serverProperties = serverProperties;
  46.   this.applicationContext = applicationContext;
  47.   this.resourceProperties = resourceProperties;
  48.   this.viewResolvers = viewResolversProvider.getIfAvailable(Collections::emptyList);
  49.   this.serverCodecConfigurer = serverCodecConfigurer;
  50.  }
  51.  
  52.  @Bean
  53.  @Order(Ordered.HIGHEST_PRECEDENCE)
  54.  public ErrorWebExceptionHandler errorWebExceptionHandler(ErrorAttributes errorAttributes) {
  55.   JsonExceptionHandler exceptionHandler = new JsonExceptionHandler(
  56.     errorAttributes, 
  57.     this.resourceProperties,
  58.     this.serverProperties.getError(), 
  59.     this.applicationContext);
  60.   exceptionHandler.setViewResolvers(this.viewResolvers);
  61.   exceptionHandler.setMessageWriters(this.serverCodecConfigurer.getWriters());
  62.   exceptionHandler.setMessageReaders(this.serverCodecConfigurer.getReaders());
  63.   return exceptionHandler;
  64.  } 
  65. }

注意点

异常时如何返回JSON而不是HTML?

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

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

这边优先是用HTML来显示的,想用JSON的改下就可以了,如下:

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

getHttpStatus需要重写

原始的方法是通过status来获取对应的HttpStatus的,代码如下:

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

如果我们定义的格式中没有status字段的话,这么就会报错,找不到对应的响应码,要么返回数据格式中增加status子段,要么重写,我这边返回的是code,所以要重写,代码如下:

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

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对w3xue的支持。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号