webclient post-call从未在intellij中收到响应

fdx2calv  于 2021-07-11  发布在  Java
关注(0)|答案(1)|浏览(345)

我已经在java应用程序中编写了一个webclient示例。我已经创建了一个ssl上下文,用于我尝试访问的服务器,以及基本身份验证。我正在请求休息。起初,我怀疑是否正确创建了ssl上下文,或者是否错误地添加了基本身份验证。然而,在我看来,我的post请求根本没有到达端点,因为我既不能打印响应主体,也不能打印响应头或状态代码。这使我认为有一些与我的代码无关的重要内容,可能是某种配置,它会阻止我的应用程序正确地发送请求并命中端点。下面是我当前的代码。

@Configuration
@SpringBootApplication
public class myApplication {

    public static SslContext getClientSslContext() throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException {
        //log.info("Creating SslContext for webClientBuilder");
        KeyStore trustStore = KeyStore.getInstance("jks");
        trustStore.load(new FileInputStream("myPath"),
                ("myPassword").toCharArray());
        TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(trustStore);
        KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        KeyStore keyStore = KeyStore.getInstance("jks");
        keyStore.load(new FileInputStream("myPath"),
                ("myPassword").toCharArray());
        keyManagerFactory.init(keyStore, ("myPassword").toCharArray());
        //log.info("Created SslContext for webClientBuilder");
        return SslContextBuilder.forClient()
                .keyManager(keyManagerFactory)
                .trustManager(trustManagerFactory)
                .build();
    }

    static Mono<ClientResponse> createBackUpRequest() throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException {
        Gson gson = new Gson();
        //JSONObject reqBodyJson = new JSONObject();
        LinkedHashMap<String, Object> reqBody
                = new LinkedHashMap<String, Object>();
        LinkedHashMap<String, String> variables
                = new LinkedHashMap<String, String>();

        reqBody.put("variables", variables);

        System.out.println(gson.toJson(reqBody));

        SslContext sslContext = getClientSslContext();
        HttpClient httpClient = HttpClient.create().secure(sslSpec -> sslSpec.sslContext(sslContext));

        WebClient webClient = WebClient.builder()
                .clientConnector(new ReactorClientHttpConnector(httpClient))
                //.baseUrl("host")
                .defaultHeaders(HttpHeaders -> HttpHeaders.setBasicAuth("myUser", "myPassword"))
                .defaultHeader(HttpHeaders.USER_AGENT, "Spring 5 WebClient")
                .defaultHeader(HttpHeaders.ACCEPT, "application/json")
                .defaultHeader(HttpHeaders.CONTENT_TYPE, "application/json")
                .build();

        return webClient.post()
                .uri("host")
                .body(Mono.just(reqBody), LinkedHashMap.class)//if directly putting the map doesn't work
                .exchange()
                .doOnSuccess(clientResponse -> System.out.println("clientResponse.headers() = " + clientResponse.headers()))
                .doOnSuccess(clientResponse -> System.out.println("clientResponse.statusCode() = " + clientResponse.statusCode()));
    }

    public static void main(String[] args) throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException {
        SpringApplication.run(QaAutomatedBackupApplication.class, args);
        System.out.println("print out");
        createBackUpRequest();
        System.out.println("second print out");
    }
}
6tr1vspr

6tr1vspr1#

问题是你从不订阅React流。
在你订阅之前什么都不会发生。你做了什么 createBackUpRequest 就是制定一个计划( Mono )如何获得 ClientResponse 数据。
将主方法更改为

public static void main(String[] args) throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException {
        SpringApplication.run(QaAutomatedBackupApplication.class, args);
        System.out.println("print out");
        createBackUpRequest().subscribe(response -> 
            System.out.println("second print out")
        );
    }

然后它将实际发出web请求。否则这个计划就永远不会执行。请记住,如果您的springboot应用程序不是springweb应用程序(它将继续在其他线程上运行,侦听web请求),那么它将在等待流完成之前立即退出。
如果是这样,你只想测试和实验,你可以把一个 Thread.sleep(10000) 最后,以便在应用程序关闭之前有时间执行web请求。但很明显,这不是一个很好的现实世界的实现。

相关问题