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

How to solve issue SaajSoapMessageException: Could not write message to OutputStream

SaajSoapMessageException error

While implementing a SOAP client using Spring WS I’ve got the following error when I tried to write the content of the SOAP message to my logger:

org.springframework.ws.soap.saaj.SaajSoapMessageException: Could not write message to OutputStream: Error during saving a multipart message; nested exception is com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Error during saving a multipart message
at org.springframework.ws.soap.saaj.SaajSoapMessage.writeTo(SaajSoapMessage.java:280)

Continue reading

Spring: Getting HTTP 406 from a REST controller

You may get a HTTP 406 Not Acceptable error while trying to return Java objects from a REST controller.

The server is not able to handle your request because the HTTP header “Accept” does not match with any of the content types it can handle.

In fact, the server might be unable to deliver the response in whatever content-type, some piece of configuration is missing in your project.

Continue reading