Search in sources :

Example 1 with WebClientCustomizer

use of org.springframework.boot.web.reactive.function.client.WebClientCustomizer in project apollo by ctripcorp.

the class ApolloClientLongPollingExtensionInitializer method initialize.

@Override
public void initialize(ApolloClientProperties apolloClientProperties, Binder binder, BindHandler bindHandler) {
    WebClient.Builder webClientBuilder = WebClient.builder();
    List<ApolloClientWebClientCustomizerFactory> factories = ServiceBootstrap.loadAllOrdered(ApolloClientWebClientCustomizerFactory.class);
    if (!CollectionUtils.isEmpty(factories)) {
        for (ApolloClientWebClientCustomizerFactory factory : factories) {
            WebClientCustomizer webClientCustomizer = factory.createWebClientCustomizer(apolloClientProperties, binder, bindHandler, this.log, this.bootstrapContext);
            if (webClientCustomizer != null) {
                webClientCustomizer.customize(webClientBuilder);
            }
        }
    }
    HttpClient httpClient = new ApolloWebClientHttpClient(webClientBuilder.build());
    ApolloConfigDataInjectorCustomizer.registerIfAbsent(HttpClient.class, () -> httpClient);
}
Also used : WebClientCustomizer(org.springframework.boot.web.reactive.function.client.WebClientCustomizer) ApolloClientWebClientCustomizerFactory(com.ctrip.framework.apollo.config.data.extension.webclient.customizer.spi.ApolloClientWebClientCustomizerFactory) HttpClient(com.ctrip.framework.apollo.util.http.HttpClient) WebClient(org.springframework.web.reactive.function.client.WebClient)

Example 2 with WebClientCustomizer

use of org.springframework.boot.web.reactive.function.client.WebClientCustomizer in project spring-boot by spring-projects.

the class ClientHttpConnectorAutoConfigurationTests method shouldCreateHttpClientBeans.

@Test
void shouldCreateHttpClientBeans() {
    this.contextRunner.run((context) -> {
        assertThat(context).hasSingleBean(ReactorResourceFactory.class);
        assertThat(context).hasSingleBean(ReactorClientHttpConnector.class);
        WebClientCustomizer clientCustomizer = context.getBean(WebClientCustomizer.class);
        WebClient.Builder builder = mock(WebClient.Builder.class);
        clientCustomizer.customize(builder);
        then(builder).should().clientConnector(any(ReactorClientHttpConnector.class));
    });
}
Also used : WebClientCustomizer(org.springframework.boot.web.reactive.function.client.WebClientCustomizer) WebClient(org.springframework.web.reactive.function.client.WebClient) ReactorClientHttpConnector(org.springframework.http.client.reactive.ReactorClientHttpConnector) Test(org.junit.jupiter.api.Test)

Example 3 with WebClientCustomizer

use of org.springframework.boot.web.reactive.function.client.WebClientCustomizer in project spring-boot by spring-projects.

the class WebClientAutoConfigurationTests method webClientShouldApplyCustomizers.

@Test
void webClientShouldApplyCustomizers() {
    this.contextRunner.withUserConfiguration(WebClientCustomizerConfig.class).run((context) -> {
        WebClient.Builder builder = context.getBean(WebClient.Builder.class);
        WebClientCustomizer customizer = context.getBean("webClientCustomizer", WebClientCustomizer.class);
        builder.build();
        then(customizer).should().customize(any(WebClient.Builder.class));
    });
}
Also used : WebClientCustomizer(org.springframework.boot.web.reactive.function.client.WebClientCustomizer) WebClient(org.springframework.web.reactive.function.client.WebClient) Test(org.junit.jupiter.api.Test)

Example 4 with WebClientCustomizer

use of org.springframework.boot.web.reactive.function.client.WebClientCustomizer in project spring-boot by spring-projects.

the class ClientHttpConnectorAutoConfigurationTests method shouldNotOverrideCustomClientConnector.

