package com.github.wxiaoqi.security.gate.config; import feign.codec.Decoder; import org.springframework.beans.BeansException; import org.springframework.beans.factory.ObjectFactory; import org.springframework.boot.autoconfigure.http.HttpMessageConverters; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.openfeign.support.ResponseEntityDecoder; import org.springframework.cloud.openfeign.support.SpringDecoder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.MediaType; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.web.client.RestTemplate; import org.springframework.web.reactive.function.client.WebClient; import java.util.ArrayList; import java.util.List; /** * @author ace * @create 2019/1/27. */ @Configuration public class GatewayConfig { /** * 负载均衡注解 * @return */ @LoadBalanced @Bean public RestTemplate restTemplate() { return new RestTemplate(); } @Bean public Decoder feignDecoder() { return new ResponseEntityDecoder(new SpringDecoder(feignHttpMessageConverter())); } public ObjectFactory<HttpMessageConverters> feignHttpMessageConverter() { final HttpMessageConverters httpMessageConverters = new HttpMessageConverters(new GateWayMappingJackson2HttpMessageConverter()); return new ObjectFactory<HttpMessageConverters>() { @Override public HttpMessageConverters getObject() throws BeansException { return httpMessageConverters; } }; } public class GateWayMappingJackson2HttpMessageConverter extends MappingJackson2HttpMessageConverter { GateWayMappingJackson2HttpMessageConverter(){ List<MediaType> mediaTypes = new ArrayList<>(); mediaTypes.add(MediaType.valueOf(MediaType.TEXT_HTML_VALUE + ";charset=UTF-8")); setSupportedMediaTypes(mediaTypes); } } @Bean @LoadBalanced public WebClient.Builder loadBalancedWebClientBuilder() { return WebClient.builder(); } }
时间: 2024-03-11 07:45:25 浏览: 433
解决 github项目clone报错 Failed connect to github.com:443; Connection
这是一个 Spring Cloud Gateway 的配置类,其中包含了一些 Bean 的定义,例如负载均衡的 RestTemplate 和 WebClient.Builder。此外,还定义了一个 GateWayMappingJackson2HttpMessageConverter 类,用于处理响应报文的转换。同时,该类还使用了 Feign 的 Decoder 进行响应解码。
阅读全文