Search in sources :

Example 11 with RestTemplateBuilder

use of org.springframework.boot.web.client.RestTemplateBuilder in project spring-boot by spring-projects.

the class RestTemplateAutoConfiguration method restTemplateBuilder.

@Bean
@ConditionalOnMissingBean
public RestTemplateBuilder restTemplateBuilder() {
    RestTemplateBuilder builder = new RestTemplateBuilder();
    HttpMessageConverters converters = this.messageConverters.getIfUnique();
    if (converters != null) {
        builder = builder.messageConverters(converters.getConverters());
    }
    List<RestTemplateCustomizer> customizers = this.restTemplateCustomizers.getIfAvailable();
    if (!CollectionUtils.isEmpty(customizers)) {
        customizers = new ArrayList<>(customizers);
        AnnotationAwareOrderComparator.sort(customizers);
        builder = builder.customizers(customizers);
    }
    return builder;
}
Also used : RestTemplateBuilder(org.springframework.boot.web.client.RestTemplateBuilder) RestTemplateCustomizer(org.springframework.boot.web.client.RestTemplateCustomizer) HttpMessageConverters(org.springframework.boot.autoconfigure.http.HttpMessageConverters) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 12 with RestTemplateBuilder

use of org.springframework.boot.web.client.RestTemplateBuilder in project spring-boot by spring-projects.

the class RootUriRequestExpectationManagerTests method boundRestTemplateShouldPrefixRootUri.

@Test
public void boundRestTemplateShouldPrefixRootUri() {
    RestTemplate restTemplate = new RestTemplateBuilder().rootUri("http://example.com").build();
    MockRestServiceServer server = RootUriRequestExpectationManager.bindTo(restTemplate);
    server.expect(requestTo("/hello")).andRespond(withSuccess());
    restTemplate.getForEntity("/hello", String.class);
}
Also used : RestTemplateBuilder(org.springframework.boot.web.client.RestTemplateBuilder) RestTemplate(org.springframework.web.client.RestTemplate) MockRestServiceServer(org.springframework.test.web.client.MockRestServiceServer) Test(org.junit.Test)

Example 13 with RestTemplateBuilder

use of org.springframework.boot.web.client.RestTemplateBuilder in project spring-boot by spring-projects.

the class RootUriRequestExpectationManagerTests method boundRestTemplateWhenUrlIncludesDomainShouldNotPrefixRootUri.

@Test
public void boundRestTemplateWhenUrlIncludesDomainShouldNotPrefixRootUri() {
    RestTemplate restTemplate = new RestTemplateBuilder().rootUri("http://example.com").build();
    MockRestServiceServer server = RootUriRequestExpectationManager.bindTo(restTemplate);
    server.expect(requestTo("/hello")).andRespond(withSuccess());
    this.thrown.expect(AssertionError.class);
    this.thrown.expectMessage("expected:<http://example.com/hello> but was:<http://spring.io/hello>");
    restTemplate.getForEntity("http://spring.io/hello", String.class);
}
Also used : RestTemplateBuilder(org.springframework.boot.web.client.RestTemplateBuilder) RestTemplate(org.springframework.web.client.RestTemplate) MockRestServiceServer(org.springframework.test.web.client.MockRestServiceServer) Test(org.junit.Test)

Example 14 with RestTemplateBuilder

use of org.springframework.boot.web.client.RestTemplateBuilder in project spring-boot by spring-projects.

the class RootUriRequestExpectationManagerTests method forRestTemplateWhenNotUsingRootUriTemplateHandlerShouldReturnOriginalRequestExpectationManager.

@Test
public void forRestTemplateWhenNotUsingRootUriTemplateHandlerShouldReturnOriginalRequestExpectationManager() throws Exception {
    RestTemplate restTemplate = new RestTemplateBuilder().build();
    RequestExpectationManager actual = RootUriRequestExpectationManager.forRestTemplate(restTemplate, this.delegate);
    assertThat(actual).isSameAs(this.delegate);
}
Also used : RequestExpectationManager(org.springframework.test.web.client.RequestExpectationManager) RestTemplateBuilder(org.springframework.boot.web.client.RestTemplateBuilder) RestTemplate(org.springframework.web.client.RestTemplate) Test(org.junit.Test)

Aggregations

RestTemplateBuilder (org.springframework.boot.web.client.RestTemplateBuilder)14 Test (org.junit.Test)12 RestTemplate (org.springframework.web.client.RestTemplate)10 MockRestServiceServer (org.springframework.test.web.client.MockRestServiceServer)4 RequestExpectationManager (org.springframework.test.web.client.RequestExpectationManager)2 Before (org.junit.Before)1 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)1 HttpMessageConverters (org.springframework.boot.autoconfigure.http.HttpMessageConverters)1 MockServerRestTemplateCustomizer (org.springframework.boot.test.web.client.MockServerRestTemplateCustomizer)1 RestTemplateCustomizer (org.springframework.boot.web.client.RestTemplateCustomizer)1 Bean (org.springframework.context.annotation.Bean)1