Spring WebClient:非法参数异常没有足够的变量来展开'Comment_Count' [英] Spring Webclient : illegalargumentexception not enough variables to expand 'comment_count'

本文介绍了Spring WebClient:非法参数异常没有足够的变量来展开'Comment_Count'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring WebClient使用包含{COMMENT_COUNT}的URL发出Facebook图形API请求

但是,获取此异常

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

我知道REST模板的解决方案,但我需要使用Spring WebClient。

推荐答案

您可以使用UriComponentsBuilder创建您的URL,如下所示

 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("field","Comments{COMMENT_COUNT}") .queryParam("Access_Token","acacaaac").Build().toString(),commentsCount).retrieve().bodyToMono(Object.class);

这篇关于Spring WebClient:非法参数异常没有足够的变量来展开&#39;Comment_Count&#39;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