Search code examples
spring-bootfacebook-graph-apispring-webfluxillegalargumentexceptionspring-webclient

Spring Webclient : illegalargumentexception not enough variables to expand 'comment_count'


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.


Solution

  • You can create your URL using UriComponentsBuilder as follows:

    webClient.get().uri(getFacebookGraphURI(3)).retrieve().bodyToMono(Object.class);
        
    private URI getFacebookGraphURI(int comments) {
         return UriComponentsBuilder.fromHttpUrl("https://graph.facebook.com")
                .pathSegment("v3.2", "PAGE_ID", "posts").queryParam("fields", "comments{comment_count}")
                .queryParam("access_token", "acacaaac").build(comments);
    }
    

    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);