【问题标题】:Spring webclient throwing block not supported exceptionSpring webclient抛出块不支持异常
【发布时间】:2020-09-02 09:22:04
【问题描述】:

我正在尝试从 spring-boot 发起 HTTP 调用。电话在邮递员中工作正常,下面是电话的curl 版本,

curl --location --request POST 'https://sampletest.com:8811/rest/oauth/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--header 'Accept: application/json' \
--data-urlencode 'client_id=126763****ghsei99****' \
--data-urlencode 'client_secret=126763****ghsei99****' \
--data-urlencode 'param1=pppp' \
--data-urlencode 'param2=pppp'

但我尝试使用WebClient 拨打同样的电话。

WebClient webClient = WebClient.builder().baseUrl("https://sampletest.com:8811")
                    .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE).build();

            MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
            formData.add("client_id","126763****ghsei99****");
            formData.add("client_secret", "126763****ghsei99****");
            formData.add("param1","pppp");
            formData.add("param2","pppp");

            AuthenticationResponseBean authenticationResponseBean = webClient.post().uri("/rest/oauth/token").body(BodyInserters.fromFormData(formData))
                    .retrieve().bodyToFlux(AuthenticationResponseBean.class).blockLast();

但是,应用程序正在抛出异常,

java.lang.IllegalStateException: block()/blockFirst()/blockLast() 是 阻塞,线程 reactor-http-nio-3 不支持

这里出了什么问题?我对reactive-programming 很陌生,谷歌搜索问题表明为什么需要block

提前谢谢你。

【问题讨论】:

    标签: java spring-boot http resttemplate spring-webclient


    【解决方案1】:

    据我了解,阻塞实际上阻塞了线程。来自ProyectReactor docs

    • " 无限期阻塞,直到上游发出最后一个值或 完成”。

    所以如果你不需要阻塞,或者你不需要等待最后一个元素,你就不需要它。因此,根据您的需要,只需订阅即可。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-22
      • 2010-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多