pro.chenggang.plugin.springcloud.gateway.config.GlobalExceptionJsonHandlerConfig Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spring-cloud-gateway-plugin Show documentation
Show all versions of spring-cloud-gateway-plugin Show documentation
Spring Cloud Gateway Plugin Project
package pro.chenggang.plugin.springcloud.gateway.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
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.context.annotation.Primary;
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;
import pro.chenggang.plugin.springcloud.gateway.properties.GatewayPluginProperties;
import pro.chenggang.plugin.springcloud.gateway.response.JsonExceptionHandler;
import pro.chenggang.plugin.springcloud.gateway.response.factory.DefaultExceptionHandlerStrategyFactory;
import pro.chenggang.plugin.springcloud.gateway.response.factory.ExceptionHandlerStrategyFactory;
import pro.chenggang.plugin.springcloud.gateway.response.strategy.ExceptionHandlerStrategy;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
* Gateway Plugin Config
* @author chenggang
* @date 2019/01/29
*/
@Slf4j
@Configuration
@ConditionalOnProperty(prefix = GatewayPluginProperties.GATEWAY_PLUGIN_PROPERTIES_PREFIX,value = "exception-json-handler",havingValue = "true")
public class GlobalExceptionJsonHandlerConfig {
/**
* ExceptionHandlerStrategyFactory
* @param applicationContext
* @return
*/
@Bean
@ConditionalOnMissingBean(ExceptionHandlerStrategyFactory.class)
public ExceptionHandlerStrategyFactory exceptionHandlerStrategyFactory(ApplicationContext applicationContext){
DefaultExceptionHandlerStrategyFactory factory = new DefaultExceptionHandlerStrategyFactory();
Map exceptionHandlerStrategyMap = applicationContext.getBeansOfType(ExceptionHandlerStrategy.class);
if(null != exceptionHandlerStrategyMap && !exceptionHandlerStrategyMap.isEmpty()){
exceptionHandlerStrategyMap.forEach((k,v)->factory.addStrategy(v));
}
log.debug("Load ExceptionHandler Strategy Factory Config Bean");
return factory;
}
/**
* ErrorWebExceptionHandler
* @param viewResolversProvider
* @param serverCodecConfigurer
* @param exceptionHandlerExceptionHandlerStrategyFactory
* @return
*/
@Primary
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public ErrorWebExceptionHandler errorWebExceptionHandler(ObjectProvider> viewResolversProvider,
ServerCodecConfigurer serverCodecConfigurer,
ExceptionHandlerStrategyFactory exceptionHandlerExceptionHandlerStrategyFactory) {
JsonExceptionHandler jsonExceptionHandler = new JsonExceptionHandler(exceptionHandlerExceptionHandlerStrategyFactory);
jsonExceptionHandler.setViewResolvers(viewResolversProvider.getIfAvailable(Collections::emptyList));
jsonExceptionHandler.setMessageWriters(serverCodecConfigurer.getWriters());
jsonExceptionHandler.setMessageReaders(serverCodecConfigurer.getReaders());
log.debug("Load Json Exception Handler Config Bean");
return jsonExceptionHandler;
}
}