Stories

Detail Return Return

SpringCloud-WebClient使用Get和Post請求 - Stories Detail

前言

這是一篇關於SpringCloud中使用webclient調用微服務的總結性文章,使用期間遇到好多問題及其解決方法,希望幫助到大家。

準備

首先我們在SpringCloud項目中配置WebClient,如下所示:

@Component
public class WebClientConfig {
    @Bean
    @LoadBalanced
    public WebClient.Builder webClientBuilder() {
        return WebClient.builder();
    }
}

然後在方法中引用:

@Resource
private WebClient.Builder webClientBuilder;

下面來説明一下如何調用?

操作

主要圍繞get和post請求來請求微服務接口數據,如下:
1、Get請求

 public Mono<FileShare> getSharedFriends(String fileId, LoginUser loginUser) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            String userJson = mapper.writeValueAsString(loginUser);
            WebClient webClient = webClientBuilder.baseUrl("http://space-service").build();

            return webClient.get()
                    .uri(uriBuilder -> uriBuilder
                            // 注意:path中不能添加http://space-service,應該在上面添加
                            .path("/crud/file/getSharedFriends")
                            .queryParam("fileId", fileId)
                            .build())
                    // 將用户信息傳遞給下游接口
                    .header(UserContext.USER, userJson)
                    .retrieve()
                    .bodyToMono(new ParameterizedTypeReference<ResultSuccess<Object>>() {})
                    .flatMap(resultSuccess -> {
                        log.info("resultSuccess={}", JSONArray.toJSONString(resultSuccess));
                        if (resultSuccess == null || resultSuccess.getData() == null) {
                            log.error("Received null data from server");
                            return Mono.empty();  // 避免 NullPointerException
                        }
                        log.info("resultSuccess.getData()={}", resultSuccess.getData());

                        ObjectMapper objectMapper = new ObjectMapper(); // Jackson ObjectMapper
                        FileShare fileShare = objectMapper.convertValue(resultSuccess.getData(), FileShare.class);

                        return Mono.just(fileShare);
                    })
                    .onErrorResume(e -> {
                        log.error("Error retrieving FileShare: {}", e.getMessage());
                        return Mono.empty();
                    });
        } catch (Exception ex) {
            log.info("getSharedFriends Exception ={}", ex.getMessage());
            return Mono.empty();
        }
    }

解釋一下上面的代碼:

  1. 我們在header中添加請求頭包含用户信息數據傳遞給下游接口
  2. webClientBuilder.baseUrl中設置服務地址
  3. 使用bodyToMono轉換返回來的數據,當然你可以寫自己的類型或者String.class
  4. flatMap處理轉換好的數據並返回
  5. 如果出現異常使用onErrorResume來處理
  6. queryParam添加?請求參數

然後我們可以處理接口返回來的數據

Mono<FileShare> fs = spaceWebClient.getSharedFriends(fileId, loginUser);
            return fs.switchIfEmpty(Mono.defer(() -> {
                        // 返回一個空的 FileShare 對象,以保持類型一致
                        FileShare emptyFileShare = new FileShare();  // 或者根據你的需求設置合適的默認值
                        return Mono.just(emptyFileShare);  // 返回類型為 Mono<FileShare>
                    })
            ).flatMap(fileShare -> {
                log.info("fileShare = {}", fileShare);
                List<String> uids = new ArrayList<>();
                List<User> user;
                uids.add(loginUser.getUid());
                if (fileShare == null || fileShare.getFriendIds() == null || fileShare.getFriendIds().isEmpty()) {
                    user = userService.getUserByName(uids, userName, 5);
                    return Mono.just(ResponseEntity.ok(new ResultSuccess<>(user)));
                } else {
                    uids.addAll(fileShare.getFriendIds());
                    user = userService.getUserByName(uids, userName, 5);
                }
                return Mono.just(ResponseEntity.ok(new ResultSuccess<>(user)));
            });

注意:如果webclient方法中返回Mono.empty(),則不會進入flatMap方法中,所以我們在switchIfEmpty方法中默認設置一個值

上面的flatMap處理你返回的接口數據,這樣就完成了Get請求示例,下面看看Post請求。

2、Post請求
跟Get一樣,代碼如下:

