对接知识库使用okhttp客户端

This commit is contained in:
DESKTOP-VA9NQUP\liwen 2022-06-11 19:05:22 +08:00
parent 657cfc0915
commit d3149c3ca9
2 changed files with 43 additions and 20 deletions

View File

@ -38,6 +38,7 @@
<javacv.version>1.5.7</javacv.version> <javacv.version>1.5.7</javacv.version>
<system.linux-x86_64>linux-x86_64</system.linux-x86_64> <system.linux-x86_64>linux-x86_64</system.linux-x86_64>
<ffmpeg.version>5.0</ffmpeg.version> <ffmpeg.version>5.0</ffmpeg.version>
<okhttp.version>4.9.3</okhttp.version>
<java.version>1.8</java.version> <java.version>1.8</java.version>
</properties> </properties>
@ -248,6 +249,14 @@
<version>${ehcache.version}</version> <version>${ehcache.version}</version>
</dependency> </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核心库--> <!-- javacv+javacpp核心库-->
<dependency> <dependency>
<groupId>org.bytedeco</groupId> <groupId>org.bytedeco</groupId>

View File

@ -17,6 +17,10 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -274,36 +278,45 @@ public class ResourceController {
@GetMapping("knowledgeBase") @GetMapping("knowledgeBase")
@ApiOperation("对接知识库数据") @ApiOperation("对接知识库数据")
public Result<String> knowledgeBase() { public Result<String> knowledgeBase() {
long timestamp = new Date().getTime();
final MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<>(); // final MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<>();
paramMap.add("app_id", appId); // paramMap.add("app_id", appId);
paramMap.add("interface_id", methodId); // paramMap.add("interface_id", methodId);
paramMap.add("version", version); // paramMap.add("version", version);
paramMap.add("charset", charset); // paramMap.add("charset", charset);
paramMap.add("timestamp", String.valueOf(timestamp)); // paramMap.add("timestamp", String.valueOf(timestamp));
paramMap.add("origin", origin); // paramMap.add("origin", origin);
int pageSize = 100; int pageSize = 100;
AtomicInteger pageIndex = new AtomicInteger(1); AtomicInteger pageIndex = new AtomicInteger(1);
Arrays.stream(catalogIds).map(index -> { Arrays.stream(catalogIds).map(index -> {
List<CompletableFuture> task = new ArrayList<>(); List<CompletableFuture> task = new ArrayList<>();
AtomicBoolean end = new AtomicBoolean(true); AtomicBoolean end = new AtomicBoolean(false);
do { do {
paramMap.remove("sign");
paramMap.remove("biz_content");
RestTemplate restTemplate1 = new RestTemplate();
final MultiValueMap<String, Object> paramMapIndex = new LinkedMultiValueMap<>();
int page = pageIndex.getAndIncrement(); int page = pageIndex.getAndIncrement();
task.add(CompletableFuture.supplyAsync(() -> { task.add(CompletableFuture.supplyAsync(() -> {
long timestamp = new Date().getTime();
final JSONObject bizContentParam = new JSONObject(); final JSONObject bizContentParam = new JSONObject();
OkHttpClient client = new OkHttpClient();
bizContentParam.put("appkey", appKey); bizContentParam.put("appkey", appKey);
bizContentParam.put("catalogId", index); bizContentParam.put("catalogId", index);
bizContentParam.put("pageIndex", page); bizContentParam.put("pageIndex", page);
bizContentParam.put("pageSize", pageSize); bizContentParam.put("pageSize", pageSize);
String bizContent_ = bizContentParam.toJSONString(); String bizContent_ = bizContentParam.toJSONString();
paramMapIndex.add("biz_content", bizContent_); // 通过FormBody对象构建Builder来添加表单参数
logger.error(index + "分页{}对接知识库数据请求参数:" + JSON.toJSONString(paramMapIndex), page); 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 { 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); logger.error("{}分页signResult数据" + signResult, page);
JSONObject signJsonObject = JSON.parseObject(signResult); JSONObject signJsonObject = JSON.parseObject(signResult);
if (!signJsonObject.containsKey("data")) { if (!signJsonObject.containsKey("data")) {
@ -318,8 +331,11 @@ public class ResourceController {
} }
Map<String, Object> signData = (Map<String, Object>) signJsonObject.get("data"); Map<String, Object> signData = (Map<String, Object>) signJsonObject.get("data");
String signString = signData.get("sign").toString(); 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); logger.error("{}分页数据:" + gatewayResult, page);
JSONObject gatewayJsonObject = JSON.parseObject(gatewayResult); JSONObject gatewayJsonObject = JSON.parseObject(gatewayResult);
JSONObject gatewayData = JSON.parseObject(gatewayJsonObject.get("data").toString()); JSONObject gatewayData = JSON.parseObject(gatewayJsonObject.get("data").toString());
@ -334,8 +350,6 @@ public class ResourceController {
logger.error("获取异常:", e); logger.error("获取异常:", e);
end.set(false); end.set(false);
return new JSONArray(); return new JSONArray();
} finally {
paramMapIndex.clear();
} }
}).thenAcceptAsync(list -> { }).thenAcceptAsync(list -> {
logger.info("知识库数据量:{}", list.size()); logger.info("知识库数据量:{}", list.size());