Affects: Spring Framework 6.1.0-RC2
The new RestClient allows you to set attributes but it seems that they are not saved in the ClientHttpRequest so you can't retrieve them in a ClientHttpRequestInterceptor.
So what are the RestClient attributes for?
Comment From: poutsma
So what are the RestClient attributes for?
A fair question. They are leftovers from an abandoned experiment that should never have made it into 6.1.0 GA. Therefore, the attribute(String, Object)
and attributes(Consumer)
methods will be removed in 6.1.1, as they are essentially no-ops and not hooked up to the related infrastructure (such as ClientHttpRequest
and—as you correctly pointed out—ClientHttpRequestInterceptor
).
Comment From: chrislhardin
So I actually used the attributes but wasn't aware it was not working. What is the alternative to putting attributes onto the request in this? uriVariables is a HashMap that I was passing in. I don't see another way to add the request attributes other than calling url(u -> u.build(uriVariables)) but I think that is to build the actual url from its constituent components. I just want to pass the url in its entirety and then pass some variable onto it. I could expand them on the url String before I make the call I guess.
restClient.method(method).uri(url)
.attributes(a -> Optional.ofNullable(uriVariables).ifPresent(v ->
a.putAll(uriVariables)))
.headers(httpHeaders -> httpHeaders.addAll(headers)).retrieve().toEntity(responseType);
Comment From: poutsma
So I actually used the attributes but wasn't aware it was not working. What is the alternative to putting attributes onto the request in this? uriVariables is a HashMap that I was passing in. I don't see another way to add the request attributes other than calling url(u -> u.build(uriVariables)) but I think that is to build the actual url from its constituent components. I just want to pass the url in its entirety and then pass some variable onto it. I could expand them on the url String before I make the call I guess.
In the WebClient
, where the concept originated and is still in use, request attributes are used for propagating request-related data. This is necessary because in a reactive environment, you cannot rely on ThreadLocal
. You can compare them to Servlet request attributes.
From your description, it looks like you are trying to build a URI with query parameters based on a map. You can do that by doing something like:
restClient.method(method)
.uri(uriBuilder -> uriBuilder.queryParams(queryParams).build())
.retrieve()
...
Comment From: chrislhardin
yeah I saw that but I was missing the url part so I ended up with this. I haven't tested it yet.
restClient.method(method).uri(url, uriBuilder -> uriBuilder.queryParams(uriVariables).build())
.headers(httpHeaders -> httpHeaders.addAll(headers)).retrieve().toEntity(responseType);
Comment From: KrishnaST
@poutsma Why prefer ThreadLocals when a clean API can be made available to pass attributes?