question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

WebClient decodes string array as one string

See original GitHub issue

Assume 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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
pablo53commented, Aug 29, 2022

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 a Flux<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"].

1reaction
bringyoucommented, May 27, 2021

The workaround I mentioned is something like

webClient.get().uri().retrieve().bodyToFlux(String[].class);   // now we get a flux<String[]> which only has one item
webClient.get().uri().retrieve().bodyToMono(String[].class);  // or a mono<String[]>

then you can convert string[] to any type you like

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found