Authored by 王涛

动态接口优化

@@ -5,6 +5,7 @@ import org.springframework.util.PropertyPlaceholderHelper; @@ -5,6 +5,7 @@ import org.springframework.util.PropertyPlaceholderHelper;
5 import java.util.ArrayList; 5 import java.util.ArrayList;
6 import java.util.List; 6 import java.util.List;
7 import java.util.Map; 7 import java.util.Map;
  8 +import java.util.Objects;
8 import java.util.regex.Matcher; 9 import java.util.regex.Matcher;
9 import java.util.regex.Pattern; 10 import java.util.regex.Pattern;
10 11
@@ -18,7 +19,7 @@ public class ParamsResolverHelper { @@ -18,7 +19,7 @@ public class ParamsResolverHelper {
18 new PropertyPlaceholderHelper(placeholderPrefix, placeholderSuffix); 19 new PropertyPlaceholderHelper(placeholderPrefix, placeholderSuffix);
19 20
20 public static String resolveParams(final Map<String, Object> param, String con) { 21 public static String resolveParams(final Map<String, Object> param, String con) {
21 - con = helper.replacePlaceholders(con, (key -> param.get(key) + "")); 22 + con = helper.replacePlaceholders(con, (key -> Objects.equals(param.get(key) ,null) ? "" : param.get(key) + ""));
22 return con; 23 return con;
23 } 24 }
24 25
@@ -29,15 +29,14 @@ import org.springframework.http.HttpHeaders; @@ -29,15 +29,14 @@ import org.springframework.http.HttpHeaders;
29 import org.springframework.http.HttpMethod; 29 import org.springframework.http.HttpMethod;
30 import org.springframework.http.ResponseEntity; 30 import org.springframework.http.ResponseEntity;
31 import org.springframework.stereotype.Service; 31 import org.springframework.stereotype.Service;
  32 +import org.springframework.web.client.HttpClientErrorException;
32 import org.springframework.web.client.RestClientException; 33 import org.springframework.web.client.RestClientException;
33 import org.springframework.web.client.RestTemplate; 34 import org.springframework.web.client.RestTemplate;
34 35
35 import javax.annotation.Resource; 36 import javax.annotation.Resource;
36 import java.sql.*; 37 import java.sql.*;
37 -import java.util.ArrayList; 38 +import java.util.*;
38 import java.util.Date; 39 import java.util.Date;
39 -import java.util.List;  
40 -import java.util.Map;  
41 40
42 /** 41 /**
43 * @author Raod 42 * @author Raod
@@ -104,7 +103,9 @@ public class DataSourceServiceImpl implements DataSourceService { @@ -104,7 +103,9 @@ public class DataSourceServiceImpl implements DataSourceService {
104 testRelationalDb(dto); 103 testRelationalDb(dto);
105 break; 104 break;
106 case JdbcConstants.HTTP: 105 case JdbcConstants.HTTP:
107 - testHttp(dto); 106 + // Start Wang 2022/1/21 10:51 http请求不做验证
  107 + // testHttp(dto);
  108 + // End Wang 2022/1/21 10:51
108 break; 109 break;
109 default: 110 default:
110 throw BusinessExceptionBuilder.build(ResponseCode.DATA_SOURCE_TYPE_DOES_NOT_MATCH_TEMPORARILY); 111 throw BusinessExceptionBuilder.build(ResponseCode.DATA_SOURCE_TYPE_DOES_NOT_MATCH_TEMPORARILY);
@@ -295,22 +296,40 @@ public class DataSourceServiceImpl implements DataSourceService { @@ -295,22 +296,40 @@ public class DataSourceServiceImpl implements DataSourceService {
295 */ 296 */
296 public List<JSONObject> executeHttp(DataSourceDto dto) { 297 public List<JSONObject> executeHttp(DataSourceDto dto) {
297 analysisHttpConfig(dto); 298 analysisHttpConfig(dto);
  299 + HttpMethod httpMethod = HttpMethod.valueOf(dto.getMethod());
  300 + if(Objects.equals(HttpMethod.GET, httpMethod)){
  301 + Map<String, Object> contextData = dto.getContextData();
  302 +
  303 + if(!contextData.isEmpty()){
  304 + JSONObject o = (JSONObject)JSONObject.toJSON(contextData);
  305 + dto.setDynSentence(o.toJSONString());
  306 + }
  307 + }
298 HttpHeaders headers = new HttpHeaders(); 308 HttpHeaders headers = new HttpHeaders();
299 headers.setAll(JSONObject.parseObject(dto.getHeader(), Map.class)); 309 headers.setAll(JSONObject.parseObject(dto.getHeader(), Map.class));
300 HttpEntity<String> entity = new HttpEntity<>(dto.getDynSentence(), headers); 310 HttpEntity<String> entity = new HttpEntity<>(dto.getDynSentence(), headers);
301 ResponseEntity<Object> exchange; 311 ResponseEntity<Object> exchange;
  312 + List<JSONObject> result = new ArrayList<>();
302 try { 313 try {
303 - exchange = restTemplate.exchange(dto.getApiUrl(), HttpMethod.valueOf(dto.getMethod()), entity, Object.class); 314 + exchange = restTemplate.exchange(dto.getApiUrl(),httpMethod , entity, Object.class);
  315 + } catch (HttpClientErrorException error){
  316 + int rawStatusCode = error.getRawStatusCode();
  317 + // 401 接口请求无权限
  318 + JSONObject jsonObject = new JSONObject();
  319 + jsonObject.put("code",rawStatusCode);
  320 + jsonObject.put("data",error.getMessage());
  321 + result.add(jsonObject);
  322 + return result;
304 } catch (Exception e) { 323 } catch (Exception e) {
305 log.error("error",e); 324 log.error("error",e);
306 throw BusinessExceptionBuilder.build(ResponseCode.DATA_SOURCE_CONNECTION_FAILED, e.getMessage()); 325 throw BusinessExceptionBuilder.build(ResponseCode.DATA_SOURCE_CONNECTION_FAILED, e.getMessage());
307 } 326 }
308 - if (exchange.getStatusCode().isError()) { 327 + if (exchange != null && exchange.getStatusCode().isError()) {
309 throw BusinessExceptionBuilder.build(ResponseCode.DATA_SOURCE_CONNECTION_FAILED, exchange.getBody()); 328 throw BusinessExceptionBuilder.build(ResponseCode.DATA_SOURCE_CONNECTION_FAILED, exchange.getBody());
310 } 329 }
311 Object body = exchange.getBody(); 330 Object body = exchange.getBody();
312 String jsonStr = JSONObject.toJSONString(body); 331 String jsonStr = JSONObject.toJSONString(body);
313 - List<JSONObject> result = new ArrayList<>(); 332 +
314 if (jsonStr.trim().startsWith(BusinessConstant.LEFT_BIG_BOAST) && jsonStr.trim().endsWith(BusinessConstant.RIGTH_BIG_BOAST)) { 333 if (jsonStr.trim().startsWith(BusinessConstant.LEFT_BIG_BOAST) && jsonStr.trim().endsWith(BusinessConstant.RIGTH_BIG_BOAST)) {
315 //JSONObject 334 //JSONObject
316 result.add(JSONObject.parseObject(jsonStr)); 335 result.add(JSONObject.parseObject(jsonStr));