使用spring cloud gateway时 connect- type application/xml 会被转换成application/json
时间: 2023-09-03 15:09:18 浏览: 37
这个问题可能与 Spring Cloud Gateway 中的默认编码器有关。默认情况下,Spring Cloud Gateway 使用 `NettyCodecHint` 进行编码和解码,它将所有内容类型都转换为 JSON 格式。如果你想保留原始的内容类型,可以在配置中禁用该编码器,使用另一个编码器替换它。
以下是一个示例配置,演示如何禁用 `NettyCodecHint` 编码器并使用 `ServerCodecConfigurer` 进行自定义编码器配置:
```java
@Configuration
public class GatewayConfig {
@Bean
public ServerCodecConfigurer serverCodecConfigurer() {
return ServerCodecConfigurer.create();
}
@Bean
public GatewayFilter removeNettyCodecHintFilter(ServerCodecConfigurer codecConfigurer) {
return (exchange, chain) -> {
exchange.getAttributes().put(ServerWebExchangeUtils.CLIENT_RESPONSE_CODEC_ATTR, codecConfigurer.getReaders());
return chain.filter(exchange);
};
}
}
```
这里创建了一个 `ServerCodecConfigurer` 实例,并在 `GatewayFilter` 中使用它来替换默认的编码器。这样就可以避免 Spring Cloud Gateway 将内容类型转换为 JSON,而保留原始的内容类型。