猿问

如果没有 block() 则不会发送请求

我想使用此 webflux 客户端代码发送带回复和不带回复的 POST 请求。我尝试了这个代码实现:


public class RestClientBuilder {

    private String token;

    private String username;

    private String password;

    private URL gatewayUrl;

    private SslContextBuilder sslContextBuilder;


    public static RestClientBuilder builder() {

        return new RestClientBuilder();

    }


    public RestClientBuilder token(String token) {

        this.token = validateAndTrim(token, "Token");

        return this;

    }


    public RestClientBuilder usernamePassword(String username, String password) {

        this.username = validateAndTrim(username, "Username");

        this.password = validateAndTrim(password, "Password");

        return this;

    }


    private String validateAndTrim(String value, final String parameter) {

        if (value == null || value.trim().isEmpty()) {

            throw new IllegalArgumentException(parameter + " is empty");

        }

        return value.trim();

    }


    public RestClientBuilder gatewayUrl(String gatewayUrl) {

        String urlSt = validateAndTrim(gatewayUrl, "Gateway URL");

        try {

            this.gatewayUrl = new URL(urlSt);

        } catch (MalformedURLException e) {

            throw new IllegalArgumentException("Malformed URL: " + urlSt, e);

        }

        return this;

    }


    public RestClientBuilder truststore(File truststoreFile) {

        getSslContextBuilder().trustManager(truststoreFile);

        return this;

    }


    public RestClientBuilder sslCertificate(File keyCertChainFile, File keyFile, String keyPassword) {

        getSslContextBuilder().keyManager(keyCertChainFile, keyFile, keyPassword);

        return this;

    }


    public RestClient build() throws SSLException {

        SslContext sslContext = sslContextBuilder != null ? sslContextBuilder.build() : null;

        return new RestClient(gatewayUrl.toString(), token, username, password, sslContext);

    }



繁星coding
浏览 154回答 3
3回答

桃花长相依

每当您使用 Spring-webflux 时,您都必须记住一件事。即你不必打破你的链条。因为有必要,有人应该在你的链上调用订阅。因为它适用于 RXJava 规范。如果你打破了链条,那么你必须打电话,block()这是不推荐的。您必须按以下方式修改您的代码。让我们假设您有一个处理程序正在调用您的collectEnvironmentData()方法,并且您的方法正在调用远程服务。public&nbsp; Mono<ServerResponse> handelerMethod(ServerRequest request){&nbsp; return collectEnvironmentData().flatMap(aVoid -> ServerResponse.ok().build());}你的方法应该修改为public Mono<Void> collectEnvironmentData() throws JAXBException {ReportRequest report = new ReportRequest();report.setVersion("1.0");RestClient client = null;try {&nbsp; &nbsp; client = RestClientBuilder.builder()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .gatewayUrl(URL2)//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .token(contract.getTerminal_token())//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .usernamePassword("user", "password")//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .truststore(new File("server.pem"))//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .sslCertificate(new File("client.pem"), new File("clientKey.p8"),&nbsp;//"secret").build();} catch (SSLException e) {&nbsp; &nbsp; e.printStackTrace();}return client.executeOnly(report);}按照上述方式改变你的实现,希望它能奏效。

胡子哥哥

我将如何实现你的方法是:public Mono<Void> executeOnly(ReportRequest transaction) {&nbsp; &nbsp; Mono<ReportRequest> transactionMono = Mono.just(transaction);&nbsp; &nbsp; return client.post().uri(gatewayUrl)&nbsp; &nbsp; &nbsp; &nbsp; .accept(MediaType.APPLICATION_XML)&nbsp; &nbsp; &nbsp; &nbsp; .contentType(MediaType.APPLICATION_XML)&nbsp; &nbsp; &nbsp; &nbsp; .body(transaction, ReportRequest.class)&nbsp; &nbsp; &nbsp; &nbsp; .exchange()&nbsp; &nbsp; &nbsp; &nbsp; .then();}然后我会按如下方式使用它:client.executeOnly(report).subscribe()

长风秋雁

将 更改method return type为Mono<Void>进行端到端流式传输。public void collectEnvironmentData() throws JAXBException {&nbsp; &nbsp; ReportRequest report = new ReportRequest();&nbsp; &nbsp; report.setVersion("1.0");&nbsp; &nbsp; RestClient client = null;&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; client = RestClientBuilder.builder()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .gatewayUrl(URL2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .build();&nbsp; &nbsp; } catch (SSLException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }&nbsp; &nbsp; return client.executeOnly(report);}或者您也可以订阅Monoclient.executeOnly(report).subscribe();
随时随地看视频慕课网APP

相关分类

Java
我要回答