CustomErrorWebFluxAutoConfiguration.java 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package com.fdkankan.gateway.config;
  2. import com.fdkankan.gateway.exception.JsonErrorWebExceptionHandler;
  3. import org.springframework.beans.factory.ObjectProvider;
  4. import org.springframework.boot.autoconfigure.AutoConfigureBefore;
  5. import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
  6. import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
  7. import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
  8. import org.springframework.boot.autoconfigure.condition.SearchStrategy;
  9. import org.springframework.boot.autoconfigure.web.ResourceProperties;
  10. import org.springframework.boot.autoconfigure.web.ServerProperties;
  11. import org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration;
  12. import org.springframework.boot.context.properties.EnableConfigurationProperties;
  13. import org.springframework.boot.web.reactive.error.DefaultErrorAttributes;
  14. import org.springframework.boot.web.reactive.error.ErrorAttributes;
  15. import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
  16. import org.springframework.context.ApplicationContext;
  17. import org.springframework.context.annotation.Bean;
  18. import org.springframework.context.annotation.Configuration;
  19. import org.springframework.core.annotation.Order;
  20. import org.springframework.http.codec.ServerCodecConfigurer;
  21. import org.springframework.web.reactive.config.WebFluxConfigurer;
  22. import org.springframework.web.reactive.result.view.ViewResolver;
  23. import java.util.List;
  24. import java.util.stream.Collectors;
  25. /**
  26. * 网关全局异常配置,用于处理网关自身抛出的异常
  27. */
  28. @Configuration
  29. @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
  30. @ConditionalOnClass(WebFluxConfigurer.class)
  31. @AutoConfigureBefore(WebFluxAutoConfiguration.class)
  32. @EnableConfigurationProperties({ServerProperties.class, ResourceProperties.class})
  33. public class CustomErrorWebFluxAutoConfiguration {
  34. private final ServerProperties serverProperties;
  35. private final ApplicationContext applicationContext;
  36. private final ResourceProperties resourceProperties;
  37. private final List<ViewResolver> viewResolvers;
  38. private final ServerCodecConfigurer serverCodecConfigurer;
  39. public CustomErrorWebFluxAutoConfiguration(ServerProperties serverProperties,
  40. ResourceProperties resourceProperties,
  41. ObjectProvider<ViewResolver> viewResolversProvider,
  42. ServerCodecConfigurer serverCodecConfigurer,
  43. ApplicationContext applicationContext) {
  44. this.serverProperties = serverProperties;
  45. this.applicationContext = applicationContext;
  46. this.resourceProperties = resourceProperties;
  47. this.viewResolvers = viewResolversProvider.orderedStream()
  48. .collect(Collectors.toList());
  49. this.serverCodecConfigurer = serverCodecConfigurer;
  50. }
  51. @Bean
  52. @ConditionalOnMissingBean(value = ErrorWebExceptionHandler.class,
  53. search = SearchStrategy.CURRENT)
  54. @Order(-1)
  55. public ErrorWebExceptionHandler errorWebExceptionHandler(ErrorAttributes errorAttributes) {
  56. // TODO 这里完成自定义ErrorWebExceptionHandler实现逻辑
  57. JsonErrorWebExceptionHandler exceptionHandler = new JsonErrorWebExceptionHandler(
  58. errorAttributes,
  59. resourceProperties,
  60. this.serverProperties.getError(),
  61. applicationContext);
  62. exceptionHandler.setViewResolvers(this.viewResolvers);
  63. exceptionHandler.setMessageWriters(this.serverCodecConfigurer.getWriters());
  64. exceptionHandler.setMessageReaders(this.serverCodecConfigurer.getReaders());
  65. return exceptionHandler;
  66. }
  67. @Bean
  68. @ConditionalOnMissingBean(value = ErrorAttributes.class, search = SearchStrategy.CURRENT)
  69. public DefaultErrorAttributes errorAttributes() {
  70. return new DefaultErrorAttributes(this.serverProperties.getError().isIncludeException());
  71. }
  72. }