Compare commits
No commits in common. "9e0729f6f5bb64793324c0d53440011a12e268b6" and "8eaaa501bcdd86d6ca8d93dfd617929c1386d461" have entirely different histories.
9e0729f6f5
...
8eaaa501bc
|
|
@ -1,31 +1,17 @@
|
|||
package com.lyxh.api.docking.exception;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import lombok.Data;
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.time.Instant;
|
||||
|
||||
@Data
|
||||
public class ErrorResponse {
|
||||
|
||||
private final String code;
|
||||
private final String error;
|
||||
private final String message;
|
||||
private final String responseId;
|
||||
private final DataNode data;
|
||||
private final String timestamp;
|
||||
|
||||
public ErrorResponse(String code, String message) {
|
||||
this.code = code;
|
||||
public ErrorResponse(String error, String message) {
|
||||
this.error = error;
|
||||
this.message = message;
|
||||
this.responseId = DigestUtils.md5Hex(UUID.randomUUID().toString());
|
||||
this.data = new DataNode();
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class DataNode {
|
||||
private final int status = 0;
|
||||
|
||||
@JsonProperty("result")
|
||||
private final Object result = null;
|
||||
this.timestamp = Instant.now().toString();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ public class GlobalExceptionHandler {
|
|||
.reduce((a, b) -> a + "; " + b)
|
||||
.orElse("validation failed");
|
||||
return ResponseEntity.badRequest()
|
||||
.body(new ErrorResponse("400", msg));
|
||||
.body(new ErrorResponse("VALIDATION_ERROR", msg));
|
||||
}
|
||||
|
||||
@ExceptionHandler(ApiCallException.class)
|
||||
|
|
@ -34,13 +34,13 @@ public class GlobalExceptionHandler {
|
|||
String detail = ex.getResponseBody() != null && !ex.getResponseBody().isEmpty()
|
||||
? ex.getResponseBody() : ex.getMessage();
|
||||
return ResponseEntity.status(status)
|
||||
.body(new ErrorResponse(String.valueOf(status.value()), detail));
|
||||
.body(new ErrorResponse(ex.getCode(), detail));
|
||||
}
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
public ResponseEntity<ErrorResponse> handleGeneral(Exception ex) {
|
||||
log.error("Unexpected error", ex);
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
.body(new ErrorResponse("500", "Internal server error"));
|
||||
.body(new ErrorResponse("INTERNAL_ERROR", "Internal server error"));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +1,9 @@
|
|||
package com.lyxh.api.docking.service;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import com.lyxh.api.docking.client.ApiCallResult;
|
||||
import com.lyxh.api.docking.client.GenericApiClient;
|
||||
import com.lyxh.api.docking.entity.ApiCacheData;
|
||||
import com.lyxh.api.docking.entity.ThirdPartyConfig;
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
|
@ -15,7 +11,6 @@ import org.springframework.stereotype.Service;
|
|||
|
||||
import java.time.Duration;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
|
|
@ -37,9 +32,6 @@ public class GenericApiService {
|
|||
@Autowired
|
||||
private ApiInvokeLogService invokeLogService;
|
||||
|
||||
@Autowired
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
/**
|
||||
* 执行第三方 API 调用(含缓存、日志),返回响应体原始 JSON
|
||||
*
|
||||
|
|
@ -73,19 +65,7 @@ public class GenericApiService {
|
|||
ApiCacheData cached = cacheDataService.getNonExpiredCache(apiCode, cacheKey, config.getId());
|
||||
if (cached != null) {
|
||||
log.info("Cache hit: weOrderNo={} apiCode={} cacheKey={}", weOrderNo, apiCode, cacheKey);
|
||||
// 命中缓存,更新 responseId
|
||||
String cacheValue = cached.getCacheValue();
|
||||
try {
|
||||
JsonNode root = objectMapper.readTree(cacheValue);
|
||||
if (root.has("responseId")) {
|
||||
String newResponseId = DigestUtils.md5Hex(UUID.randomUUID().toString());
|
||||
((ObjectNode) root).put("responseId", newResponseId);
|
||||
return objectMapper.writeValueAsString(root);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("Failed to replace responseId in cached response: weOrderNo={}", weOrderNo, e);
|
||||
}
|
||||
return cacheValue;
|
||||
return cached.getCacheValue();
|
||||
}
|
||||
log.info("Cache miss: weOrderNo={} apiCode={} cacheKey={}", weOrderNo, apiCode, cacheKey);
|
||||
}
|
||||
|
|
|
|||
24
对接接口文档.md
24
对接接口文档.md
|
|
@ -105,26 +105,22 @@
|
|||
|
||||
## 错误响应
|
||||
|
||||
错误响应与三方接口响应保持相同结构,其中`data` 中 `status=0`、`result=null`:
|
||||
统一错误格式:
|
||||
|
||||
```json
|
||||
{
|
||||
"code": "503",
|
||||
"message": "External API unreachable after 1 attempts",
|
||||
"responseId": "a1b2c3d4e5f6789012345678abcdef01",
|
||||
"data": {
|
||||
"status": 0,
|
||||
"result": null
|
||||
}
|
||||
"error": "ERROR_CODE",
|
||||
"message": "错误描述",
|
||||
"timestamp": "2026-07-08T10:30:00Z"
|
||||
}
|
||||
```
|
||||
|
||||
| HTTP状态码 | code | 说明 |
|
||||
|---|---|---|
|
||||
| 400 | 400 | 请求参数校验失败 |
|
||||
| 502 | 502 | 三方 API HTTP 错误 |
|
||||
| 503 | 503 | 三方 API 网络不可达 |
|
||||
| 500 | 500 | 服务内部未知异常 |
|
||||
| HTTP状态码 | error | 说明 |
|
||||
|---|---|------------------|
|
||||
| 400 | VALIDATION_ERROR | 请求参数校验失败 |
|
||||
| 502 | HTTP_ERROR | 三方 API HTTP 错误 |
|
||||
| 503 | NETWORK_ERROR | 三方 API 网络不可达 |
|
||||
| 500 | INTERNAL_ERROR | 服务内部未知异常 api配置信息未找到 |
|
||||
|
||||
## 完整调用示例
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue