调用外部的接口 返回的内容大致为:{ "code": "0", "messages": "成功", "content": { "dqfpdm": "123456789011", "dqfphm": "1122334455" } } 后端使用Map<String, Object>接收响应内容,因为不能把外部接口返回的内容直接返回给前端,所以要怎么写一个去解析,响应的是成功还是失败,成功再把需要的值 返回给前端,不同的外部接口 content 值不一样,要怎么操作 写一个统用的
时间: 2023-08-13 13:03:31 浏览: 88
为了解析外部接口的响应内容并确定成功与否,你可以编写一个通用的方法来处理不同的 `content` 值。以下是一个示例实现:
```java
public Map<String, Object> parseApiResponse(Map<String, Object> response) {
String code = (String) response.get("code");
if ("0".equals(code)) {
// 成功
Map<String, Object> content = (Map<String, Object>) response.get("content");
return extractValuesFromContent(content);
} else {
// 失败
String errorMessage = (String) response.get("messages");
return handleFailure(errorMessage);
}
}
public Map<String, Object> extractValuesFromContent(Map<String, Object> content) {
// 根据不同的外部接口的 content 结构,从中提取需要的值
String dqfpdm = (String) content.get("dqfpdm");
String dqfphm = (String) content.get("dqfphm");
Map<String, Object> extractedValues = new HashMap<>();
extractedValues.put("dqfpdm", dqfpdm);
extractedValues.put("dqfphm", dqfphm);
return extractedValues;
}
public Map<String, Object> handleFailure(String errorMessage) {
// 处理失败情况,例如记录日志、返回错误信息等
// ...
return null; // 根据实际需求返回失败结果或者抛出异常
}
```
然后,在你的服务类中调用 `parseApiResponse` 方法来解析外部接口的响应,并返回需要的值给前端:
```java
@Override
public Map<String, Object> callInquiryUnusedInvoice(String kpzdbs, String fplxdm) {
Map<String, Object> contentObj = new HashMap<>() ;
contentObj.put("kpzdbs", kpzdbs);
contentObj.put("fplxdm", fplxdm);
BaiWangBaseRequestDto baiWangBaseRequestDto = callInterface(contentObj, BaiWangConstant.INQUIRY_UNUSED_INVOICE, baiWangProperties.getUnusedInvoicePath());
Map<String, Object> response = baiWangClient.callEnterpriseName(baiWangBaseRequestDto);
// 解析响应内容
Map<String, Object> parsedResponse = parseApiResponse(response);
return parsedResponse;
}
```
通过使用 `parseApiResponse` 方法,你可以根据不同的外部接口的 `content` 结构来提取需要的值,并根据成功或失败的情况进行相应的处理。这样你就能够在统一的方法中处理不同接口的返回内容。请根据实际情况进行调整和扩展。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)