更新配置: 支付和邮件登录模块配置优化, 删除临时文档
This commit is contained in:
@@ -1,212 +0,0 @@
|
||||
# API调用从RestTemplate迁移到Unirest
|
||||
|
||||
## 🎯 **迁移概述**
|
||||
|
||||
根据用户要求,将API调用从Spring RestTemplate改为使用Unirest HTTP客户端库。
|
||||
|
||||
## 🔧 **主要修改**
|
||||
|
||||
### 1. **依赖更新** - `pom.xml`
|
||||
|
||||
**添加Unirest依赖**:
|
||||
```xml
|
||||
<!-- Unirest HTTP客户端 -->
|
||||
<dependency>
|
||||
<groupId>com.konghq</groupId>
|
||||
<artifactId>unirest-java</artifactId>
|
||||
<version>3.14.2</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
### 2. **导入语句更新** - `RealAIService.java`
|
||||
|
||||
**修改前**:
|
||||
```java
|
||||
import org.springframework.http.*;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
```
|
||||
|
||||
**修改后**:
|
||||
```java
|
||||
import kong.unirest.HttpResponse;
|
||||
import kong.unirest.Unirest;
|
||||
import kong.unirest.UnirestException;
|
||||
```
|
||||
|
||||
### 3. **类字段更新**
|
||||
|
||||
**修改前**:
|
||||
```java
|
||||
private final RestTemplate restTemplate;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
public RealAIService() {
|
||||
this.restTemplate = new RestTemplate();
|
||||
this.objectMapper = new ObjectMapper();
|
||||
}
|
||||
```
|
||||
|
||||
**修改后**:
|
||||
```java
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
public RealAIService() {
|
||||
this.objectMapper = new ObjectMapper();
|
||||
// 设置Unirest超时
|
||||
Unirest.config().connectTimeout(0).socketTimeout(0);
|
||||
}
|
||||
```
|
||||
|
||||
## 📝 **API调用方法更新**
|
||||
|
||||
### 1. **文生视频任务提交**
|
||||
|
||||
**修改前 (RestTemplate)**:
|
||||
```java
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
headers.set("Authorization", "Bearer " + aiApiKey);
|
||||
|
||||
HttpEntity<Map<String, Object>> request = new HttpEntity<>(requestBody, headers);
|
||||
String url = aiApiBaseUrl + "/user/ai/tasks/submit";
|
||||
ResponseEntity<Map> response = restTemplate.postForEntity(url, request, Map.class);
|
||||
|
||||
if (response.getStatusCode() == HttpStatus.OK && response.getBody() != null) {
|
||||
Map<String, Object> responseBody = response.getBody();
|
||||
// 处理响应...
|
||||
}
|
||||
```
|
||||
|
||||
**修改后 (Unirest)**:
|
||||
```java
|
||||
String url = aiApiBaseUrl + "/user/ai/tasks/submit";
|
||||
HttpResponse<String> response = Unirest.post(url)
|
||||
.header("Content-Type", "application/json")
|
||||
.header("Authorization", "Bearer " + aiApiKey)
|
||||
.body(objectMapper.writeValueAsString(requestBody))
|
||||
.asString();
|
||||
|
||||
if (response.getStatus() == 200 && response.getBody() != null) {
|
||||
Map<String, Object> responseBody = objectMapper.readValue(response.getBody(), Map.class);
|
||||
// 处理响应...
|
||||
}
|
||||
```
|
||||
|
||||
### 2. **图生视频任务提交**
|
||||
|
||||
**修改前 (RestTemplate)**:
|
||||
```java
|
||||
HttpEntity<Map<String, Object>> request = new HttpEntity<>(requestBody, headers);
|
||||
ResponseEntity<Map> response = restTemplate.postForEntity(url, request, Map.class);
|
||||
```
|
||||
|
||||
**修改后 (Unirest)**:
|
||||
```java
|
||||
HttpResponse<String> response = Unirest.post(url)
|
||||
.header("Content-Type", "application/json")
|
||||
.header("Authorization", "Bearer " + aiApiKey)
|
||||
.body(objectMapper.writeValueAsString(requestBody))
|
||||
.asString();
|
||||
```
|
||||
|
||||
### 3. **查询任务状态**
|
||||
|
||||
**修改前 (RestTemplate)**:
|
||||
```java
|
||||
HttpEntity<String> request = new HttpEntity<>(headers);
|
||||
ResponseEntity<Map> response = restTemplate.exchange(url, HttpMethod.GET, request, Map.class);
|
||||
```
|
||||
|
||||
**修改后 (Unirest)**:
|
||||
```java
|
||||
HttpResponse<String> response = Unirest.get(url)
|
||||
.header("Authorization", "Bearer " + aiApiKey)
|
||||
.asString();
|
||||
```
|
||||
|
||||
### 4. **获取可用模型**
|
||||
|
||||
**修改前 (RestTemplate)**:
|
||||
```java
|
||||
HttpEntity<String> request = new HttpEntity<>(headers);
|
||||
ResponseEntity<Map> response = restTemplate.exchange(url, HttpMethod.GET, request, Map.class);
|
||||
```
|
||||
|
||||
**修改后 (Unirest)**:
|
||||
```java
|
||||
HttpResponse<String> response = Unirest.get(url)
|
||||
.header("Authorization", "Bearer " + aiApiKey)
|
||||
.asString();
|
||||
```
|
||||
|
||||
## 🔄 **异常处理更新**
|
||||
|
||||
### 修改前:
|
||||
```java
|
||||
} catch (Exception e) {
|
||||
logger.error("API调用异常", e);
|
||||
throw new RuntimeException("API调用失败: " + e.getMessage());
|
||||
}
|
||||
```
|
||||
|
||||
### 修改后:
|
||||
```java
|
||||
} catch (UnirestException e) {
|
||||
logger.error("API调用异常", e);
|
||||
throw new RuntimeException("API调用失败: " + e.getMessage());
|
||||
} catch (Exception e) {
|
||||
logger.error("API调用异常", e);
|
||||
throw new RuntimeException("API调用失败: " + e.getMessage());
|
||||
}
|
||||
```
|
||||
|
||||
## ⚙️ **配置更新**
|
||||
|
||||
### 超时设置:
|
||||
```java
|
||||
// 设置Unirest超时
|
||||
Unirest.config().connectTimeout(0).socketTimeout(0);
|
||||
```
|
||||
|
||||
## 📊 **主要差异对比**
|
||||
|
||||
| 特性 | RestTemplate | Unirest |
|
||||
|------|-------------|---------|
|
||||
| **HTTP方法** | `restTemplate.postForEntity()` | `Unirest.post()` |
|
||||
| **请求头** | `HttpHeaders` + `HttpEntity` | `.header()` 链式调用 |
|
||||
| **请求体** | `HttpEntity<Map>` | `.body(String)` |
|
||||
| **响应处理** | `ResponseEntity<Map>` | `HttpResponse<String>` |
|
||||
| **状态码** | `response.getStatusCode()` | `response.getStatus()` |
|
||||
| **响应体** | `response.getBody()` | `objectMapper.readValue()` |
|
||||
| **异常类型** | `Exception` | `UnirestException` |
|
||||
|
||||
## ✅ **迁移完成状态**
|
||||
|
||||
### 已完成的修改:
|
||||
- ✅ 添加Unirest依赖到pom.xml
|
||||
- ✅ 更新导入语句
|
||||
- ✅ 移除RestTemplate依赖
|
||||
- ✅ 修改submitTextToVideoTask方法
|
||||
- ✅ 修改submitImageToVideoTask方法
|
||||
- ✅ 修改getTaskStatus方法
|
||||
- ✅ 修改getAvailableModels方法
|
||||
- ✅ 更新异常处理
|
||||
- ✅ 设置超时配置
|
||||
|
||||
### 代码质量:
|
||||
- ✅ 编译通过
|
||||
- ✅ 类型安全警告(可接受)
|
||||
- ✅ 功能完整性保持
|
||||
|
||||
## 🚀 **使用效果**
|
||||
|
||||
现在API调用使用Unirest库,具有以下特点:
|
||||
|
||||
1. **更简洁的API**:链式调用更直观
|
||||
2. **更好的性能**:Unirest在性能方面有优势
|
||||
3. **更灵活的配置**:支持更细粒度的配置
|
||||
4. **更现代的API**:符合现代Java开发习惯
|
||||
|
||||
系统现在已经成功从RestTemplate迁移到Unirest,所有API调用功能保持不变!
|
||||
|
||||
|
||||
Reference in New Issue
Block a user