-
Notifications
You must be signed in to change notification settings - Fork 38.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow ExchangeStrategies customizations in WebClient #23961
Comments
If you are using Spring Boot and creating your WebClient using a WebClient.Builder configured by Spring Boot (you can get it injected), you can use the new configuration property spring.codec.max-in-memory-size, see spring-projects/spring-boot#18828. Otherwise if you’re creating that client from scratch you need to configure the codecs like:
|
Thanks @bclozel . Since I use the autowired We happen to be using Spring HATEOAS and I wonder if something there is overriding this configuration. In particular there's class webClient.mutate().exchangeStrategies(hypermediaExchangeStrategies()).build() I've already noticed that this seems to mess up the Could that be the reason? |
We need to improve the documentation for the client side of things. This should help non Spring Boot users and developers building their own Now for HATEOAS, we're probably missing an extension point to allow it to customize the codecs without replacing them - we should provide |
Thanks @bclozel ! |
@bclozel Is it intentional that we're introducing an |
@jhoeller yes this is intentional. I’ve added a new method on the We’ve added clone and mutate methods to solve the current problem first without breaking the contract and we’ll remove all that in a future release. I’ve added comments along those lines in the commit message itself. |
This change is breaking integrations with other projects. Reverting for now and rescheduling to another version. |
…naged version dependencies * Fix issue with H2 by updating version to r2dbc-h2-0.8.1.RELEASE * Adjust WebClientConfig.java as per spring-projects/spring-framework#23961 (comment) and increase spring.codec.max-in-memory-size property value in application.yml to 512000000 * Adjust SpaceUsersTask.java to flatMap on save
Hi, |
We have such tests for each codec (e.g. for Jackson) so you shouldn't have to test that. All you should test is how your app is configured. That aside you can probably use |
@rstoyanchev, you're right. I just wanted to create a simple unit test to ensure that the the custom memory size was set correctly. This is a snippet of the solution I managed to do with some Kotlin code: @SpringBootTest
class MyTest(@Value("100") private val maxMemorySize: Int) {
...
@Test
fun `DataBufferLimitException is thrown when 'memoryBufferData' is greater than the custom 'maxMemorySize (100)'`() {
// Given:
val memoryBufferData = "A".repeat(maxMemorySize)
val toJson = "\"$memoryBufferData\""
val mockResponse = MockResponse().setBody(toJson)
val webClient = MockWebServer()
.also { it.enqueue(mockResponse) }
.also { it.start() }
// Then:
assertThrows<DataBufferLimitException> {
webClient.get(path = "/test", entityType = Any::class)
}
}
@Test
fun `Response is the same as 'memoryBufferData' when memoryBuffer is within the custom 'maxMemorySize (100)'`() {
// Given:
val belowMaxMemorySize = maxMemorySize - 2
val memoryBufferData = "A".repeat(belowMaxMemorySize)
val toJson = "\"$memoryBufferData\""
val mockResponse = MockResponse().setBody(toJson)
val webClient = MockWebServer()
.also { it.enqueue(mockResponse) }
.also { it.start() }
// When:
val response = webClient.get(path = "/test", entityType = Any::class)
// Then:
assertEquals(response, memoryBufferData)
...
} Thank you for taking the time @rstoyanchev ! |
i was getting this error for a simple RestController (i post a large json string). here is how i successfully changed the
this was surprisingly hard to find |
@datumgeek we've got this covered in the reference docs. Don't hesitate to share improvement ideas! Thanks! |
@bclozel - thank you for the reference !! i did see that part of the doc... but from that description, i wasn't able to figure out how to fix the error in the rest controller... maybe it needs a reference to a code sample? i'm guessing this is a fairly common issue for folks developing rest controllers... the answer was also not present in the stackoverflow questions i found when searching. i tried to update a few of them 😄 once you have the recipe, it is very easy 😉 |
There is a code sample in the config section. There are links to it from the referenced part of the doc? |
@rstoyanchev - maybe i'm just being dense 😉 i was trying to configure the i was thinking it would be good if there was a code sample for how to do this via like maybe if it said specifically, if you are trying to change the maxInMemoryBytes for a RestController do this:
|
No that's fine, I made an improvement. |
In my case it was jensgram's answer on Stackoverflow that helped for the original problem:
I hope this helps the next engineer that comes across here... |
I still don't understand, why there is no simple configuration property to set the maxInMemory size for all webclient client instances globally. Using exchange strategies is too clumsy to setup such simple thing. |
Can anyone confirm the default limit is still 256KB? In my Spring Boot
which seems to indicate the default limit is actually 1MB. Doubling the limit by adding the following filter (as indicated in this section of the documentation) did not fix the issue:
Doubling the limit while building the
|
I think there's some confusion here because there are two parts to be configured, the client and the server. Depending on your use-cases you might need to configure just the client (if you are making requests), the server (if your are receiving requests as a resource server) or both. The server can be configured by overriding WebFluxConfigurer.configureHttpMessageCodecs where you get a ServerCodecConfigurer. The client can be configured with the WebClient.Builder.codecs where you get a ClientCodecConfigurer. |
Seems caused by 89d053d / #23884.
After upgrading from Spring Boot 2.2.0 to 2.2.1, WebClient started throwing
org.springframework.core.io.buffer.DataBufferLimitException: Exceeded limit on max bytes to buffer : 262144
when calling a JSON REST API (that has a large response size).From the linked documentation (https://github.com/spring-projects/spring-framework/blob/master/src/docs/asciidoc/web/webflux.adoc#limits), it's not clear what configuration should be changed to make our API calls work again.
Is this expected behavior?
Full stack trace:
The text was updated successfully, but these errors were encountered: