Search code examples
javaspring-boottimeoutmaven-pluginresttemplate

Configuring connection and read timeouts for a generated api rest client


I am using swagger-codegen-maven-plugin to generate an api rest client. I am using the resttemplate library and my pom.xml configuration looks like the following.

   <build>
        <plugins>
            <plugin>
                <groupId>io.swagger</groupId>
                <artifactId>swagger-codegen-maven-plugin</artifactId>
                <version>2.3.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>${project.basedir}/../swagger.yml</inputSpec>
                            <language>java</language>
                            <generateApis>true</generateApis>
                            <generateApiTests>false</generateApiTests>
                            <generateSupportingFiles>true</generateSupportingFiles>
                            <generateModelDocumentation>false</generateModelDocumentation>
                            <modelPackage>my.base.package.here</modelPackage>
                            <apiPackage>my.base.api.package.here</apiPackage>
                            <modelNamePrefix>MyApiPrefix</modelNamePrefix>
                            <configOptions>
                                <library>resttemplate</library>
                                <sourceFolder>src/gen/java</sourceFolder>
                                <java8>true</java8>
                                <dateLibrary>java8</dateLibrary>
                                <hideGenerationTimestamp>true</hideGenerationTimestamp>
                            </configOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

The client generation works sucessfully. Now I am using this client in a spring boot app and I need to configure the connection timeout and the read timeout values.

I was surprised to find no setters for these two properties on the generated ApiClient. So I had to come out with a workaround that uses the RestTemplateBuilder to do that. It looks like this:

@Configuration
public class MyApiClientConfiguration {

    @Autowired
    public MyApiClientConfiguration(
            final RestTemplate restTemplate,
            final RestTemplateBuilder restTemplateBuilder
    ) {
        restTemplateBuilder
                .setConnectTimeout(connectTimeoutValue)
                .setReadTimeout(readTimeoutValue)
                .configure(restTemplate);

        ApiClient apiClient = new ApiClient(restTemplate);

        MyApiPrefixApi.setApiClient(apiClient);
    }
}

The doc for the configure method does not say much:

configure

public T configure(T restTemplate)

Configure the provided RestTemplate instance using this builder.

Is this the standard approach to configure timeouts when using the resttemplate library? UPDATE: My actual implementation does not work (it seems the ResttemplateBuilder own configurations are interfering in a wrong way with the autowired RestTemplate (that I am trying to further configure through the builder)) configuration. The api client generated by other libraries (such as jersey for example) provide a setter to directly configure the timeout using:

apiClient.setConnectTimeout(timeoutValue);

Solution

  • Though the use of the configure method does not work, it is possible to configure the timeout values by creating a RestTemplate bean using the RestTemplateBuilder's build method.

    @Bean
    public RestTemplate restTemplate(
           RestTemplateBuilder restTemplateBuilder,
           final int serviceConnectionTimeout,
           final int serviceReadTimeout
            ) {
            return restTemplateBuilder
                .setConnectTimeout(serviceConnectionTimeout)
                .setReadTimeout(serviceReadTimeout)
                .build();
    }
    

    Ref: Spring RestTemplate timeout