Browse Source

启用shell

master
liuchen864 1 month ago
parent
commit
1e683632c1
  1. 12
      win-admin/src/main/java/com/win/web/controller/base/CommandController.java
  2. 189
      win-admin/src/main/java/com/win/web/controller/base/ExcelController.java
  3. 18
      win-admin/src/main/java/com/win/web/controller/base/ShellController.java
  4. 2
      win-admin/src/main/resources/application-dev.yml
  5. 2
      win-admin/src/main/resources/application-prod.yml
  6. 2
      win-admin/src/main/resources/application-test.yml
  7. BIN
      win-admin/src/main/resources/changchun.xls
  8. BIN
      win-admin/src/main/resources/changchun.xlsx
  9. BIN
      win-admin/src/main/resources/foshan.xls
  10. BIN
      win-admin/src/main/resources/foshan_qingdao.xlsx
  11. BIN
      win-admin/src/main/resources/guowai1.xls
  12. BIN
      win-admin/src/main/resources/guowai1.xlsx
  13. BIN
      win-admin/src/main/resources/guowai2.xls
  14. BIN
      win-admin/src/main/resources/guowai2.xlsx
  15. 12
      win-admin/src/main/resources/logback-spring.xml
  16. BIN
      win-admin/src/main/resources/qingdao.xls
  17. 4
      win-quartz/pom.xml
  18. 114
      win-quartz/src/main/java/com/win/quartz/task/WinTask.java
  19. 61
      win-quartz/src/main/java/com/win/quartz/util/JsonToXmlConverter.java

12
win-admin/src/main/java/com/win/web/controller/base/CommandController.java

