Closed xenoterracide closed 1 week ago
The underlying issue here is that the FormHttpMessageConverter
only accepts MultiValueMap
types; not Map
types. The following does work:
var login = restClient
.post()
.uri("/login")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.body(CollectionUtils.toMultiValueMap(Map.of("username", List.of("foo"), "password", List.of("bar"))))
.retrieve()
.toEntity(String.class);
which is definitely less elegant.
To fix this, we would have to change FormHttpMessageConverter
from a HttpMessageConverter<MultiValueMap<String, ?>>
to a HttpMessageConverter<Map<String, ?>>
which can determine at runtime whether it's dealing with a single or multi value map. However, this is a breaking change. Could we make such a change in 6.2, @jhoeller?
Alternatively, we can introduce a new SingleValueFormHttpMessageConverter
which implements HttpMessageConverter<Map<String, ?>>
and simply delegates to a (the existing?) FormHttpMessageConverter
.
I definitely prefer doing the former, as having duplicate form converters seems unnecessary and complicated, also in terms of configuration.
Maybe a couple of other alternatives. I'm not sure how possible the first one is.
- Could the map be converted to a multi-map under the hood without changing the interface? I'm not looking at The converter as I say this.
Not really, the interface defines what types are accepted for reading and writing.
- Not really the same answer as I wrote the bug for but perhaps there could be a static factory on The multi-map interface that matches the map.of contract. Also I would consider adding a regular of that matches the collection utils method that you mentioned. As I certainly didn't look for it there these days 😉
We can certainly consider introducing similar convenience methods. I created a separate issue for that.
After team discussion, we decided that the best way to go forward is to change the FormHttpMessageConverter
from a HttpMessageConverter<MultiValueMap<String, ?>>
to a HttpMessageConverter<Map<String, ?>>
.
Affects: 6.1.6
This feels like it should work. I get there are scenarios that it can't, and I don't care if it does on response.