From d3149c3ca9d60fed98e3066e65cac884b7318e84 Mon Sep 17 00:00:00 2001 From: "DESKTOP-VA9NQUP\\liwen" Date: Sat, 11 Jun 2022 19:05:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9=E6=8E=A5=E7=9F=A5=E8=AF=86=E5=BA=93?= =?UTF-8?q?=E4=BD=BF=E7=94=A8okhttp=E5=AE=A2=E6=88=B7=E7=AB=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- renren-admin/pom.xml | 9 ++++ .../controller/ResourceController.java | 54 ++++++++++++------- 2 files changed, 43 insertions(+), 20 deletions(-) diff --git a/renren-admin/pom.xml b/renren-admin/pom.xml index 2c5300ba..5a032808 100644 --- a/renren-admin/pom.xml +++ b/renren-admin/pom.xml @@ -38,6 +38,7 @@ 1.5.7 linux-x86_64 5.0 + 4.9.3 1.8 @@ -248,6 +249,14 @@ ${ehcache.version} + + + com.squareup.okhttp3 + okhttp + ${okhttp.version} + + + org.bytedeco diff --git a/renren-admin/src/main/java/io/renren/modules/resource/controller/ResourceController.java b/renren-admin/src/main/java/io/renren/modules/resource/controller/ResourceController.java index 85366e3a..c5fc4d12 100644 --- a/renren-admin/src/main/java/io/renren/modules/resource/controller/ResourceController.java +++ b/renren-admin/src/main/java/io/renren/modules/resource/controller/ResourceController.java @@ -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 knowledgeBase() { - long timestamp = new Date().getTime(); - final MultiValueMap 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 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 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 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 signData = (Map) 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());