From 790d112ceade0a034e3d3d2b2f054e757288eae9 Mon Sep 17 00:00:00 2001 From: zhouwentao <1577701412@qq.com> Date: Mon, 31 Jul 2023 16:12:15 +0800 Subject: [PATCH] updates --- .../java/org/jeecg/common/util/RestUtil.java | 27 +++++++++++ .../org/jeecg/config/RestTemplateConfig.java | 29 ++++++++++++ .../modules/api/controller/ApiController.java | 46 +++++++++---------- 3 files changed, 79 insertions(+), 23 deletions(-) diff --git a/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/RestUtil.java b/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/RestUtil.java index 2176357..2be9a4c 100644 --- a/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/RestUtil.java +++ b/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/RestUtil.java @@ -3,12 +3,18 @@ package org.jeecg.common.util; import com.alibaba.fastjson.JSONObject; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; +import org.jeecg.config.RestTemplateConfig; import org.springframework.http.*; import org.springframework.http.client.SimpleClientHttpRequestFactory; import org.springframework.http.converter.StringHttpMessageConverter; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; import java.nio.charset.StandardCharsets; +import java.security.KeyManagementException; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; import java.util.Iterator; import java.util.Map; @@ -260,4 +266,25 @@ public class RestUtil { return urlVariables.substring(1); } + public static JSONObject postTest(String httpUrl,JSONObject requestBodyJson,JSONObject headerJson){ + RestTemplate restTemplate = null; + try { + restTemplate = new RestTemplate(RestTemplateConfig.generateHttpRequestFactory()); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } catch (KeyManagementException e) { + e.printStackTrace(); + } catch (KeyStoreException e) { + e.printStackTrace(); + } + MultiValueMap headers =(MultiValueMap) headerJson.getInnerMap();; + headers.add("Accept", "*/*"); + headers.add("Content-Type", "application/json"); + String requestBody = requestBodyJson.toJSONString(); + HttpEntity entity = new HttpEntity(requestBody, headers); + ResponseEntity responseEntity=restTemplate.postForEntity(httpUrl,entity,String.class); + //获取返回结果 + String result= responseEntity.getBody(); + return JSONObject.parseObject(result); + } } diff --git a/jeecg-boot-base-core/src/main/java/org/jeecg/config/RestTemplateConfig.java b/jeecg-boot-base-core/src/main/java/org/jeecg/config/RestTemplateConfig.java index 860889a..9c9d191 100644 --- a/jeecg-boot-base-core/src/main/java/org/jeecg/config/RestTemplateConfig.java +++ b/jeecg-boot-base-core/src/main/java/org/jeecg/config/RestTemplateConfig.java @@ -1,11 +1,25 @@ package org.jeecg.config; + +import org.apache.http.conn.ssl.NoopHostnameVerifier; +import org.apache.http.conn.ssl.SSLConnectionSocketFactory; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.ssl.SSLContexts; +import org.apache.http.ssl.TrustStrategy; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.client.ClientHttpRequestFactory; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.http.client.SimpleClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; +import javax.net.ssl.SSLContext; +import java.security.KeyManagementException; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; + /** * 优雅的http请求方式RestTemplate * @author: jeecg-boot @@ -28,4 +42,19 @@ public class RestTemplateConfig { factory.setConnectTimeout(15000); return factory; } + + public static HttpComponentsClientHttpRequestFactory generateHttpRequestFactory() + throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException { + TrustStrategy acceptingTrustStrategy = (x509Certificates, authType) -> true; + SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build(); + SSLConnectionSocketFactory connectionSocketFactory = new SSLConnectionSocketFactory(sslContext, new NoopHostnameVerifier()); + + HttpClientBuilder httpClientBuilder = HttpClients.custom(); + httpClientBuilder.setSSLSocketFactory(connectionSocketFactory); + CloseableHttpClient httpClient = httpClientBuilder.build(); + HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(); + factory.setHttpClient(httpClient); + return factory; + } + } diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/api/controller/ApiController.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/api/controller/ApiController.java index 07aa680..ce2d501 100644 --- a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/api/controller/ApiController.java +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/api/controller/ApiController.java @@ -58,8 +58,8 @@ public class ApiController { @RequestMapping("/identify") @Transactional @ResponseBody - public Result pushSemantic(@RequestBody JSONObject requestBody){ - if (requestBody==null) { + public Result pushSemantic(@RequestBody JSONObject requestBody) { + if (requestBody == null) { return Result.error("请输入请求参数"); } //1.获取请求参数 @@ -72,37 +72,37 @@ public class ApiController { List sourceJsonList = sourceJson.toJavaList(JSONObject.class);//校验数据源 //============================================================= //2.参数判断 - AssertUtils.notNull(requestId,"[requestId]-不可为空"); - AssertUtils.notTrue(!"door".equals(scenes),String.format("暂不支持该场景类型[%s]",scenes)); - AssertUtils.notNull(ruleId,"[ruleId]-不可为空"); + AssertUtils.notNull(requestId, "[requestId]-不可为空"); + AssertUtils.notTrue(!"door".equals(scenes), String.format("暂不支持该场景类型[%s]", scenes)); + AssertUtils.notNull(ruleId, "[ruleId]-不可为空"); OcrRuleCheckVo ruleCheck = ocrRuleCheckService.findById(ruleId); - AssertUtils.notNull(ruleCheck,"[ruleId]-不存在"); - AssertUtils.notNull(priority,"[priority]-不可为空"); - AssertUtils.notNull(sourceJson,"[sourceJson]-不可为空"); + AssertUtils.notNull(ruleCheck, "[ruleId]-不存在"); + AssertUtils.notNull(priority, "[priority]-不可为空"); + AssertUtils.notNull(sourceJson, "[sourceJson]-不可为空"); //获取识别的图片 - List fileList=new ArrayList<>(); + List fileList = new ArrayList<>(); //获取相对路径 List ocr_relative_path = sysDictService.queryDictItemsByCode("ocr_relative_path"); - String relativePath=null; - if (ocr_relative_path!=null && ocr_relative_path.size()>0) { + String relativePath = null; + if (ocr_relative_path != null && ocr_relative_path.size() > 0) { for (DictModel dictModel : ocr_relative_path) { if (dictModel.getValue().equals("0")) { - relativePath= dictModel.getText(); + relativePath = dictModel.getText(); } } } - List fileUrlList= FileOUtils.fileLists(relativePath,sourceImages); + List fileUrlList = FileOUtils.fileLists(relativePath, sourceImages); for (String fileUrl : fileUrlList) { //判断附件是否是 图片格式 - if (fileUrl.lastIndexOf(".png")!=-1 || fileUrl.lastIndexOf(".jpg")!=-1|| fileUrl.lastIndexOf(".jpeg")!=-1) { + if (fileUrl.lastIndexOf(".png") != -1 || fileUrl.lastIndexOf(".jpg") != -1 || fileUrl.lastIndexOf(".jpeg") != -1) { fileList.add(fileUrl); } } - AssertUtils.hasSize(fileList,"图片目录下未获取到可使用的图片格式文件"); - OcrIdentify ocrIdentify=new OcrIdentify(); + AssertUtils.hasSize(fileList, "图片目录下未获取到可使用的图片格式文件"); + OcrIdentify ocrIdentify = new OcrIdentify(); //2.创建识别任务 - if(true){ + if (true) { ocrIdentify.setTaskName(requestId); ocrIdentify.setRequestId(requestId); ocrIdentify.setStatus("0");//任务进行中 @@ -120,7 +120,7 @@ public class ApiController { ocrIdentifyService.save(ocrIdentify); } //3.请求python ocr识别,异步执行 - ocrIdentifyService.postSemantic(ocrIdentify,fileList); + ocrIdentifyService.postSemantic(ocrIdentify, fileList); return Result.OK("请求成功"); } @@ -155,9 +155,9 @@ public class ApiController { @PostMapping(value = "/imgNotify") @Transactional(rollbackFor = Exception.class) public JSONObject imgNotify() { - String ss="{\t\"requestId\":\"10001\",\t\"result\":{\t\t\"ocrResult\":[\t\t\t{\t\t\t\t\"tag\":\"hospitalName\",\t\t\t\t\"inputText\":\"仁和医院\",\t\t\t\t\"ocrText\":\"仁和医院\",\t\t\t\t\"ocrPrecisionRate\":0.8,\t\t\t\t\"sourceImage\":{\t\t\t\t\t\"fileName\":\"test1.png\",\t\t\t\t\t\"path\":\"/usr/local/ocr/test1.png\"\t\t\t\t},\t\t\t\t\"failureReason\":\"\"\t\t\t}\t\t],\t\t\"ruleValidation\":false,\t\t\"retrieveReviewCompliance\":\"80\",\t\t\"imageTagRetrievePercentage\":\"66.6\",\t\t\"failureReason\":\"医生名称不匹配\"\t}}"; - JSONObject requestBody=JSONObject.parseObject(ss); - JSONObject semanticResponseJson = RestUtil.post("http://111.202.228.113:8071/callback", requestBody); + String ss = "{\t\"requestId\":\"10001\",\t\"result\":{\t\t\"ocrResult\":[\t\t\t{\t\t\t\t\"tag\":\"hospitalName\",\t\t\t\t\"inputText\":\"仁和医院\",\t\t\t\t\"ocrText\":\"仁和医院\",\t\t\t\t\"ocrPrecisionRate\":0.8,\t\t\t\t\"sourceImage\":{\t\t\t\t\t\"fileName\":\"test1.png\",\t\t\t\t\t\"path\":\"/usr/local/ocr/test1.png\"\t\t\t\t},\t\t\t\t\"failureReason\":\"\"\t\t\t}\t\t],\t\t\"ruleValidation\":false,\t\t\"retrieveReviewCompliance\":\"80\",\t\t\"imageTagRetrievePercentage\":\"66.6\",\t\t\"failureReason\":\"医生名称不匹配\"\t}}"; + JSONObject requestBody = JSONObject.parseObject(ss); + JSONObject semanticResponseJson = RestUtil.postTest("http://111.202.228.113:8071/callback", requestBody, new JSONObject()); return semanticResponseJson; /*String requestId = jsonObject.getString("requestId"); String fileName = jsonObject.getString("fileName"); @@ -170,8 +170,8 @@ public class ApiController { @PostMapping(value = "/imgNotify1") @Transactional(rollbackFor = Exception.class) public JSONObject imgNotify1() { - String ss="{\t\"requestId\":\"10001\",\t\"result\":{\t\t\"ocrResult\":[\t\t\t{\t\t\t\t\"tag\":\"hospitalName\",\t\t\t\t\"inputText\":\"仁和医院\",\t\t\t\t\"ocrText\":\"仁和医院\",\t\t\t\t\"ocrPrecisionRate\":0.8,\t\t\t\t\"sourceImage\":{\t\t\t\t\t\"fileName\":\"test1.png\",\t\t\t\t\t\"path\":\"/usr/local/ocr/test1.png\"\t\t\t\t},\t\t\t\t\"failureReason\":\"\"\t\t\t}\t\t],\t\t\"ruleValidation\":false,\t\t\"retrieveReviewCompliance\":\"80\",\t\t\"imageTagRetrievePercentage\":\"66.6\",\t\t\"failureReason\":\"医生名称不匹配\"\t}}"; - JSONObject requestBody=JSONObject.parseObject(ss); + String ss = "{\t\"requestId\":\"10001\",\t\"result\":{\t\t\"ocrResult\":[\t\t\t{\t\t\t\t\"tag\":\"hospitalName\",\t\t\t\t\"inputText\":\"仁和医院\",\t\t\t\t\"ocrText\":\"仁和医院\",\t\t\t\t\"ocrPrecisionRate\":0.8,\t\t\t\t\"sourceImage\":{\t\t\t\t\t\"fileName\":\"test1.png\",\t\t\t\t\t\"path\":\"/usr/local/ocr/test1.png\"\t\t\t\t},\t\t\t\t\"failureReason\":\"\"\t\t\t}\t\t],\t\t\"ruleValidation\":false,\t\t\"retrieveReviewCompliance\":\"80\",\t\t\"imageTagRetrievePercentage\":\"66.6\",\t\t\"failureReason\":\"医生名称不匹配\"\t}}"; + JSONObject requestBody = JSONObject.parseObject(ss); JSONObject semanticResponseJson = RestUtil.post("https://192.168.5.179:8080/api/task/image/ocr/callback", requestBody); return semanticResponseJson; /*String requestId = jsonObject.getString("requestId");