Raod 3 years ago
parent 976539bd5c
commit d971f6c6a7

@ -7,6 +7,11 @@ package com.anjiplus.template.gaea.business.constant;
*/
public interface BusinessConstant {
String LEFT_BIG_BOAST = "{";
String RIGTH_BIG_BOAST = "}";
String LEFT_MIDDLE_BOAST = "[";
String RIGHT_MIDDLE_BOAST = "]";
/**
*
*/

@ -10,6 +10,7 @@ import com.anji.plus.gaea.exception.BusinessException;
import com.anji.plus.gaea.exception.BusinessExceptionBuilder;
import com.anji.plus.gaea.utils.GaeaAssert;
import com.anjiplus.template.gaea.business.code.ResponseCode;
import com.anjiplus.template.gaea.business.constant.BusinessConstant;
import com.anjiplus.template.gaea.business.modules.dataSet.controller.dto.DataSetDto;
import com.anjiplus.template.gaea.business.modules.dataSetParam.service.DataSetParamService;
import com.anjiplus.template.gaea.business.modules.dataSource.controller.dto.DataSourceDto;
@ -267,9 +268,9 @@ public class DataSourceServiceImpl implements DataSourceService {
HttpHeaders headers = new HttpHeaders();
headers.setAll(JSONObject.parseObject(dto.getHeader(), Map.class));
HttpEntity<String> entity = new HttpEntity<>(dto.getDynSentence(), headers);
ResponseEntity<JSONObject> exchange;
ResponseEntity<Object> exchange;
try {
exchange = restTemplate.exchange(dto.getApiUrl(), HttpMethod.valueOf(dto.getMethod()), entity, JSONObject.class);
exchange = restTemplate.exchange(dto.getApiUrl(), HttpMethod.valueOf(dto.getMethod()), entity, Object.class);
} catch (Exception e) {
log.error("error",e);
throw BusinessExceptionBuilder.build(ResponseCode.DATA_SOURCE_CONNECTION_FAILED, e.getMessage());
@ -277,9 +278,18 @@ public class DataSourceServiceImpl implements DataSourceService {
if (exchange.getStatusCode().isError()) {
throw BusinessExceptionBuilder.build(ResponseCode.DATA_SOURCE_CONNECTION_FAILED, exchange.getBody());
}
JSONObject body = exchange.getBody();
Object body = exchange.getBody();
String jsonStr = JSONObject.toJSONString(body);
List<JSONObject> result = new ArrayList<>();
result.add(body);
if (jsonStr.trim().startsWith(BusinessConstant.LEFT_BIG_BOAST) && jsonStr.trim().endsWith(BusinessConstant.RIGTH_BIG_BOAST)) {
//JSONObject
result.add(JSONObject.parseObject(jsonStr));
} else if (jsonStr.trim().startsWith(BusinessConstant.LEFT_MIDDLE_BOAST) && jsonStr.trim().endsWith(BusinessConstant.RIGHT_MIDDLE_BOAST)) {
//List
result = JSONArray.parseArray(jsonStr, JSONObject.class);
} else {
result.add(new JSONObject());
}
return result;
}

@ -0,0 +1,37 @@
package com.anjiplus.template.gaea.business.modules.dataSource.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.anjiplus.template.gaea.business.ReportApplication;
import com.anjiplus.template.gaea.business.modules.dataSource.controller.dto.DataSourceDto;
import com.anjiplus.template.gaea.business.modules.dataSource.service.DataSourceService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
/**
* Created by raodeming on 2021/7/19.
*/
@SpringBootTest(classes = ReportApplication.class)
@RunWith(SpringRunner.class)
public class DataSourceServiceImplTest {
@Autowired
private DataSourceService dataSourceService;
@Test
public void testHttp(){
DataSourceDto dto = new DataSourceDto();
dto.setSourceType("http");
dto.setHeader("{\"Content-Type\":\"application/json\"}");
dto.setSourceConfig("{\"apiUrl\":\"http://10.108.26.163:9200/_xpack/sql?format=json\",\"method\":\"POST\",\"header\":\"{\\\"Content-Type\\\":\\\"application/json\\\"}\",\"body\":\"{\\\"query\\\":\\\"select 1\\\"}\"}");
dto.setDynSentence("{\"query\": \"select HISTOGRAM(logTime,INTERVAL 1 MONTH) as h ,count(flag),flag from \\\"analysis-wifilogin\\\" where logTime>='2021-02-22 00:28:10.000' and logTime<'2021-03-22 00:28:10.000' GROUP BY h,flag\"}");
List<JSONObject> execute = dataSourceService.execute(dto);
System.out.println(execute);
}
}
Loading…
Cancel
Save