Description
Ruslan Stelmachenko opened SPR-16093 and commented
There is convinient method ClientRequest.from(ClientRequest other)
which returns ClientRequest.Builder
instance.
It is useful to create a request copy in ExchangeFilterFunction
to modify request in some way. For example, to add some heasers.
Actually, this is used in ExchangeFilterFunctions.basicAuthentication()
helper.
It works fine, but sometimes you need to modify not only headers, but also request URI (to add sign
query parameter for example).
Now we forced to manually copy each request part to the new request just to change the URL:
private ExchangeFilterFunction addSignFilter() {
return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
URI url = UriComponentsBuilder.fromUri(clientRequest.url())
.queryParam("sign", createSign(clientRequest))
.build()
.toUri();
ClientRequest authenticatedRequest = ClientRequest.method(clientRequest.method(), url)
.headers(headers -> headers.addAll(clientRequest.headers()))
.cookies(cookies -> cookies.addAll(clientRequest.cookies()))
.attributes(attributes -> attributes.putAll(clientRequest.attributes()))
.body(clientRequest.body())
.build();
return Mono.just(authenticatedRequest);
});
}
But if request builder had the url(URI url)
setter, then we were able to do:
private ExchangeFilterFunction addSignFilter() {
return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
URI url = UriComponentsBuilder.fromUri(clientRequest.url())
.queryParam("sign", createSign(clientRequest))
.build()
.toUri();
ClientRequest authenticatedRequest = ClientRequest.from(clientRequest)
.url(url)
.build();
return Mono.just(authenticatedRequest);
});
}
This is much less error-prone when we want to change just one part of the request (URL in this case), than copying each request part by hand when creating new builder instance manually or using ClientRequest.method(HttpMethod method, URI url)
.
Affects: 5.0 GA