ソースを参照

重构方案接口调用

liuaini 3 週間 前
コミット
eb6a6f43f0

+ 26 - 9
services/load-transfer-si/src/main/java/com/hdkj/lt/si/service/recon/impl/ReconResultServiceImpl.java

@@ -1,25 +1,28 @@
 package com.hdkj.lt.si.service.recon.impl;
 
 import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
 import com.fasterxml.jackson.databind.DeserializationFeature;
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
 
-import com.hdkj.lt.core.rest.service.IRemoteCallService;
+import com.hdkj.lt.base.exception.ExceptionCast;
+import com.hdkj.lt.core.rest.config.HttpClientConfig;
 import com.hdkj.lt.modle.dto.recon.*;
 import com.hdkj.lt.modle.po.*;
 import com.hdkj.lt.modle.vo.recon.ReconSaveResultVO;
 import com.hdkj.lt.si.dao.*;
 import com.hdkj.lt.si.service.recon.ReconResultService;
-import com.hdkj.lt.si.service.remote.impl.ReconRemoteCallServiceImpl;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.codec.Charsets;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.core.io.ClassPathResource;
-import org.springframework.http.HttpMethod;
+import org.springframework.http.*;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.StreamUtils;
+import org.springframework.web.client.RestTemplate;
 
 import javax.annotation.Resource;
 import java.io.IOException;
@@ -53,13 +56,14 @@ public class ReconResultServiceImpl implements ReconResultService {
             .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
     private static final DateTimeFormatter ISO_OFFSET = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
 
-//    private static final String REQUEST_URI = "/api/reconfiguration";
+    @Value("${remote.recon.request-uri}")
+    private String requestUri;
 
     @Value("${remote.recon.recon-uri}")
     private String reconUri;
 
-    @Resource(type = ReconRemoteCallServiceImpl.class)
-    private IRemoteCallService remoteCallService;
+    @Resource(name = HttpClientConfig.CONN_POOL_REST_TEMPLATE)
+    private RestTemplate restTemplate;
 
     @Override
     @Transactional(rollbackFor = Exception.class)
@@ -69,17 +73,30 @@ public class ReconResultServiceImpl implements ReconResultService {
 //        params.put("county_sid", "honghu");
 //        params.put("county_name", "honghu");
 //        params.put("problem_feeder_ids", Collections.singletonList("15DKX-20690"));
+        String requestUrl = requestUri.concat(reconUri);
+        HttpHeaders headers = new HttpHeaders();
+        headers.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
+        headers.set(HttpHeaders.ACCEPT_CHARSET, Charsets.UTF_8.toString());
+        HttpEntity<Object> httpEntity = new HttpEntity<>(params, headers);
         String str;
         RawResultDTO dto;
         try {
-            Object data = remoteCallService.call(reconUri, HttpMethod.POST, params, Object.class);
-            str = JSON.toJSONString(data);
+            ResponseEntity<Object> restRespEntity = restTemplate.postForEntity(requestUrl, httpEntity, Object.class);
+            if (!restRespEntity.getStatusCode().is2xxSuccessful() || Objects.isNull(restRespEntity.getBody())) {
+                String errorMsg = String.format("调用重构方案接口失败, requestUrl: %s, response: %s",
+                        requestUrl, JSON.toJSONString(restRespEntity));
+                log.error(errorMsg);
+                ExceptionCast.cast(errorMsg);
+            }
 
+            Object body = restRespEntity.getBody();
+            str = JSON.toJSONString(body);
+            log.info("调用重构方案接口成功, requestUrl: {}, result: {}",
+                    requestUrl, str);
         // 2. 解析返回的JSON数据
 //            str = StreamUtils.copyToString(new ClassPathResource("1.txt").getInputStream(), StandardCharsets.UTF_8);
 
             JsonNode root = OM.readTree(str);
-            log.info("重构方案接口返回参数{}",str);
             JsonNode rawNode = root.get("raw_result");
             if (rawNode == null || rawNode.isNull()) {
                 throw new RuntimeException("算法响应缺少 raw_result 节点");