WebClient decodes string array as one string
See original GitHub issueAssume we have an endpoint that returns string array, like this (ContentType is application/json)
["a","a","a"]
and retrieve it’s response as Flux<String>
webClient.get().uri().retrieve()
.bodyToFlux(String.class)
.subscribe(s -> println(s))
In my opinion, it should print “a” for three times, but only prints ["a","a","a"]
.
However, there is a workaround, we can replace String.class
with String[].class
, but the result returned in semantic isn’t a Flux
anymore, but a Mono
.
(The version of Spring Webflux is 5.2.3-RELEASE)
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:8 (2 by maintainers)
Top Results From Across the Web
Spring WebClient: How to convert a HTTP response body to ...
Setting a breakpoint in StringDecoder#decode and manually overwriting the defaultCharset field during debugging, however, does help. java ...
Read more >Spring 5 WebClient and WebTestClient Tutorial with Examples
Hi I am getting "Only one connection receive subscriber allowed" when I am sending XML body using post method. I suspect xml body...
Read more >Web on Reactive Stack - Spring
If what you need is to render a JSON array from Flux<String> , use Flux#collectToList() and encode a Mono<List<String>> .
Read more >Get List of JSON Objects with WebClient - Baeldung
In this article, we'll find out how to convert a JSON Array into a Java Array of Object, Array of POJO, and a...
Read more >WebClient.UploadData Method (System.Net) - Microsoft Learn
The following code example converts a string entered from the console to a Byte array and posts the array to the specified server...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Having read the maintainers’ response, I still can’t understand why is this ticket closed, and why the following:
["a","b","c"]
is interpreted by WebClient as a single string. Former RestTemplate interpreted it correctly as a collection. So, Flux should correctly parse it as aFlux<String>
with 3 string elements, and not one string representing JSON. WebClient would be right, if the content was like this:"[\"a\",\"b\",\"c\"]"
. Then I’d agree that Flux should return one string element["a","b","c"]
.The workaround I mentioned is something like
then you can convert string[] to any type you like