fix: 修复第三方素材API测试连接逻辑,400业务错误也视为连接成功

This commit is contained in:
Your Name 2026-01-07 14:27:55 +08:00
parent 7936451d61
commit c87c646d4d

View File

@ -130,13 +130,16 @@ public class ExternalAssetService {
String apiKey = systemConfigService.getConfigValue("external_asset_api_key");
if (apiUrl == null || apiUrl.isEmpty() || apiKey == null || apiKey.isEmpty()) {
log.warn("第三方素材API配置不完整");
return false;
}
try {
// 测试连接(使用一个测试任务号)
// 测试连接(使用正确格式的测试任务号)
String url = apiUrl.endsWith("/") ? apiUrl : apiUrl + "/";
url = url + "product-assets?taskNo=TEST000000000000";
url = url + "product-assets?taskNo=TASK000000000000";
log.info("测试第三方素材API连接: {}", url);
HttpHeaders headers = new HttpHeaders();
headers.set("X-API-Key", apiKey);
@ -150,12 +153,42 @@ public class ExternalAssetService {
String.class
);
log.info("测试连接响应状态: {}", response.getStatusCode());
// 只要能收到响应(无论是404还是200),说明连接正常
return response.getStatusCode().is2xxSuccessful() ||
response.getStatusCode() == HttpStatus.NOT_FOUND;
} catch (org.springframework.web.client.HttpClientErrorException e) {
// HTTP 4xx 错误
log.info("测试连接HTTP错误: {} - {}", e.getStatusCode(), e.getResponseBodyAsString());
// 400 Bad Request 但是包含业务错误码(如TASK_NOT_FOUND, INVALID_TASK_NO)
// 说明API连接正常,只是参数不对
if (e.getStatusCode() == HttpStatus.BAD_REQUEST) {
String body = e.getResponseBodyAsString();
if (body.contains("TASK_NOT_FOUND") || body.contains("INVALID_TASK_NO")) {
log.info("API连接正常,测试任务号不存在或格式不对(这是正常的)");
return true;
}
}
// 404 Not Found 也说明连接正常
if (e.getStatusCode() == HttpStatus.NOT_FOUND) {
log.info("API连接正常,任务不存在(这是正常的)");
return true;
}
// 401/403 认证错误
if (e.getStatusCode() == HttpStatus.UNAUTHORIZED ||
e.getStatusCode() == HttpStatus.FORBIDDEN) {
log.error("API密钥无效或权限不足");
return false;
}
return false;
} catch (Exception e) {
log.error("测试第三方素材API连接失败", e);
log.error("测试第三方素材API连接失败: {}", e.getMessage());
return false;
}
}