Created
March 25, 2022 23:22
-
-
Save Jim-Shaddix/3d18ee87ca8b7fea9965ec51da353ea2 to your computer and use it in GitHub Desktop.
Spring Web Client configuration for fetching html. Here I am setting custom properties so that the webclient is able to recieve largver payload sizes. This is needed if you are planning on recieving web resources (suchs as html) from a webclient request.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.web.reactive.function.client.ExchangeStrategies; | |
import org.springframework.web.reactive.function.client.WebClient; | |
@Configuration | |
public class WebClientConfig { | |
/** | |
* This custom webclient configuration is needed | |
* to specify max byte return value. This is needed | |
* because the default size is not large enough for the html | |
* webpages that this client will be requesting. | |
*/ | |
@Bean | |
public WebClient webClient() { | |
// 16 Bytes | |
final int size = 16 * 1024 * 1024; | |
// setting max byte return size in strategy | |
final ExchangeStrategies strategies = ExchangeStrategies.builder() | |
.codecs(codecs -> codecs.defaultCodecs().maxInMemorySize(size)) | |
.build(); | |
// returning a webclient with the configured strategy | |
return WebClient.builder() | |
.exchangeStrategies(strategies) | |
.build(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment