如何配置 spring-5 “webclient (with jetty connector)” webflux with mediatype "application/x-ndjson"

how to configure spring-5 “webclient (with jetty connector)” webflux with mediatype "application/x-ndjson"

我正在尝试访问将 return 响应媒体类型 "application/x-ndjson" header 的其余端点。如何使用 spring 5 Webclient 使用此端点? 如果我在 Spring 5 WebClient 中将媒体类型设置为 "application/stream+json" 会起作用吗?

您应该能够使用您想要支持的所有媒体类型(包括此特定媒体类型)创建您自己的 Jackson2JsonDecoder

类似于:

Jackson2JsonDecoder jsonDecoder = new Jackson2JsonDecoder(Jackson2ObjectMapperBuilder.json().build(), 
    new MimeType("application", "json"), new MimeType("application", "x-ndjson"));

然后在构建时直接在 WebClient 中配置此解码器:

WebClient webClient = WebClient.builder().codecs(codecs -> codecs.defaultCodecs().jackson2JsonDecoder(jsonDecoder)).build();

如果您使用 Spring Boot,您可以通过以下方式实现所有这些:

@Configuration
public class JsonConfiguration {

  @Bean
  public CodecCustomizer ndJsonCustomizer(ObjectMapper objectMapper) {
    Jackson2JsonDecoder jsonDecoder = new Jackson2JsonDecoder(objectMapper, 
        new MimeType("application", "json"), new MimeType("application", "x-ndjson"));
    return codecs -> codecs.defaultCodecs().jackson2JsonDecoder(jsonDecoder);
  }

}

将来,您可能不需要这样做,因为 Spring 框架团队是 considering ndjson support out-of-the-box to replace stream+json