public Mono<List<NotifyRemind>> queryNotifyBy(LoginUser loginUser, String senderId, String objId, List<String> recipientIds) {
        try {
            ObjectMapper mapper = new ObjectMapper();

            NotifyRemindRequest  notifyRemindRequest = new NotifyRemindRequest();
            notifyRemindRequest.setSenderId(senderId);
            notifyRemindRequest.setObjectId(objId);
            notifyRemindRequest.setRecipientIds(recipientIds);

            String userJson = mapper.writeValueAsString(loginUser);
            WebClient webClient = webClientBuilder.baseUrl("http://notify-service").build();

            return webClient.post()
                    .uri(uriBuilder -> uriBuilder
                            // 注意:path中不能添加http://space-service,應該在上面添加
                            .path("/crud/remind/queryBy")
                            .build())
                    .bodyValue(notifyRemindRequest)
                    // 將用户信息傳遞給下游接口
                    .header(UserContext.USER, userJson)
                    .retrieve()
                    .bodyToMono(new ParameterizedTypeReference<ResultInfo<Object>>() {})
                    .flatMap(resultInfo -> {
                        log.info("resultSuccess={}", JSONArray.toJSONString(resultInfo));
                        if (resultInfo == null || resultInfo.getData() == null) {
                            List<NotifyRemind> empty = new ArrayList<>();
                            log.error("Received null data from server");
                            return Mono.just(empty);  // 避免 NullPointerException
                        }
                        ObjectMapper objectMapper = new ObjectMapper(); // Jackson ObjectMapper
                        // 使用 TypeReference 來指定目標類型
                        List<NotifyRemind> notifyReminds = objectMapper.convertValue(
                                //注意:不要使用List.class,因為返回的是List<LinkedHashMap>,改成:new TypeReference<>() {}
                                resultInfo.getData(), new TypeReference<>() {});
                        return Mono.just(notifyReminds);
                    })
                    .onErrorResume(e -> {
                        log.error("Error retrieving FileShare: {}", e.getMessage());
                        return Mono.empty();
                    });
        } catch (Exception ex) {
            log.info("getSharedFriends Exception ={}", ex.getMessage());
            return Mono.empty();
        }
    }

除了bodyValue添加請求參數類,其它的跟Get請求類似,不過有個注意點:

objectMapper.convertValue轉換成自己想要的List<NotifyRemind>類型時

請使用:

objectMapper.convertValue(resultInfo.getData(), new TypeReference<>() {})

不要使用:

objectMapper.convertValue(resultInfo.getData(), List.class)

接着我們在Controller層來接收下返回的接口數據:

Mono<List<NotifyRemind>> listMono = notifyWebClient.queryNotifyBy(loginUser, loginUser.getUid(), fileId, fids);

listMono.subscribe(res -> {
    log.info("result:{}", res);
    if (res.isEmpty()) {
        for (String fid : fids) {
            sendNotify(loginUser, file, fid);
        }
    } else {
        // 找出 fids 中不存在於 notifyRemind.id 的值
        List<String> missingIds = fids.stream()
                .filter(fid -> res.stream().noneMatch(recipient -> recipient.getRecipientId().equals(fid)))
                .collect(Collectors.toList());
        for (String fid : missingIds) {
            sendNotify(loginUser, file, fid);
        }
    }
});

總結

1、Mono如果你沒使用的話則它不會請求接口,如:

Mono<FileShare> fs = spaceWebClient.getSharedFriends(fileId, loginUser);

這段代碼它不會請求接口,只有加上上面的.flatMap才會正常請求
2、不要把WebClient的請求放到循環中,如while和for
3、path是設置路徑,服務名需要在webClientBuilder.baseUrl中設置
4、因為我不想返回數據,所以使用.subscribe方法來接收

引用

Spring Webflux webclient出現問題,嘗試發送post請求時沒有任何反應

user avatar u_16297326 Avatar journey_64224c9377fd5 Avatar AmbitionGarden Avatar lvlaotou Avatar jkdataapi Avatar lenve Avatar devlive Avatar dengjijie Avatar javaedge Avatar swifter Avatar jeecg Avatar changlina Avatar
Favorites 22 users favorite the story!
Favorites

Add a new Comments

Some HTML is okay.