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