Skip to content

use templates with the restClient #168

@tomellm

Description

@tomellm

Lander showed us how to use Templates with the restClient. This would allow us to not have to use the restClient directly but through an interface.

 @Bean
public TestClient2 eLoketClient2(GenericConversionService conversionService, ObservationRegistry observationRegistry) {
    HttpClient httpClient = HttpClient.newBuilder()
        .connectTimeout(Duration.of(timeout, ChronoUnit.SECONDS))
        .build();
    JdkClientHttpRequestFactory requestFactory = new JdkClientHttpRequestFactory(httpClient);
    requestFactory.setReadTimeout(Duration.of(timeout, ChronoUnit.SECONDS));

    RestClient restClient = RestClient.builder()
        .requestFactory(requestFactory)
        .baseUrl(baseUrl)
            .observationRegistry(observationRegistry)
            .observationConvention(customClientRequestObservationConvention)
        .messageConverters(c -> {
            MappingJackson2HttpMessageConverter converter = c.stream()
                    .filter(MappingJackson2HttpMessageConverter.class::isInstance)
                    .map(MappingJackson2HttpMessageConverter.class::cast)
                    .findFirst().get();
                converter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON, MediaType.APPLICATION_PROBLEM_JSON, MediaType.TEXT_PLAIN));
            }
        )
        .build();
    HttpServiceProxyFactory factory = HttpServiceProxyFactory
        .builderFor(RestClientAdapter.create(restClient))
        .conversionService(conversionService)
        .build();
    return factory.createClient(TestClient2.class);
}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions