Search in sources :

Example 21 with RestTemplateBuilder

use of org.springframework.boot.web.client.RestTemplateBuilder in project FredBoat by Frederikam.

the class RestTemplateConfiguration method quarterdeckRestTemplate.

@Bean
public RestTemplate quarterdeckRestTemplate(BackendConfig backendConfig, GsonHttpMessageConverter gson) {
    return new RestTemplateBuilder().basicAuthorization(backendConfig.getQuarterdeck().getUser(), backendConfig.getQuarterdeck().getPass()).requestFactory(this::clientHttpRequestFactory).messageConverters(gson).additionalInterceptors(((req, body, execution) -> {
        String method = req.getMethodValue();
        String uri = req.getURI().toString();
        log.debug(">>>{} {} {}", method, uri, new String(body));
        ClientHttpResponse response = execution.execute(req, body);
        if (response.getBody().markSupported()) {
            // this is true if the buffering http request factory is used
            response.getBody().mark(Integer.MAX_VALUE);
            log.debug("<<<{} {} {}", method, uri, IOUtils.toString(response.getBody(), "UTF-8"));
            response.getBody().reset();
        }
        return response;
    })).build();
}
Also used : RestTemplateBuilder(org.springframework.boot.web.client.RestTemplateBuilder) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) Bean(org.springframework.context.annotation.Bean)

Example 22 with RestTemplateBuilder

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

the class RestTemplateMetricsConfigurationTests method restTemplateCreatedWithBuilderIsInstrumented.

@Test
void restTemplateCreatedWithBuilderIsInstrumented() {
    this.contextRunner.run((context) -> {
        MeterRegistry registry = context.getBean(MeterRegistry.class);
        RestTemplateBuilder builder = context.getBean(RestTemplateBuilder.class);
        validateRestTemplate(builder, registry);
    });
}
Also used : RestTemplateBuilder(org.springframework.boot.web.client.RestTemplateBuilder) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) Test(org.junit.jupiter.api.Test)

Example 23 with RestTemplateBuilder

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

the class MetricsRestTemplateCustomizerTests method whenAutoTimingIsDisabledUriTemplateHandlerDoesNotCaptureUris.

@Test
void whenAutoTimingIsDisabledUriTemplateHandlerDoesNotCaptureUris() {
    AtomicBoolean enabled = new AtomicBoolean();
    AutoTimer autoTimer = new AutoTimer() {

        @Override
        public boolean isEnabled() {
            return enabled.get();
        }

        @Override
        public void apply(Builder builder) {
        }
    };
    RestTemplate restTemplate = new RestTemplateBuilder(new MetricsRestTemplateCustomizer(this.registry, new DefaultRestTemplateExchangeTagsProvider(), "http.client.requests", autoTimer)).build();
    MockRestServiceServer mockServer = MockRestServiceServer.createServer(restTemplate);
    mockServer.expect(MockRestRequestMatchers.requestTo("/first/123")).andExpect(MockRestRequestMatchers.method(HttpMethod.GET)).andRespond(MockRestResponseCreators.withSuccess("OK", MediaType.APPLICATION_JSON));
    mockServer.expect(MockRestRequestMatchers.requestTo("/second/456")).andExpect(MockRestRequestMatchers.method(HttpMethod.GET)).andRespond(MockRestResponseCreators.withSuccess("OK", MediaType.APPLICATION_JSON));
    assertThat(restTemplate.getForObject("/first/{id}", String.class, 123)).isEqualTo("OK");
    assertThat(this.registry.find("http.client.requests").timer()).isNull();
    enabled.set(true);
    assertThat(restTemplate.getForObject(URI.create("/second/456"), String.class)).isEqualTo("OK");
    this.registry.get("http.client.requests").tags("uri", "/second/456").timer();
    this.mockServer.verify();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AutoTimer(org.springframework.boot.actuate.metrics.AutoTimer) RestTemplateBuilder(org.springframework.boot.web.client.RestTemplateBuilder) Builder(io.micrometer.core.instrument.Timer.Builder) RestTemplateBuilder(org.springframework.boot.web.client.RestTemplateBuilder) RestTemplate(org.springframework.web.client.RestTemplate) MockRestServiceServer(org.springframework.test.web.client.MockRestServiceServer) Test(org.junit.jupiter.api.Test)

Example 24 with RestTemplateBuilder

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

the class MetricsRestTemplateCustomizerTests method whenCustomizerAndLocalHostUriTemplateHandlerAreUsedTogetherThenRestTemplateBuilderCanBuild.

@Test
void whenCustomizerAndLocalHostUriTemplateHandlerAreUsedTogetherThenRestTemplateBuilderCanBuild() {
    MockEnvironment environment = new MockEnvironment();
    environment.setProperty("local.server.port", "8443");
    LocalHostUriTemplateHandler uriTemplateHandler = new LocalHostUriTemplateHandler(environment, "https");
    RestTemplate restTemplate = new RestTemplateBuilder(this.customizer).uriTemplateHandler(uriTemplateHandler).build();
    assertThat(restTemplate.getUriTemplateHandler()).asInstanceOf(InstanceOfAssertFactories.type(RootUriTemplateHandler.class)).extracting(RootUriTemplateHandler::getRootUri).isEqualTo("https://localhost:8443");
}
Also used : RestTemplateBuilder(org.springframework.boot.web.client.RestTemplateBuilder) MockEnvironment(org.springframework.mock.env.MockEnvironment) RestTemplate(org.springframework.web.client.RestTemplate) RootUriTemplateHandler(org.springframework.boot.web.client.RootUriTemplateHandler) LocalHostUriTemplateHandler(org.springframework.boot.test.web.client.LocalHostUriTemplateHandler) Test(org.junit.jupiter.api.Test)

Example 25 with RestTemplateBuilder

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

the class RootUriRequestExpectationManagerTests method forRestTemplateWhenNotUsingRootUriTemplateHandlerShouldReturnOriginalRequestExpectationManager.

@Test
void forRestTemplateWhenNotUsingRootUriTemplateHandlerShouldReturnOriginalRequestExpectationManager() {
    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.jupiter.api.Test)

Aggregations

RestTemplateBuilder (org.springframework.boot.web.client.RestTemplateBuilder)37 Test (org.junit.jupiter.api.Test)22 RestTemplate (org.springframework.web.client.RestTemplate)21 MockRestServiceServer (org.springframework.test.web.client.MockRestServiceServer)5 TestRestTemplate (org.springframework.boot.test.web.client.TestRestTemplate)4 MeterRegistry (io.micrometer.core.instrument.MeterRegistry)3 Test (org.junit.Test)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 LocalHostUriTemplateHandler (org.springframework.boot.test.web.client.LocalHostUriTemplateHandler)3 Bean (org.springframework.context.annotation.Bean)3 RestTemplateResponseErrorHandler (edu.hawaii.its.api.controller.RestTemplateResponseErrorHandler)2 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)2 MockServerRestTemplateCustomizer (org.springframework.boot.test.web.client.MockServerRestTemplateCustomizer)2 HttpEntity (org.springframework.http.HttpEntity)2 HttpHeaders (org.springframework.http.HttpHeaders)2 RequestExpectationManager (org.springframework.test.web.client.RequestExpectationManager)2 Builder (io.micrometer.core.instrument.Timer.Builder)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Method (java.lang.reflect.Method)1