I am using spring webclient to make a Facebook graph api request with url containing {comment_count}
But, getting this exception
java.lang.IllegalArgumentException: Not enough variable values available to expand reactive spring
Code Snippet :
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
@Component
public class Stackoverflow {
WebClient client = WebClient.create();
public Mono<Post> fetchPost(String url) {
// Url contains "comments{comment_count}"
return client.get().uri(url).retrieve()
.bodyToMono(Post.class);
}
}
I know the solution with resttemplate, But i need to use spring webclient.
2
Answers
You can create your URL using UriComponentsBuilder as follows
OR
int commentsCount = 3; webClient.get().uri(UriComponentsBuilder.fromHttpUrl(“https://graph.facebook.com“)
.pathSegment(“v3.2”, “PAGE_ID”, “posts”).queryParam(“fields”, “comments{comment_count}”)
.queryParam(“access_token”, “acacaaac”).build().toString(),commentsCount).retrieve().bodyToMono(Object.class);
The solution I use is to disable the encoding in DefaultUriBuilderFactory
It’s in Kotlin, in Java you just have to use
DefaultUriBuilderFactory#setEncodingMode(EncodingMode)
withNONE
as parameter.Due to this change of behavior, you have to encode your query params yourself. To do so, I use the
And I can perform call like this:
For Java (as per comment by Anurag below) it will be something like this:
As noted above, you will still need to URL-encode the parameter
value
. With this approach, you are merely avoiding "double URL encoding" by replacing theurlBuilderFactory
.