Spring Boot: How to set RestTemplate read timeout

Using the class RestTemplateBuilder it is very easy to configure the RestTemplate you need.

Here is a snippet that shows you how to configure the read timeout on a RestTemplate instance.

NB: you can set timeouts in java.time.Duration (instead of int) since Spring Boot 2.1

@Component
public class MyRestClient {

	@Value("${service.client.timeout.read}")
	private Duration readTimeout;

	private RestTemplate restTemplate;

	public MyRestClient(RestTemplateBuilder restTemplateBuilder) {
	   this.restTemplate = restTemplateBuilder
							 .setReadTimeout(readTimeout)
							 .build();
	}
	
	// Code that actually uses RestTemplate...
}

Then you just have to set the value of the property “service.client.timeout.read” (instead of hardcoding it)

service.client.timeout.read=5s

Spring Boot: How to disable console logging using properties

To disable console logging, add the following properties to application.properties file:

# Empty this property to disable console logging
logging.pattern.console=

# Specify the path to the main log file so you will still have your logs somewhere
logging.file=/path/to/folder/application.log

If you want to enable console logging in your IDE, here are two ways to re-enable it:

  • Add an alias to the log file /path/to/folder/application.log in your Run Configuration
  • Override the property “logging.pattern.console” in your Run Configuration

Tested with Spring Boot 2.1.4