对接知识库使用okhttp客户端
This commit is contained in:
parent
657cfc0915
commit
d3149c3ca9
|
@ -38,6 +38,7 @@
|
|||
<javacv.version>1.5.7</javacv.version>
|
||||
<system.linux-x86_64>linux-x86_64</system.linux-x86_64>
|
||||
<ffmpeg.version>5.0</ffmpeg.version>
|
||||
<okhttp.version>4.9.3</okhttp.version>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
|
@ -248,6 +249,14 @@
|
|||
<version>${ehcache.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp -->
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>okhttp</artifactId>
|
||||
<version>${okhttp.version}</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- javacv+javacpp核心库-->
|
||||
<dependency>
|
||||
<groupId>org.bytedeco</groupId>
|
||||
|
|
|
@ -17,6 +17,10 @@ import io.swagger.annotations.Api;
|
|||
import io.swagger.annotations.ApiImplicitParam;
|
||||
import io.swagger.annotations.ApiImplicitParams;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import okhttp3.FormBody;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -274,36 +278,45 @@ public class ResourceController {
|
|||
@GetMapping("knowledgeBase")
|
||||
@ApiOperation("对接知识库数据")
|
||||
public Result<String> knowledgeBase() {
|
||||
long timestamp = new Date().getTime();
|
||||
final MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<>();
|
||||
paramMap.add("app_id", appId);
|
||||
paramMap.add("interface_id", methodId);
|
||||
paramMap.add("version", version);
|
||||
paramMap.add("charset", charset);
|
||||
paramMap.add("timestamp", String.valueOf(timestamp));
|
||||
paramMap.add("origin", origin);
|
||||
|
||||
// final MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<>();
|
||||
// paramMap.add("app_id", appId);
|
||||
// paramMap.add("interface_id", methodId);
|
||||
// paramMap.add("version", version);
|
||||
// paramMap.add("charset", charset);
|
||||
// paramMap.add("timestamp", String.valueOf(timestamp));
|
||||
// paramMap.add("origin", origin);
|
||||
int pageSize = 100;
|
||||
AtomicInteger pageIndex = new AtomicInteger(1);
|
||||
Arrays.stream(catalogIds).map(index -> {
|
||||
List<CompletableFuture> task = new ArrayList<>();
|
||||
AtomicBoolean end = new AtomicBoolean(true);
|
||||
AtomicBoolean end = new AtomicBoolean(false);
|
||||
do {
|
||||
paramMap.remove("sign");
|
||||
paramMap.remove("biz_content");
|
||||
RestTemplate restTemplate1 = new RestTemplate();
|
||||
final MultiValueMap<String, Object> paramMapIndex = new LinkedMultiValueMap<>();
|
||||
int page = pageIndex.getAndIncrement();
|
||||
task.add(CompletableFuture.supplyAsync(() -> {
|
||||
long timestamp = new Date().getTime();
|
||||
final JSONObject bizContentParam = new JSONObject();
|
||||
OkHttpClient client = new OkHttpClient();
|
||||
bizContentParam.put("appkey", appKey);
|
||||
bizContentParam.put("catalogId", index);
|
||||
bizContentParam.put("pageIndex", page);
|
||||
bizContentParam.put("pageSize", pageSize);
|
||||
String bizContent_ = bizContentParam.toJSONString();
|
||||
paramMapIndex.add("biz_content", bizContent_);
|
||||
logger.error(index + "分页{}对接知识库数据请求参数:" + JSON.toJSONString(paramMapIndex), page);
|
||||
// 通过FormBody对象构建Builder来添加表单参数
|
||||
final FormBody.Builder signFormBody = new FormBody.Builder()
|
||||
.add("app_id", appId)
|
||||
.add("interface_id", methodId)
|
||||
.add("version", version)
|
||||
.add("timestamp", String.valueOf(timestamp))
|
||||
.add("origin", origin)
|
||||
.add("biz_content", bizContent_);
|
||||
logger.error(index + "分页{}对接知识库数据请求参数:" + signFormBody.build().contentType().toString(), page);
|
||||
|
||||
Request signRequest = new Request.Builder().url(sign).post(signFormBody.build()).build();
|
||||
try {
|
||||
String signResult = restTemplate1.postForObject(sign, paramMapIndex, String.class);
|
||||
Response signResponse =
|
||||
client.newCall(signRequest).execute();
|
||||
String signResult = signResponse.body().string();
|
||||
logger.error("{}分页signResult数据:" + signResult, page);
|
||||
JSONObject signJsonObject = JSON.parseObject(signResult);
|
||||
if (!signJsonObject.containsKey("data")) {
|
||||
|
@ -318,8 +331,11 @@ public class ResourceController {
|
|||
}
|
||||
Map<String, Object> signData = (Map<String, Object>) signJsonObject.get("data");
|
||||
String signString = signData.get("sign").toString();
|
||||
paramMapIndex.add("sign", signString);
|
||||
String gatewayResult = restTemplate1.postForObject(gateway, paramMapIndex, String.class);
|
||||
|
||||
signFormBody.add("sign", signString);
|
||||
Request gatewayRequest = new Request.Builder().url(sign).post(signFormBody.build()).build();
|
||||
Response gatewayResponse = client.newCall(gatewayRequest).execute();
|
||||
String gatewayResult = gatewayResponse.body().string();
|
||||
logger.error("{}分页数据:" + gatewayResult, page);
|
||||
JSONObject gatewayJsonObject = JSON.parseObject(gatewayResult);
|
||||
JSONObject gatewayData = JSON.parseObject(gatewayJsonObject.get("data").toString());
|
||||
|
@ -334,8 +350,6 @@ public class ResourceController {
|
|||
logger.error("获取异常:", e);
|
||||
end.set(false);
|
||||
return new JSONArray();
|
||||
} finally {
|
||||
paramMapIndex.clear();
|
||||
}
|
||||
}).thenAcceptAsync(list -> {
|
||||
logger.info("知识库数据量:{}", list.size());
|
||||
|
|
Loading…
Reference in New Issue