diff --git a/Linux服务器监听运维功能.md b/Linux服务器监听运维功能.md index e78a0c8..40ecb25 100644 --- a/Linux服务器监听运维功能.md +++ b/Linux服务器监听运维功能.md @@ -310,6 +310,92 @@ } ``` +### 1.5.5 获取配置服务支持的所有接口信息 + +| 调用方式 | 接口地址 | +| ------------ | :-------------------------------------------------- | +| POST | http://ip:port/background/findAllAvailableInterface | +| Content-Type | application/json;charset=UTF-8 | + +| 请求参数 | | | | | +| -------- | -------- | ------- | ---- | ------------------ | +| 参数项 | 名称 | 类型 | 必选 | 描述 | +| configId | 服务主键 | Integer | 是 | 主键(formdata形式) | +``` +请求示例 +"configId": 1 +``` + +| 返回结果 | | | | +| ------------ | -------------------- | ------ | --------------------------------- | +| 参数项 | 名称 | 类型 | 描述 | +| code | 响应码 | int | 200为操作成功,其他code表示失败 | +| message | 提示信息 | string | 200为操作成功, 其他为对应错误信息 | +| data | 返回信息 | | | +| +id | 主键 | int | 主键 | +| +mouldId | 模板id | int | 模板id | +| +methodType | 请求方式类型 | string | 请求方式类型 | +| +contentType | 请求头参数类型 | string | 请求头参数类型 | +| +uri | 请求地址 | string | 请求地址 | +| +defaultJson | 默认请求参数(json串) | string | 默认请求参数(json串) | +| +sort | 排序 | int | 排序 | + +``` +响应示例 +{ + "code": 200, + "message": "操作成功", + "data": [ + { + "id": 1, + "mouldId": 1, + "methodType": "post", + "contentType": "application/json;charset=UTF-8", + "uri": "/vehicle/base64", + "defaultJson": "{"TPXX":"#base64#"}", + "sort": 1 + } + ] +} +``` +### 1.5.6 根据配置明细项id获取请求http的返回值信息 + +| 调用方式 | 接口地址 | +| ------------ | :----------------------------------------------- | +| POST | http://ip:port/background/getServiceHttpResponse | +| Content-Type | application/json;charset=UTF-8 | + +| 请求参数 | | | | | +| ---------------- | ------------ | ------- | ---- | ------------------------------------------ | +| 参数项 | 名称 | 类型 | 必选 | 描述 | +| configId | 配置服务id | Integer | 是 | 配置服务id (param格式) | +| interfaceMouldId | 接口id | Integer | 是 | 接口id (param格式) | +| params | 请求参数信息 | string | 否 | 如果是需要文件传入对应参数部分写入"#file#" | +| multipartFile | 文件信息 | string | 否 | 文件信息 (param格式) | +``` +请求示例 +"configId": 1 +"interfaceMouldId": "start", +"params": "{"TPXX":"#file#"}", +"multipartFile": xxx +``` + +| 返回结果 | | | | +| -------- | -------- | ------ | --------------------------------- | +| 参数项 | 名称 | 类型 | 描述 | +| code | 响应码 | int | 200为操作成功,其他code表示失败 | +| message | 提示信息 | string | 200为操作成功, 其他为对应错误信息 | +| data | 返回信息 | | | +| result | 操作结果 | string | 操作成功信息或者失败原因 | + +``` +响应示例 +{ + "code": 200, + "message": "操作成功", + "data": {} +} +``` ## 1.6 查询配置好的服务运行情况 diff --git a/src/main/java/com/objecteye/controller/BackGroundController.java b/src/main/java/com/objecteye/controller/BackGroundController.java index 61e214a..0509872 100644 --- a/src/main/java/com/objecteye/controller/BackGroundController.java +++ b/src/main/java/com/objecteye/controller/BackGroundController.java @@ -89,7 +89,8 @@ public class BackGroundController extends BasicController { @ApiOperation("根据配置明细项id获取请求http的返回值信息") @RequestMapping(value = "getServiceHttpResponse", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"}) public CommonResult getServiceHttpResponse(@RequestParam Integer configId, @RequestParam Integer interfaceMouldId, - @RequestParam String params, @RequestParam(required = false) MultipartFile multipartFile) { + @RequestParam(required = false) String params, + @RequestParam(required = false) MultipartFile multipartFile) { return CommonResult.success(iBackGroundService.getServiceHttpResponse(configId, interfaceMouldId, params, multipartFile)); } diff --git a/src/main/java/com/objecteye/service/impl/BackGroundServiceImpl.java b/src/main/java/com/objecteye/service/impl/BackGroundServiceImpl.java index a4efb25..229c433 100644 --- a/src/main/java/com/objecteye/service/impl/BackGroundServiceImpl.java +++ b/src/main/java/com/objecteye/service/impl/BackGroundServiceImpl.java @@ -18,19 +18,23 @@ import com.objecteye.utils.LinuxUtils; import com.objecteye.vo.VSyServiceMainTable; import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.FileUtils; +import org.apache.http.Consts; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +import org.apache.http.entity.mime.MultipartEntityBuilder; +import org.apache.http.entity.mime.content.FileBody; +import org.apache.http.entity.mime.content.StringBody; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.core.io.FileSystemResource; import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpHeaders; -import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; -import org.springframework.util.LinkedMultiValueMap; -import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; import org.springframework.web.multipart.MultipartFile; @@ -593,32 +597,49 @@ public class BackGroundServiceImpl implements IBackGroundService { throw new CustomXException("201", "请求路径信息异常"); } JSONObject paramJson = JSON.parseObject(paramsStr); - MultiValueMap multiValueMap = new LinkedMultiValueMap<>(); - for (String key : paramJson.keySet()) { - String value = paramJson.getString(key); - if ("#file#".equals(value)) { - if (multipartFile != null && multipartFile.getOriginalFilename() != null) { - try { - FileUtils.writeByteArrayToFile(new File(multipartFile.getOriginalFilename()), multipartFile.getBytes()); - } catch (IOException e) { - e.printStackTrace(); + CloseableHttpResponse response = null; + String resultString = ""; + CloseableHttpClient httpClient = HttpClients.createDefault(); + HttpPost httpPost = new HttpPost("http://" + ip + ":" + port + uri); + MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create(); + try { + if (paramsStr != null && paramsStr.contains("#file#")) { + for (String key : paramJson.keySet()) { + String value = paramJson.getString(key); + if ("#file#".equals(value)) { + if (multipartFile != null && multipartFile.getOriginalFilename() != null) { + FileUtils.writeByteArrayToFile(new File(multipartFile.getOriginalFilename()), multipartFile.getBytes()); + FileBody fileBody = new FileBody(new File(multipartFile.getOriginalFilename()), org.apache.http.entity.ContentType.create("image/png", Consts.UTF_8)); + multipartEntityBuilder.addPart(key, fileBody); + } else { + throw new CustomXException("201", "参数为空"); + } + } else { + multipartEntityBuilder.addPart(key, new StringBody(value)); } - FileSystemResource fileSystemResource = new FileSystemResource(new File(multipartFile.getOriginalFilename())); - multiValueMap.add(key, fileSystemResource); - } else { - throw new CustomXException("201", "参数为空"); } - } else { - multiValueMap.add(key, value); + httpPost.setEntity(multipartEntityBuilder.build()); + } else if (paramsStr != null) { + StringEntity stringEntity = new StringEntity(paramsStr, "utf-8"); + httpPost.addHeader("Content-Type", syServiceInterfaceMould.getContentType()); + httpPost.setEntity(stringEntity); + } + response = httpClient.execute(httpPost); + resultString = EntityUtils.toString(response.getEntity(), "utf-8"); + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (response != null) { + try { + response.close(); + } catch (IOException e) { + e.printStackTrace(); + } } } - HttpHeaders httpHeaders = new HttpHeaders(); - httpHeaders.setContentType(MediaType.parseMediaType(syServiceInterfaceMould.getContentType())); - HttpEntity> httpEntity = new HttpEntity<>(multiValueMap, httpHeaders); if ("post".equals(syServiceInterfaceMould.getMethodType().toLowerCase())) { - ResponseEntity responseEntity = restTemplate.postForEntity("http://" + ip + ":" + port + uri, httpEntity, String.class); - return responseEntity.getBody(); + return resultString; } else { return "其他请求方式暂不支持"; } @@ -639,7 +660,7 @@ public class BackGroundServiceImpl implements IBackGroundService { .andItemNameIn(Arrays.asList("serviceInterface", "interfaceMould")).example()); List interfaceMouldIdList = new ArrayList<>(); - Integer mouldId = null; + List mouldIdList = new ArrayList<>(); for (SyServiceConfigItem syServiceConfigItem : syServiceConfigItems) { if (syServiceConfigItem == null) { continue; @@ -647,16 +668,19 @@ public class BackGroundServiceImpl implements IBackGroundService { if ("serviceInterface".equals(syServiceConfigItem.getItemName())) { interfaceMouldIdList.add(Integer.valueOf(syServiceConfigItem.getItemValue())); } else if ("interfaceMould".equals(syServiceConfigItem.getItemName())) { - mouldId = Integer.valueOf(syServiceConfigItem.getItemValue()); + mouldIdList.add(Integer.valueOf(syServiceConfigItem.getItemValue())); } } SyServiceInterfaceMouldExample syServiceInterfaceMouldExample = new SyServiceInterfaceMouldExample(); + boolean hasCondition = false; if (interfaceMouldIdList.size() > 0) { + hasCondition = true; syServiceInterfaceMouldExample.or(SyServiceInterfaceMouldExample.newAndCreateCriteria().andIdIn(interfaceMouldIdList)); } - if (mouldId != null) { - syServiceInterfaceMouldExample.or(SyServiceInterfaceMouldExample.newAndCreateCriteria().andMouldIdEqualTo(mouldId)); + if (mouldIdList.size() > 0) { + hasCondition = true; + syServiceInterfaceMouldExample.or(SyServiceInterfaceMouldExample.newAndCreateCriteria().andMouldIdIn(mouldIdList)); } - return syServiceInterfaceMouldMapper.selectByExample(syServiceInterfaceMouldExample); + return hasCondition ? syServiceInterfaceMouldMapper.selectByExample(syServiceInterfaceMouldExample) : new ArrayList<>(); } }