catch webClient error

This commit is contained in:
2025-07-23 09:12:14 +08:00
parent 81acee7ce0
commit 8528f173a6
2 changed files with 16 additions and 5 deletions

View File

@@ -11,6 +11,7 @@ import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient; import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@@ -46,7 +47,8 @@ public class ApiService {
public <T> Mono<List<T>> getInfoPakistan(GetInfoVO getInfoVO) { public <T> Mono<List<T>> getInfoPakistan(GetInfoVO getInfoVO) {
ParameterizedTypeReference<List<T>> typeRef = (ParameterizedTypeReference<List<T>>) tableObjectMapping.get(getInfoVO.getTableName()); ParameterizedTypeReference<List<T>> typeRef = (ParameterizedTypeReference<List<T>>) tableObjectMapping.get(getInfoVO.getTableName());
if (typeRef == null) { if (typeRef == null) {
throw new IllegalArgumentException("不支持该表名: " + getInfoVO.getTableName()); // throw new IllegalArgumentException("不支持该表名: " + getInfoVO.getTableName());
return Mono.empty();
} }
return webClientBuilder return webClientBuilder
@@ -58,7 +60,12 @@ public class ApiService {
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.bodyValue(getInfoVO) .bodyValue(getInfoVO)
.retrieve() .retrieve()
.bodyToMono(typeRef); .bodyToMono(typeRef)
.onErrorResume(e -> {
log.warn("WebClient 请求 eldData 失败,错误: {}", e.getMessage());
return Mono.just(Collections.emptyList());
});
} }
// Pro API // Pro API
@@ -79,6 +86,10 @@ public class ApiService {
.build()) .build())
.header("eldKey", "435e833ca0610442d25b8011475c8352") .header("eldKey", "435e833ca0610442d25b8011475c8352")
.retrieve() .retrieve()
.bodyToMono(typeRef); .bodyToMono(typeRef)
.onErrorResume(e -> {
log.warn("WebClient 请求 eldData 失败,错误: {}", e.getMessage());
return Mono.just(Collections.emptyList());
});
} }
} }

View File

@@ -106,11 +106,11 @@ public class CompareService {
List<String> valueFields = getValueField(tableName); List<String> valueFields = getValueField(tableName);
Map<String, Map<String, Object>> pakistanMap = pakistanList.stream() Map<String, Map<String, Object>> pakistanMap = pakistanList.stream()
.collect(Collectors.toMap(item -> getValue(item, valueFields), item -> item,(existing, replacement) -> existing)); .collect(Collectors.toMap(item -> getValue(item, valueFields), item -> item, (existing, replacement) -> existing));
log.info("customerId = {} - tableName = {} - pakistanSet = {}", customerId, tableName, pakistanMap.keySet()); log.info("customerId = {} - tableName = {} - pakistanSet = {}", customerId, tableName, pakistanMap.keySet());
Map<String, Map<String, Object>> proMap = proList.stream() Map<String, Map<String, Object>> proMap = proList.stream()
.collect(Collectors.toMap(item -> getValue(item, valueFields), item -> item,(existing, replacement) -> existing)); .collect(Collectors.toMap(item -> getValue(item, valueFields), item -> item, (existing, replacement) -> existing));
log.info("customerId = {} - tableName = {} - proSet = {}", customerId, tableName, proMap.keySet()); log.info("customerId = {} - tableName = {} - proSet = {}", customerId, tableName, proMap.keySet());
// 合并所有 key // 合并所有 key