FunAsrManager.java
4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package com.bs.reception.manager;
import com.alibaba.fastjson.JSONObject;
import com.bs.fileservice.feign.RemoteFileService;
import com.bs.reception.model.aliasr.BaseResponse;
import com.bs.reception.model.aliasr.CreateTaskResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@Service
public class FunAsrManager {
@Autowired
private Environment environment;
@Autowired
private RemoteFileService remoteFileService;
@Autowired
private RestTemplate restTemplate;
@Value("${funasr.add.task.url}")
private String funasrTaskUrl;
public String createAsrTask(String uploadFile) {
try {
// 1. 准备请求头和请求体
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
Map<String, String> requestBody = new HashMap<>();
requestBody.put("url", uploadFile);
HttpEntity<Map<String, String>> requestEntity = new HttpEntity<>(requestBody, headers);
log.info("开始创建ASR任务,URL: {}, 音频文件: {}", funasrTaskUrl, uploadFile);
// 2. 发送POST请求
ResponseEntity<String> response = restTemplate.exchange(
funasrTaskUrl,
HttpMethod.POST,
requestEntity,
String.class
);
log.info("响应状态码: {}", response.getStatusCode());
log.info("响应体: {}", response.getBody());
// 3. 处理响应
if (response.getStatusCode().is2xxSuccessful() && response.getBody() != null) {
BaseResponse<CreateTaskResp> baseResponse = parseResponse(response.getBody());
if (baseResponse != null && baseResponse.getCode()==200) {
CreateTaskResp taskData = baseResponse.getData();
if (taskData != null && taskData.getTaskId() != null) {
log.info("✅ ASR任务创建成功! 任务ID: {}, 耗时: {}ms",
taskData.getTaskId(),
baseResponse.getTimeConsuming());
return taskData.getTaskId();
} else {
log.error("❌ 任务数据解析失败");
}
} else {
log.error("❌ ASR任务创建失败: 错误码={}, 消息={}",
baseResponse != null ? baseResponse.getCode() : "N/A",
baseResponse != null ? baseResponse.getMsg() : "响应解析失败");
}
} else {
log.error("❌ HTTP请求失败,状态码: {}", response.getStatusCode());
}
} catch (Exception e) {
log.error("❌ 创建ASR任务时发生异常", e);
}
return null;
}
/**
* 解析响应体
*/
private BaseResponse<CreateTaskResp> parseResponse(String responseBody) {
try {
JSONObject json = JSONObject.parseObject(responseBody);
if (json == null) {
return null;
}
BaseResponse<CreateTaskResp> baseResponse = new BaseResponse<>();
baseResponse.setCode(json.getInteger("code"));
baseResponse.setMsg(json.getString("msg"));
baseResponse.setTimeConsuming(json.getLong("time_consuming"));
JSONObject dataJson = json.getJSONObject("data");
if (dataJson != null) {
CreateTaskResp taskResp = new CreateTaskResp();
taskResp.setTaskId(dataJson.getString("task_id"));
baseResponse.setData(taskResp);
}
return baseResponse;
} catch (Exception e) {
log.error("解析响应JSON失败: {}", responseBody, e);
return null;
}
}
}