@ -23,6 +23,7 @@ import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.List;
@RestController
@ -71,6 +72,15 @@ public class CommandController {
if(/*!activeProfile.contains("dev") && */!StringUtils.equals(sign, computeSign.toUpperCase())) {
return AjaxResult.error(HttpStatus.SIGN_ERROR, "签名不正确");
}
//url特殊字符要转换
body = body.replace("+", "%2B");
body = body.replace("/", "%2F");
body = body.replace("?", "%3F");
body = body.replace("#", "%23");
body = body.replace("&", "%26");
body = body.replace("=", "%3D");
//处理kettle加密中文有问题
body = URLDecoder.decode(body, "UTF-8");
JSONObject jsonObject = JSONObject.parseObject(body);
String key = "";
for(String keyTemp : jsonObject.keySet()) {
@ -88,6 +98,7 @@ public class CommandController {
QueryWrapper<LinuxAccess> linuxAccessQueryWrapper = new QueryWrapper<>();
linuxAccessQueryWrapper.eq("company_code", companyCode);
linuxAccessQueryWrapper.eq("uri", interfaceName);
linuxAccessQueryWrapper.isNull("delete_time");
LinuxAccess linuxAccess = linuxAccessService.getOne(linuxAccessQueryWrapper);
if(linuxAccess == null) {
return AjaxResult.error(HttpStatus.LINUX_ACCESS_ERROR, "linux访问控制不存在");
@ -95,6 +106,7 @@ public class CommandController {
QueryWrapper<QadAccess> qadAccessQueryWrapper = new QueryWrapper<>();
qadAccessQueryWrapper.eq("company_code", companyCode);
qadAccessQueryWrapper.eq("domain", domain);
qadAccessQueryWrapper.isNull("delete_time");
QadAccess qadAccess = qadAccessService.getOne(qadAccessQueryWrapper);
if(qadAccess == null) {
return AjaxResult.error(HttpStatus.QAD_ACCESS_ERROR, "QAD_ACCESS_ERROR");

189
win-admin/src/main/java/com/win/web/controller/base/ExcelController.java

@ -159,6 +159,15 @@ public class ExcelController {
String schdLineDate = jsonObjectLine.getString("schd_line_date");
String schdLineDateDl = jsonObjectLine.getString("schd_line_date_dl");
String schdLineDateTh = jsonObjectLine.getString("schd_line_date_th");
if(schdLineDate != null) {
schdLineDate = schdLineDate.replace("-", "/");
}
if(schdLineDateDl != null) {
schdLineDateDl = schdLineDateDl.replace("-", "/");
}
if(schdLineDateTh != null) {
schdLineDateTh = schdLineDateTh.replace("-", "/");
}
if (schdLineDate != null) {
if ("F".equals(schdLineFcQual)) {
JSONObject firm = firmMap.get(schdLineDate);
@ -247,7 +256,9 @@ public class ExcelController {
if("changchun".equals(type)) {//长春模板
fileNames.add(fillChangchun(orderNo, jsonObjectHeader, firmOrderArray, firmArray, forecastArray));
} else if("foshan".equals(type)) {
fileNames.add(fillFoshanQingdao(orderNo, jsonObjectHeader, firmOrderArray, firmArray, forecastArray));
fileNames.add(fillFoshan(orderNo, jsonObjectHeader, firmOrderArray, firmArray, forecastArray));
} else if("qingdao".equals(type)) {
fileNames.add(fillQingdao(orderNo, jsonObjectHeader, firmOrderArray, firmArray, forecastArray));
} else if("guowai1".equals(type)) {
fileNames.add(fillGuowai1(orderNo, jsonObjectHeader, firmOrderArray, firmArray, forecastArray));
} else if("guowai2".equals(type)) {
@ -419,7 +430,177 @@ public class ExcelController {
}
/**
* 横财佛山青岛格式excel
* 横财佛山格式excel
* @param orderNo 订单号
* @param jsonObjectHeader 头部信息
* @param orderArray 订单数据
* @param firmArray firm数据
* @param forecastArray forecast数据
* @return 生成文件的路径
* @throws IOException io异常
*/
private String fillFoshan(String orderNo, JSONObject jsonObjectHeader, JSONArray orderArray, JSONArray firmArray, JSONArray forecastArray) throws IOException {
ClassPathResource templateFile = new ClassPathResource("foshan.xls");
String filePath = WinConfig.getUploadPath();
String fileName = orderNo + ".xls";
File file = new File(filePath);
if (!file.exists()) {
file.mkdirs();
}
file = new File(filePath + "/" + fileName);
if (file.exists()) {
file.delete();
}
final ExcelWriter excelWriter = EasyExcelFactory.write(filePath + "/" + fileName).excelType(ExcelTypeEnum.XLS).withTemplate(templateFile.getInputStream()).build();
try {
final WriteSheet writeSheet = EasyExcelFactory.writerSheet().build();
//默认纵向添加数据
FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
//基础数据
excelWriter.fill(jsonObjectHeader, writeSheet);
//order数据
excelWriter.fill(new FillWrapper("order", orderArray), fillConfig, writeSheet);
} finally {
if (excelWriter != null) {
excelWriter.finish();
}
}
FileInputStream inputStream = new FileInputStream(filePath + "/" + fileName);
Workbook workbook = new HSSFWorkbook(inputStream);
Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表
CellStyle cellStyle1 = getCellStyle(sheet, 14, 8);//Firm订标题样式
CellStyle cellStyle2 = getCellStyle(sheet, 14, 9);//Pick Up Date提货时间
CellStyle cellStyle3 = getCellStyle(sheet, 14, 10);//forecast标题
CellStyle cellStyle4 = getCellStyle(sheet, 15, 8);//Firm日期样式
CellStyle cellStyle5 = getCellStyle(sheet, 15, 10);//Forecast日期样式
CellStyle cellStyle6 = getCellStyle(sheet, 16, 8);//Firm数据样式
CellStyle cellStyle7 = getCellStyle(sheet, 16, 10);//Forecast数据样式
Row row14 = sheet.getRow(14);
Row row15 = sheet.getRow(15);
for (int i = 0; i < firmArray.size(); i++) {
int cellIndex = 8 + i;
JSONObject firm = firmArray.getJSONObject(i);
//设置firm标题
Cell firmCell = row14.getCell(cellIndex);
if (firmCell == null) {
firmCell = row14.createCell(cellIndex);
}
firmCell.setCellValue("Firm订单");
firmCell.setCellStyle(cellStyle1);
//设置firm日期
Cell firmDateCell = row15.getCell(cellIndex);
if (firmDateCell == null) {
firmDateCell = row15.createCell(cellIndex);
}
firmDateCell.setCellValue(firm.getString("schd_line_date"));
firmDateCell.setCellStyle(cellStyle4);
//设置每个firm订单数据
for (int count = 0; count < orderArray.size(); count++) {
int cellValue = 0;
JSONObject object = orderArray.getJSONObject(count);
JSONObject order = firm.getJSONObject(object.getString("schd_line_line"));
Row nextRow = sheet.getRow(16 + count);
Cell nextCell = nextRow.getCell(cellIndex);
if (nextCell == null) {
nextCell = nextRow.createCell(cellIndex);
}
if (order != null) {
cellValue = order.getIntValue("schd_line_qty");
}
if (cellValue != 0) {
nextCell.setCellValue(cellValue);
} else {
nextCell.setCellValue("");
}
nextCell.setCellStyle(cellStyle6);
}
}
//设置firm前的Pick Up Date 提货日期列
for (int count = 0; count < orderArray.size(); count++) {
Row beforeRow = sheet.getRow(16 + count);
Cell beforeCell = beforeRow.getCell(7);
if (beforeCell == null) {
beforeCell = beforeRow.createCell(7);
}
beforeCell.setCellValue("");
beforeCell.setCellStyle(cellStyle1);
}
//设置forecast前的Pick Up Date 提货日期列
for (int count = 0; count < orderArray.size() + 2; count++) {
int cellIndex = 8 + firmArray.size();
Row nextRow = sheet.getRow(14 + count);
Cell nextCell = nextRow.getCell(cellIndex);
if (nextCell == null) {
nextCell = nextRow.createCell(cellIndex);
}
nextCell.setCellValue("");
nextCell.setCellStyle(cellStyle2);
if (count == 0) {
nextCell.setCellValue("Pick Up Date 提货日期");
nextCell.setCellStyle(cellStyle2);
} else if (count == 1) {
nextCell.setCellValue("ETA " + jsonObjectHeader.getString("schm_header_arrive_us") + "\n到达" + jsonObjectHeader.getString("schm_header_arrive_ch") + "时间");
} else {
nextCell.setCellValue("");
}
}
for (int i = 0; i < forecastArray.size(); i++) {
int cellIndex = 9 + firmArray.size() + i;
//设置forecast标题
Cell forecastCell = row14.getCell(cellIndex);
if (forecastCell == null) {
forecastCell = row14.createCell(cellIndex);
}
if (i == 0) {
forecastCell.setCellValue("forecast 预测");
} else {
forecastCell.setCellValue("");
}
forecastCell.setCellStyle(cellStyle3);
//设置forecast日期
JSONObject forecast = forecastArray.getJSONObject(i);
Cell forecastDateCell = row15.getCell(cellIndex);
if (forecastDateCell == null) {
forecastDateCell = row15.createCell(cellIndex);
}
forecastDateCell.setCellValue(forecast.getString("schd_line_date"));
forecastDateCell.setCellStyle(cellStyle5);
//设置forecast每个订单数据
for (int count = 0; count < orderArray.size(); count++) {
int cellValue = 0;
JSONObject object = orderArray.getJSONObject(count);
JSONObject order = forecast.getJSONObject(object.getString("schd_line_line"));
Row nextRow = sheet.getRow(16 + count);
if (order != null) {
cellValue = order.getIntValue("schd_line_qty");
}
Cell nextCell = nextRow.getCell(cellIndex);
if (nextCell == null) {
nextCell = nextRow.createCell(cellIndex);
}
if (cellValue != 0) {
nextCell.setCellValue(cellValue);
} else {
nextCell.setCellValue("");
}
nextCell.setCellStyle(cellStyle7);
}
}
if (!forecastArray.isEmpty() && forecastArray.size() > 1) {
// 合并单元格
CellRangeAddress cellRangeAddress3 = new CellRangeAddress(14, 14, firmArray.size() + 9, firmArray.size() + forecastArray.size() + 8);
sheet.addMergedRegion(cellRangeAddress3);
}
inputStream.close();
FileOutputStream outputStream = new FileOutputStream(filePath + "/" + fileName);
workbook.write(outputStream);
workbook.close();
outputStream.close();
return fileName;
}
/**
* 横财青岛格式excel
* @param orderNo 订单号
* @param jsonObjectHeader 头部信息
* @param orderArray 订单数据
@ -428,8 +609,8 @@ public class ExcelController {
* @return 生成文件的路径
* @throws IOException io异常
*/
private String fillFoshanQingdao(String orderNo, JSONObject jsonObjectHeader, JSONArray orderArray, JSONArray firmArray, JSONArray forecastArray) throws IOException {
ClassPathResource templateFile = new ClassPathResource("foshan_qingdao.xls");
private String fillQingdao(String orderNo, JSONObject jsonObjectHeader, JSONArray orderArray, JSONArray firmArray, JSONArray forecastArray) throws IOException {
ClassPathResource templateFile = new ClassPathResource("qingdao.xls");
String filePath = WinConfig.getUploadPath();
String fileName = orderNo + ".xls";
File file = new File(filePath);

18
win-admin/src/main/java/com/win/web/controller/base/ShellController.java

@ -24,6 +24,7 @@ import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.List;
@RestController
@ -60,18 +61,27 @@ public class ShellController {
return AjaxResult.error(HttpStatus.TIMESTAMP_ERROR, "时间戳不正确");
}
String tmp = interfaceName + body + "Wenyin@2024" + timestamp;
String computeSign = DigestUtils.md5DigestAsHex(tmp.getBytes()).toUpperCase();
String computeSign = DigestUtils.md5DigestAsHex(tmp.getBytes());
long tenTimestamp = timestamp + (10 * 60 * 1000); // 计算10分钟后的时间戳
long currentTimestamp = System.currentTimeMillis(); // 获取当前时间戳
//过期
if(timestamp > currentTimestamp || tenTimestamp < currentTimestamp) {
if(tenTimestamp < currentTimestamp) {
return AjaxResult.error(HttpStatus.EXPIRE_ERROR, "请求已过期");
}
List<String> activeProfile = ProfileUtil.getActiveProfile();
//dev环境不校验签名
if(/*!activeProfile.contains("dev") && */!StringUtils.equals(sign, computeSign)) {
if(/*!activeProfile.contains("dev") && */!StringUtils.equals(sign, computeSign.toUpperCase())) {
return AjaxResult.error(HttpStatus.SIGN_ERROR, "签名不正确");
}
//url特殊字符要转换
body = body.replace("+", "%2B");
body = body.replace("/", "%2F");
body = body.replace("?", "%3F");
body = body.replace("#", "%23");
body = body.replace("&", "%26");
body = body.replace("=", "%3D");
//处理kettle加密中文有问题
body = URLDecoder.decode(body, "UTF-8");
JSONObject jsonObject = JSONObject.parseObject(body);
String key = "";
for(String keyTemp : jsonObject.keySet()) {
@ -89,6 +99,7 @@ public class ShellController {
QueryWrapper<LinuxAccess> linuxAccessQueryWrapper = new QueryWrapper<>();
linuxAccessQueryWrapper.eq("company_code", companyCode);
linuxAccessQueryWrapper.eq("uri", interfaceName);
linuxAccessQueryWrapper.isNull("delete_time");
LinuxAccess linuxAccess = linuxAccessService.getOne(linuxAccessQueryWrapper);
if(linuxAccess == null) {
return AjaxResult.error(HttpStatus.LINUX_ACCESS_ERROR, "linux访问控制不存在");
@ -96,6 +107,7 @@ public class ShellController {
QueryWrapper<QadAccess> qadAccessQueryWrapper = new QueryWrapper<>();
qadAccessQueryWrapper.eq("company_code", companyCode);
qadAccessQueryWrapper.eq("domain", domain);
qadAccessQueryWrapper.isNull("delete_time");
QadAccess qadAccess = qadAccessService.getOne(qadAccessQueryWrapper);
if(qadAccess == null) {
return AjaxResult.error(HttpStatus.QAD_ACCESS_ERROR, "QAD_ACCESS_ERROR");

2
win-admin/src/main/resources/application-dev.yml

@ -148,3 +148,5 @@ swagger:
enabled: true
# 请求前缀
pathMapping: /
faway-url:
dashboardtb: http://36.97.178.71:20202/services/infoapi?wsdl

2
win-admin/src/main/resources/application-prod.yml

@ -148,3 +148,5 @@ swagger:
enabled: false
# 请求前缀
pathMapping: /
faway-url:
dashboardtb: http://36.97.178.71:20202/services/infoapi?wsdl

2
win-admin/src/main/resources/application-test.yml

@ -148,3 +148,5 @@ swagger:
enabled: false
# 请求前缀
pathMapping: /
faway-url:
dashboardtb: http://36.97.178.71:20202/services/infoapi?wsdl

BIN
win-admin/src/main/resources/changchun.xls

Binary file not shown.

BIN
win-admin/src/main/resources/changchun.xlsx

Binary file not shown.

BIN
win-admin/src/main/resources/foshan.xls

Binary file not shown.

BIN
win-admin/src/main/resources/foshan_qingdao.xlsx

Binary file not shown.

BIN
win-admin/src/main/resources/guowai1.xls

Binary file not shown.

BIN
win-admin/src/main/resources/guowai1.xlsx

Binary file not shown.

BIN
win-admin/src/main/resources/guowai2.xls

Binary file not shown.

BIN
win-admin/src/main/resources/guowai2.xlsx

Binary file not shown.

12
win-admin/src/main/resources/logback-spring.xml

@ -35,7 +35,7 @@
<!-- 日志文件名格式 -->
<fileNamePattern>${log.path}/sys-debug.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- 日志最大的历史 7天 -->
<maxHistory>7</maxHistory>
<maxHistory>1</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${log.pattern}</pattern>
@ -58,7 +58,7 @@
<!-- 日志文件名格式 -->
<fileNamePattern>${log.path}/sys-info.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- 日志最大的历史 7天 -->
<maxHistory>7</maxHistory>
<maxHistory>1</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${log.pattern}</pattern>
@ -81,7 +81,7 @@
<!-- 日志文件名格式 -->
<fileNamePattern>${log.path}/sys-warn.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- 日志最大的历史 7天 -->
<maxHistory>7</maxHistory>
<maxHistory>1</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${log.pattern}</pattern>
@ -102,8 +102,8 @@
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 日志文件名格式 -->
<fileNamePattern>${log.path}/sys-error.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- 日志最大的历史 7-->
<maxHistory>7</maxHistory>
<!-- 日志最大的历史 2-->
<maxHistory>1</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${log.pattern}</pattern>
@ -130,7 +130,7 @@
</springProfile>
<!-- test环境 -->
<springProfile name="test">
<root level="INFO">
<root level="ERROR">
<appender-ref ref="console"/>
<appender-ref ref="file_warn"/>
<appender-ref ref="file_error"/>

BIN
win-admin/src/main/resources/foshan_qingdao.xls → win-admin/src/main/resources/qingdao.xls

Binary file not shown.

4
win-quartz/pom.xml

@ -34,6 +34,10 @@
<groupId>com.win</groupId>
<artifactId>win-common</artifactId>
</dependency>
<dependency>
<groupId>com.win</groupId>
<artifactId>win-system</artifactId>
</dependency>
</dependencies>

114
win-quartz/src/main/java/com/win/quartz/task/WinTask.java

@ -1,10 +1,28 @@
package com.win.quartz.task;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.win.common.core.domain.AjaxResult;
import com.win.common.utils.uuid.UUID;
import com.win.quartz.util.JsonToXmlConverter;
import com.win.system.domain.LinuxAccess;
import com.win.system.domain.QadAccess;
import com.win.system.service.LinuxAccessService;
import com.win.system.service.QadAccessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
/**
* 定时任务调度
*
@ -14,43 +32,71 @@ import org.springframework.web.client.RestTemplate;
@Component("winTask")
public class WinTask {
@Autowired
private LinuxAccessService linuxAccessService;
@Autowired
private QadAccessService qadAccessService;
@Autowired
private RestTemplate restTemplate;
@Value("${faway-url.dashboardtb:}")
private String dashboardtb;
public void haveParams(String params) {
// // 创建一个请求头对象
// HttpHeaders headers = new HttpHeaders();
// // 设置请求头对象contentTyp的为text/xml;charset=UTF-8
// MediaType type = MediaType.parseMediaType("text/xml;charset=UTF-8");
// headers.setContentType(type);
//
// // 将请求参数进行封装并进行远程接口服务调用
// // 构造webservice请求参数
// // 请求参数,根据实际情况进行配置,以下仅作为示例
// StringBuilder requestData = new StringBuilder();
// requestData.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
// requestData.append("<soapenv:Envelope xmlns:soapenv=\"http://根据实际情况配置\" xmlns:esb=\"http://根据实际情况配置\">");
// requestData.append("<soapenv:Header/>");
// requestData.append("<soapenv:Body>");
//
// // 请求参数,根据实际情况进行配置,以下仅作为示例
// requestData.append("<ESB_USER>").append("zhangsan").append("</ESB_USER>");
// requestData.append("<ESB_PWD>").append("123456").append("</ESB_PWD>");
//
// requestData.append("</soapenv:Body>");
// requestData.append("</soapenv:Envelope>");
//
// log.info("请求报文为:" + requestData.toString());
// log.info("url:" + url);
// try {
// // 创建请求
// HttpEntity<String> request = new HttpEntity<>(requestData.toString(), headers);
// // 发送post请求并获取到响应结果
// String str = restTemplate.postForObject(url, request, String.class);
// log.info("-----------Response content-----------: " + str);
// } catch (Exception e) {
// log.error("baseWebServiceRequest出现异常:" + e.getMessage(), e);
// }
//总账科目信息
public void fawaydashboardtb() throws IOException {
String companyCode = "HFLTESTDVL";
String interfaceName = "fawaydashboardtb";
String domain = "HFLCC";
QueryWrapper<LinuxAccess> linuxAccessQueryWrapper = new QueryWrapper<>();
linuxAccessQueryWrapper.eq("company_code",companyCode);
linuxAccessQueryWrapper.eq("uri", interfaceName);
LinuxAccess linuxAccess = linuxAccessService.getOne(linuxAccessQueryWrapper);
if(linuxAccess == null) {
throw new RuntimeException("linux访问控制不存在");
}
QueryWrapper<QadAccess> qadAccessQueryWrapper = new QueryWrapper<>();
qadAccessQueryWrapper.eq("company_code",companyCode);
qadAccessQueryWrapper.eq("domain",domain);
QadAccess qadAccess = qadAccessService.getOne(qadAccessQueryWrapper);
if(qadAccess == null) {
throw new RuntimeException("QAD访问控制不存在");
}
// 获取今天的日期
LocalDate today = LocalDate.now();
// 获取上个月的日期
LocalDate lastMonth = today.minusMonths(1);
// 格式化输出
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
String formattedMonth = lastMonth.format(formatter);
JSONArray jsonArray = new JSONArray();
JSONObject ttSearchCondition = new JSONObject();
ttSearchCondition.put("report_type", "tb");
ttSearchCondition.put("report_period", formattedMonth);
jsonArray.add(ttSearchCondition);
JSONObject dsSearchCondition = new JSONObject();
dsSearchCondition.put("dsSearchCondition", jsonArray);
String json = dsSearchCondition.toString();
UUID traceid = UUID.randomUUID();
AjaxResult ajaxResult = linuxAccessService.executeCommand(linuxAccess, qadAccess, traceid.toString(), json, domain);
JSONObject jsonObject = JSONObject.parseObject(ajaxResult.get("msg").toString());
jsonObject = JSONObject.parseObject(jsonObject.getString("dsTB"));
JSONArray dataJsonArray = JSONArray.parse(jsonObject.getString("TB"));
String xml = JsonToXmlConverter.jsonToXml(dataJsonArray);
// 创建一个请求头对象
HttpHeaders headers = new HttpHeaders();
// 设置请求头对象contentTyp的为text/xml;charset=UTF-8
MediaType type = MediaType.parseMediaType("text/xml;charset=UTF-8");
headers.setContentType(type);
log.info("请求报文为:" + xml);
log.info("url:" + this.dashboardtb);
try {
// 创建请求
HttpEntity<String> request = new HttpEntity<>(xml, headers);
// 发送post请求并获取到响应结果
String str = restTemplate.postForObject(this.dashboardtb, request, String.class);
log.info("-----------Response content-----------: " + str);
} catch (Exception e) {
log.error("baseWebServiceRequest出现异常:" + e.getMessage(), e);
}
}
}

61
win-quartz/src/main/java/com/win/quartz/util/JsonToXmlConverter.java

@ -0,0 +1,61 @@
package com.win.quartz.util;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
public class JsonToXmlConverter {
public static String jsonToXml(String jsonStr) {
JSONObject jsonObject = JSON.parseObject(jsonStr);
return jsonToXml(jsonObject, "root");
}
public static String jsonToXml(Object jsonObject) {
return jsonToXml(jsonObject, "root");
}
private static String jsonToXml(Object jsonObj, String tagName) {
StringBuilder xml = new StringBuilder();
if (jsonObj instanceof JSONObject) {
xml.append("<" + tagName + ">");
JSONObject jsonObject = (JSONObject) jsonObj;
for (String key : jsonObject.keySet()) {
Object value = jsonObject.get(key);
if (value instanceof JSONObject) {
xml.append(jsonToXml(value, key));
} else if (value instanceof JSONArray) {
xml.append(arrayToXml(key, (JSONArray) value));
} else {
xml.append("<" + key + ">" + value + "</" + key + ">");
}
}
xml.append("</" + tagName + ">");
} else if (jsonObj instanceof JSONArray) {
xml.append(arrayToXml(tagName, (JSONArray) jsonObj));
} else {
xml.append("<" + tagName + ">" + jsonObj.toString() + "</" + tagName + ">");
}
return xml.toString();
}
private static String arrayToXml(String tagName, JSONArray jsonArray) {
StringBuilder xml = new StringBuilder();
for (int i = 0; i < jsonArray.size(); i++) {
Object jsonObj = jsonArray.get(i);
if (jsonObj instanceof JSONObject) {
xml.append(jsonToXml(jsonObj, tagName));
} else {
xml.append("<" + tagName + ">" + jsonObj.toString() + "</" + tagName + ">");
}
}
return xml.toString();
}
public static void main(String[] args) {
String jsonStr = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}";
String xmlStr = jsonToXml(jsonStr);
System.out.println(xmlStr);
}
}
Loading…
Cancel
Save