@Test
void shouldNotOverrideCustomClientConnector() {
    this.contextRunner.withUserConfiguration(CustomClientHttpConnectorConfig.class).run((context) -> {
        assertThat(context).hasSingleBean(ClientHttpConnector.class).hasBean("customConnector").doesNotHaveBean(ReactorResourceFactory.class);
        WebClientCustomizer clientCustomizer = context.getBean(WebClientCustomizer.class);
        WebClient.Builder builder = mock(WebClient.Builder.class);
        clientCustomizer.customize(builder);
        then(builder).should().clientConnector(any(ClientHttpConnector.class));
    });
}
Also used : WebClientCustomizer(org.springframework.boot.web.reactive.function.client.WebClientCustomizer) ReactorClientHttpConnector(org.springframework.http.client.reactive.ReactorClientHttpConnector) ClientHttpConnector(org.springframework.http.client.reactive.ClientHttpConnector) WebClient(org.springframework.web.reactive.function.client.WebClient) Test(org.junit.jupiter.api.Test)

Example 5 with WebClientCustomizer

use of org.springframework.boot.web.reactive.function.client.WebClientCustomizer in project spring-boot by spring-projects.

the class WebClientAutoConfigurationTests method shouldGetPrototypeScopedBean.

@Test
void shouldGetPrototypeScopedBean() {
    this.contextRunner.withUserConfiguration(WebClientCustomizerConfig.class).run((context) -> {
        ClientHttpResponse response = mock(ClientHttpResponse.class);
        given(response.getBody()).willReturn(Flux.empty());
        given(response.getHeaders()).willReturn(new HttpHeaders());
        ClientHttpConnector firstConnector = mock(ClientHttpConnector.class);
        given(firstConnector.connect(any(), any(), any())).willReturn(Mono.just(response));
        WebClient.Builder firstBuilder = context.getBean(WebClient.Builder.class);
        firstBuilder.clientConnector(firstConnector).baseUrl("https://first.example.org");
        ClientHttpConnector secondConnector = mock(ClientHttpConnector.class);
        given(secondConnector.connect(any(), any(), any())).willReturn(Mono.just(response));
        WebClient.Builder secondBuilder = context.getBean(WebClient.Builder.class);
        secondBuilder.clientConnector(secondConnector).baseUrl("https://second.example.org");
        assertThat(firstBuilder).isNotEqualTo(secondBuilder);
        firstBuilder.build().get().uri("/foo").retrieve().toBodilessEntity().block(Duration.ofSeconds(30));
        secondBuilder.build().get().uri("/foo").retrieve().toBodilessEntity().block(Duration.ofSeconds(30));
        then(firstConnector).should().connect(eq(HttpMethod.GET), eq(URI.create("https://first.example.org/foo")), any());
        then(secondConnector).should().connect(eq(HttpMethod.GET), eq(URI.create("https://second.example.org/foo")), any());
        WebClientCustomizer customizer = context.getBean("webClientCustomizer", WebClientCustomizer.class);
        then(customizer).should(times(2)).customize(any(WebClient.Builder.class));
    });
}
Also used : WebClientCustomizer(org.springframework.boot.web.reactive.function.client.WebClientCustomizer) HttpHeaders(org.springframework.http.HttpHeaders) ClientHttpConnector(org.springframework.http.client.reactive.ClientHttpConnector) ClientHttpResponse(org.springframework.http.client.reactive.ClientHttpResponse) WebClient(org.springframework.web.reactive.function.client.WebClient) Test(org.junit.jupiter.api.Test)

Aggregations

WebClientCustomizer (org.springframework.boot.web.reactive.function.client.WebClientCustomizer)5 WebClient (org.springframework.web.reactive.function.client.WebClient)5 Test (org.junit.jupiter.api.Test)4 ClientHttpConnector (org.springframework.http.client.reactive.ClientHttpConnector)2 ReactorClientHttpConnector (org.springframework.http.client.reactive.ReactorClientHttpConnector)2 ApolloClientWebClientCustomizerFactory (com.ctrip.framework.apollo.config.data.extension.webclient.customizer.spi.ApolloClientWebClientCustomizerFactory)1 HttpClient (com.ctrip.framework.apollo.util.http.HttpClient)1 HttpHeaders (org.springframework.http.HttpHeaders)1 ClientHttpResponse (org.springframework.http.client.reactive.ClientHttpResponse)1