我正在使用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 MonofetchPost(String url) { // Url contains "comments{comment_count}" return client.get().uri(url).retrieve() .bodyToMono(Post.class); } }
我知道resttemplate的解决方案,但是我需要使用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”,“帖子”).queryParam(“ fields”,“评论{comment_count}“).queryParam(” access_token“,” acacaaac“)。build()。toString(),commentsCount).retrieve()。bodyToMono(Object.class);