From e1c7bc68c764aff2d55d8a64dc5ad6c644df47b0 Mon Sep 17 00:00:00 2001 From: "HUOJIN\\92525" <925259474@qq.com> Date: Mon, 1 Sep 2025 19:42:10 +0800 Subject: [PATCH] no message --- .../java/com/youchain/utils/HttpPostUtil.java | 16 +++-- .../service/impl/LesServiceImpl.java | 58 +++++++++++++++---- 2 files changed, 54 insertions(+), 20 deletions(-) diff --git a/youchain-common/src/main/java/com/youchain/utils/HttpPostUtil.java b/youchain-common/src/main/java/com/youchain/utils/HttpPostUtil.java index 480a4af..192876d 100644 --- a/youchain-common/src/main/java/com/youchain/utils/HttpPostUtil.java +++ b/youchain-common/src/main/java/com/youchain/utils/HttpPostUtil.java @@ -1,19 +1,16 @@ package com.youchain.utils; -import lombok.extern.slf4j.Slf4j; import org.apache.commons.compress.utils.IOUtils; import java.io.DataOutputStream; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; -import java.nio.charset.StandardCharsets; -@Slf4j public class HttpPostUtil { public static String sendPostReq(String api_url, String request) { - InputStream instr; - String str = ""; + InputStream instr = null; + String str = null; try { URL url = new URL(api_url); URLConnection urlCon = url.openConnection(); @@ -31,13 +28,14 @@ public class HttpPostUtil { printout.close(); instr = urlCon.getInputStream(); byte[] bis = IOUtils.toByteArray(instr); - String ResponseString = new String(bis, StandardCharsets.UTF_8); - if (StringUtils.isEmpty(ResponseString)) { - log.error("返回空"); + String ResponseString = new String(bis, "UTF-8"); + if ((ResponseString == null) || ("".equals(ResponseString.trim()))) { + System.out.println("返回空"); } str = ResponseString; } catch (Exception e) { - log.error("接口异常!"); + System.out.println("接口异常!"); + // throw new Error(e.getMessage()); } return str; } diff --git a/youchain-system/src/main/java/com/youchain/businessdata/service/impl/LesServiceImpl.java b/youchain-system/src/main/java/com/youchain/businessdata/service/impl/LesServiceImpl.java index d71420f..780ed8d 100644 --- a/youchain-system/src/main/java/com/youchain/businessdata/service/impl/LesServiceImpl.java +++ b/youchain-system/src/main/java/com/youchain/businessdata/service/impl/LesServiceImpl.java @@ -25,8 +25,13 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.w3c.dom.Document; +import org.w3c.dom.Element; import javax.servlet.http.HttpServletResponse; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import java.io.ByteArrayInputStream; import java.io.IOException; import java.sql.Timestamp; import java.text.SimpleDateFormat; @@ -204,18 +209,29 @@ public class LesServiceImpl implements LesService { //返回结果 resultJson = HttpPostUtil.sendPostReq(url, json); - if (StringUtils.isEmpty(resultJson)) { - throw new BadRequestException("LES返回信息:LES回传接口调用失败!"); - } - JSONObject resulObject = JSON.parseObject(resultJson); - if (resulObject == null) { - throw new BadRequestException("LES返回信息:LES回传接口返回为空!"); - } + log.info("LES返回报文:{}", resultJson); + Map result = parseResponse(resultJson); + if (result == null) { + if (StringUtils.isEmpty(resultJson)) { + throw new BadRequestException("LES返回信息:LES回传接口调用失败!"); + } - String resultCode = resulObject.getString("resultCode"); - String displayMsg = resulObject.getString("displayMsg"); - if (!"success".equals(resultCode)) { - throw new BadRequestException("LES返回消息:" + displayMsg); + JSONObject resulObject = JSON.parseObject(resultJson); + if (resulObject == null) { + throw new BadRequestException("LES返回信息:LES回传接口返回为空!"); + } + + String resultCode = resulObject.getString("resultCode"); + String displayMsg = resulObject.getString("displayMsg"); + if (!"success".equals(resultCode)) { + throw new BadRequestException("LES返回消息:" + displayMsg); + } + } else { + String resultCode = result.get("resultCode"); + String displayMsg = result.get("displayMsg"); + if (!"success".equals(resultCode)) { + throw new BadRequestException("LES返回消息:" + displayMsg); + } } } @@ -226,6 +242,26 @@ public class LesServiceImpl implements LesService { lesRepository.save(les); } + + public static Map parseResponse(String xmlString) { + try { + Map map = new HashMap<>(); + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + Document document = builder.parse(new ByteArrayInputStream(xmlString.getBytes())); + + Element root = document.getDocumentElement(); + String resultCode = root.getElementsByTagName("resultCode").item(0).getTextContent(); + String displayMsg = root.getElementsByTagName("displayMsg").item(0).getTextContent(); + map.put("resultCode", resultCode); + map.put("displayMsg", displayMsg); + return map; + + } catch (Exception e) { + return null; + } + } + @Override @Transactional(rollbackFor = Exception.class) public void arrivedLes(Long agvTaskId, String currentPositionCode) {