Spring Webclient: незаконное исключение не достаточно переменных для расширения 'comment_count' - PullRequest
0 голосов
/ 01 октября 2019

Я использую весенний веб-клиент для отправки API-запроса на график Facebook с URL-адресом, содержащим {comment_count}

Но, получая это исключение

java.lang.IllegalArgumentException: Not enough variable values available to expand reactive spring

Фрагмент кода:

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

Я знаю решение с resttemplate, но мне нужно использовать весенний веб-клиент.

1 Ответ

5 голосов
/ 03 октября 2019

Вы можете создать свой URL с помощью UriComponentsBuilder следующим образом

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

  }

ИЛИ

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

...