Spring中调用http的方式WebClient

在Spring5以前调用http 接口可以

可以选用RestTemplate类,在Spring5以后,在Spring-webflux包中有一个更为合适的类型,叫做WebClient,能做到自己封装同步调用和异步调用,下面献上我自己的WebClient的配置和使用。

WebClient配置:

    @Test
    public void userSelect() throws SSLException {
        WebClient webClient = webClient();
        Mono mono = webClient.get()
//                .uri("user/1000002?criteria=USERID")
                .uri("user/1000002")
                .headers(httpHeaders -> {
                    httpHeaders.add("id", "1000002");
                    httpHeaders.add("clientId","HC6B0162");
                })
                .retrieve()
                .bodyToMono(String.class);

        System.out.println(mono.block());
    }

 

你可能感兴趣的:(spring应用)