自定义报文格式
1 <?xml version="1.0" encoding="gbk"?> 2 <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> 3 <soap:Header> 4 <out:system xmlns:out="http://www.ylzinfo.com/"></out:system> 5 </soap:Header> 6 <soap:Body> 7 <out:business xmlns:out="http://www.ylzinfo.com/"> 8 <result showtype="1" /> 9 <resultset name="retrieve"> 10 <row resultCode="0" resultMessage="请求成功" resultBody="null" /> 11 <row cloudPath="www.baidu.com" fileType="txt" fileTradeNo="12345678" fileDesc="测试" /> 12 </resultset> 13 </out:business> 14 <soap:Fault> 15 <faultcode>0</faultcode> 16 <faultstring>请求成功</faultstring> 17 <channelNo>1400000001</channelNo> 18 <signNo>1111111111111111111</signNo> 19 <aac002>373738383927711231</aac002> 20 <aaz999>1234567890</aaz999> 21 <aaz333>12312312312</aaz333> 22 <aae036>1578988937192</aae036> 23 <pageSize></pageSize> 24 <cPage></cPage> 25 <fileFlag>1</fileFlag> 26 </soap:Fault> 27 </soap:Body> 28 </soap:Envelope>
测试类:
1 import com.alibaba.fastjson.JSON; 2 3 import com.socialsecurity.domain.ResultCode; 4 import com.socialsecurity.domain.soap.FileDataBody; 5 import com.socialsecurity.domain.soap.ResultDataBody; 6 import com.socialsecurity.domain.soap.SoapDatagram; 7 import com.socialsecurity.util.EsbUtil; 8 import com.socialsecurity.util.SoapUtil; 9 10 import java.util.ArrayList; 11 import java.util.List; 12 import java.util.Map; 13 14 15 public class DatagramTest { 16 17 public static void main(String[] args) { 18 ResultDataBody outData = new ResultDataBody(ResultCode.BUSINESS_SUCCESS, "请求成功"); 19 FileDataBody fileDataBody = new FileDataBody(); 20 fileDataBody.setCloudPath("www.baidu.com"); 21 fileDataBody.setFileType("txt"); 22 fileDataBody.setFileTradeNo("12345678"); 23 fileDataBody.setFileDesc("测试"); 24 25 List files = new ArrayList<>(); 26 files.add(fileDataBody); 27 28 SoapDatagram datagram = new SoapDatagram.Builder(ResultCode.BUSINESS_SUCCESS, "请求成功") 29 .channelNo("1400000001") 30 .signNo("1111111111111111111") 31 .aac002("373738383927711231") 32 .aaz333("12312312312") 33 .aaz999("1234567890") 34 .aae036("1578988937192") 35 .outData(outData) 36 .files(files) 37 .build(); 38 39 String xml = SoapUtil.readerSoapXMlOut(datagram); 40 System.out.println(xml); 41 42 Map map = EsbUtil.parseDataV2(xml); 43 System.out.println(JSON.toJSONString(map)); 44 } 45 }
1 package com.socialsecurity.domain.soap; 2 3 import java.io.Serializable; 4 5 6 7 public class ResultDataBody implements Serializable { 8 9 10 /** 11 * 业务响应码 12 */ 13 private String resultCode; 14 15 /** 16 * 业务响应消息 17 */ 18 private String resultMessage; 19 20 /** 21 * 响应结果 22 */ 23 private String resultBody; 24 25 26 public ResultDataBody(String resultCode, String resultMessage) { 27 this.resultCode = resultCode; 28 this.resultMessage = resultMessage; 29 } 30 31 public ResultDataBody(String resultCode, String resultMessage, String resultBody) { 32 this.resultCode = resultCode; 33 this.resultMessage = resultMessage; 34 this.resultBody = resultBody; 35 } 36 37 public String getResultCode() { 38 return resultCode; 39 } 40 41 public void setResultCode(String resultCode) { 42 this.resultCode = resultCode; 43 } 44 45 public String getResultMessage() { 46 return resultMessage; 47 } 48 49 public void setResultMessage(String resultMessage) { 50 this.resultMessage = resultMessage; 51 } 52 53 public String getResultBody() { 54 return resultBody; 55 } 56 57 public void setResultBody(String resultBody) { 58 this.resultBody = resultBody; 59 } 60 }
1 package com.socialsecurity.domain.soap; 2 3 4 public class FileDataBody { 5 6 7 /** 8 * 云文件存储地址 9 */ 10 private String cloudPath; 11 12 /** 13 * 文件类型 14 */ 15 private String fileType; 16 17 /** 18 * 文件编号 19 */ 20 private String fileTradeNo; 21 22 /** 23 * 文件描述 24 */ 25 private String fileDesc; 26 27 public FileDataBody() { 28 } 29 30 public FileDataBody(String cloudPath, String fileType, String fileTradeNo, String fileDesc) { 31 this.cloudPath = cloudPath; 32 this.fileType = fileType; 33 this.fileTradeNo = fileTradeNo; 34 this.fileDesc = fileDesc; 35 } 36 37 public String getCloudPath() { 38 return cloudPath; 39 } 40 41 public void setCloudPath(String cloudPath) { 42 this.cloudPath = cloudPath; 43 } 44 45 public String getFileType() { 46 return fileType; 47 } 48 49 public void setFileType(String fileType) { 50 this.fileType = fileType; 51 } 52 53 public String getFileTradeNo() { 54 return fileTradeNo; 55 } 56 57 public void setFileTradeNo(String fileTradeNo) { 58 this.fileTradeNo = fileTradeNo; 59 } 60 61 public String getFileDesc() { 62 return fileDesc; 63 } 64 65 public void setFileDesc(String fileDesc) { 66 this.fileDesc = fileDesc; 67 } 68 }
1 package com.socialsecurity.domain.soap; 2 3 import java.util.List; 4 5 6 public class SoapDatagram { 7 8 /** 9 * 系统响应码 10 */ 11 private String faultCode; 12 13 /** 14 * 系统响应消息 15 */ 16 private String faultstring; 17 18 /** 19 * 渠道编号 20 */ 21 private String channelNo; 22 23 /** 24 * 签发号 25 */ 26 private String signNo; 27 28 /** 29 * 保障号码 30 */ 31 private String aac002; 32 /** 33 * 流水号 34 */ 35 private String aaz999; 36 /** 37 * XX编码 38 */ 39 private String aaz333; 40 41 /** 42 * 时间戳 43 */ 44 private String aae036; 45 46 /** 47 * 每页行数 48 */ 49 private String pageSize; 50 /** 51 * 当前页 52 */ 53 private String cPage; 54 55 /** 56 * 是否上传附件标识 57 * 0无附件 1存在附件 58 */ 59 private String fileFlag; 60 61 62 /** 63 * 出参数集 64 */ 65 private ResultDataBody outdata; 66 67 /** 68 * 附件集 69 */ 70 private List<FileDataBody> files; 71 72 public SoapDatagram() { 73 } 74 75 private SoapDatagram(Builder builder) { 76 this.faultCode = builder.faultCode; 77 this.faultstring = builder.faultstring; 78 this.channelNo = builder.channelNo; 79 this.signNo = builder.signNo; 80 this.aac002 = builder.aac002; 81 this.aaz999 = builder.aaz999; 82 this.aaz333 = builder.aaz333; 83 this.aae036 = builder.aae036; 84 this.pageSize = builder.pageSize; 85 this.cPage = builder.cPage; 86 this.fileFlag = builder.fileFlag; 87 this.outdata = builder.outdata; 88 this.files = builder.files; 89 } 90 91 public static class Builder { 92 private String faultCode; 93 private String faultstring; 94 private String channelNo; 95 private String signNo; 96 private String aac002; 97 private String aaz999; 98 private String aaz333; 99 private String aae036; 100 private String pageSize; 101 private String cPage; 102 private String fileFlag = "0"; 103 private ResultDataBody outdata; 104 private List<FileDataBody> files; 105 106 public Builder(String faultCode, String faultstring) { 107 this.faultCode = faultCode; 108 this.faultstring = faultstring; 109 } 110 111 112 public Builder channelNo(String channelNo) { 113 this.channelNo = channelNo; 114 return this; 115 } 116 117 public Builder signNo(String signNo) { 118 this.signNo = signNo; 119 return this; 120 } 121 122 public Builder aac002(String aac002) { 123 this.aac002 = aac002; 124 return this; 125 } 126 127 public Builder aaz999(String aaz999) { 128 this.aaz999 = aaz999; 129 return this; 130 } 131 132 public Builder aaz333(String aaz333) { 133 this.aaz333 = aaz333; 134 return this; 135 } 136 137 public Builder aae036(String aae036) { 138 this.aae036 = aae036; 139 return this; 140 } 141 142 public Builder pageHelper(String pageSize, String cPage) { 143 this.pageSize = pageSize; 144 this.cPage = cPage; 145 return this; 146 } 147 148 149 public Builder outData(ResultDataBody outdata) { 150 this.outdata = outdata; 151 return this; 152 } 153 154 public Builder files(List<FileDataBody> files) { 155 if (files.size() > 0) this.fileFlag = "1"; 156 this.files = files; 157 return this; 158 } 159 160 161 public SoapDatagram build() { 162 return new SoapDatagram(this); 163 } 164 165 } 166 167 168 public String getFaultCode() { 169 return faultCode; 170 } 171 172 public void setFaultCode(String faultCode) { 173 this.faultCode = faultCode; 174 } 175 176 public String getFaultstring() { 177 return faultstring; 178 } 179 180 public void setFaultstring(String faultstring) { 181 this.faultstring = faultstring; 182 } 183 184 public String getChannelNo() { 185 return channelNo; 186 } 187 188 public void setChannelNo(String channelNo) { 189 this.channelNo = channelNo; 190 } 191 192 public String getSignNo() { 193 return signNo; 194 } 195 196 public void setSignNo(String signNo) { 197 this.signNo = signNo; 198 } 199 200 public String getAac002() { 201 return aac002; 202 } 203 204 public void setAac002(String aac002) { 205 this.aac002 = aac002; 206 } 207 208 public String getAaz999() { 209 return aaz999; 210 } 211 212 public void setAaz999(String aaz999) { 213 this.aaz999 = aaz999; 214 } 215 216 public String getAaz333() { 217 return aaz333; 218 } 219 220 public void setAaz333(String aaz333) { 221 this.aaz333 = aaz333; 222 } 223 224 public String getAae036() { 225 return aae036; 226 } 227 228 public void setAae036(String aae036) { 229 this.aae036 = aae036; 230 } 231 232 public String getPageSize() { 233 return pageSize; 234 } 235 236 public void setPageSize(String pageSize) { 237 this.pageSize = pageSize; 238 } 239 240 public String getcPage() { 241 return cPage; 242 } 243 244 public void setcPage(String cPage) { 245 this.cPage = cPage; 246 } 247 248 public String getFileFlag() { 249 return fileFlag; 250 } 251 252 public void setFileFlag(String fileFlag) { 253 this.fileFlag = fileFlag; 254 } 255 256 public ResultDataBody getOutdata() { 257 return outdata; 258 } 259 260 public void setOutdata(ResultDataBody outdata) { 261 this.outdata = outdata; 262 } 263 264 public List<FileDataBody> getFiles() { 265 return files; 266 } 267 268 public void setFiles(List<FileDataBody> files) { 269 this.files = files; 270 } 271 }
1 package com.socialsecurity.util; 2 3 4 public class SoapEnvelop { 5 6 public static final String ENVELOP_END = "</soap:Envelope>"; 7 8 public static final String BODY_END = "</soap:Body>"; 9 10 public static final String BODY_START = "<soap:Body>"; 11 12 public static final String BUSINESS_START="<out:business xmlns:out=\"http://www.ylzinfo.com/\">"; 13 14 public static final String BUSINESS_END="</out:business>"; 15 16 public static final String HEAD_END = "</out:system></soap:Header>"; 17 18 public static final String HEAD_START = "<soap:Header><out:system xmlns:out=\"http://www.ylzinfo.com/\">"; 19 20 public static final String ENVELOP_START = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" soap:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"; 21 22 public static final String XML_PI = "<?xml version=\"1.0\" encoding=\"GBK\"?>"; 23 24 public static final String TotalNum_Start= "<result totalnum=\""; 25 public static final String TotalNum_End= "\" />"; 26 27 public static final String CPAGE_Start= "<result cpage=\""; 28 public static final String CPAGE_End= "\" />"; 29 30 public static final String PAGES_Start= "<result pages=\""; 31 public static final String PAGES_End= "\" />"; 32 33 public static final String ROWCOUNT_Start= "<result rowCount=\""; 34 public static final String ROWCOUNT_End= "\" />"; 35 36 public static final String Single_Row= "<result showtype=\"1\"/>"; 37 38 public static final String Show_Type_Result= "</result>"; 39 40 public static final String ServiceID_Start= "<result serviceId=\""; 41 public static final String ServiceID_End= "\"/>"; 42 43 public static final String Multilateral_Row= "<result showtype=\"2\"/>"; 44 45 public static final String Structs_START = "<resultset name=\"structs\">"; 46 public static final String Structs_END = "</resultset>"; 47 48 public static final String Retrieve_START = "<resultset name=\"retrieve\">"; 49 public static final String Retrieve_END = "</resultset>"; 50 51 public static final String Message_START = "<resultset name=\"retrieve\" information=\""; 52 public static final String Message_R= "\" >"; 53 public static final String Message_End = "</resultset>"; 54 public static final String Single= "1"; 55 56 public static final String Multilateral= "2"; 57 58 public static final String FAULT_START="<soap:Fault>"; 59 public static final String FAULT_END="</soap:Fault>"; 60 61 public static final String FAULT_CODE_START="<faultcode>"; 62 public static final String FAULT_CODE_END="</faultcode>"; 63 64 public static final String FAULT_STR_START="<faultstring>"; 65 public static final String FAULT_STR_END="</faultstring>"; 66 67 68 public static final String ERROR_MSG_START="<error msg=\""; 69 public static final String ERROR_MSG_END="\" />"; 70 public static final String REVERSAL_RESULT_START="<result>"; 71 public static final String REVERSAL_RESULT_END="</result>"; 72 public static final String REVERSAL_SUCCESS_START="<success>"; 73 public static final String REVERSAL_SUCCESS_END="</success>"; 74 public static final String REVERSAL_URL_START="<url>"; 75 public static final String REVERSAL_URL_END="</url>"; 76 public static final String REVERSAL_SQL_START="<sql>"; 77 public static final String REVERSAL_SQL_END="</sql>"; 78 public static final String REVERSAL_METHOD_START="<method>"; 79 public static final String REVERSAL_METHOD_END="</method>"; 80 public static final String REVERSAL_NAMESPACE_START="<namespace>"; 81 public static final String REVERSAL_NAMESPACE_END="</namespace>"; 82 public static final String REVERSAL_ERRORMSG_START="<errorMsg>"; 83 public static final String REVERSAL_ERRORMSG_END="</errorMsg>"; 84 public static final String REVERSAL_PARA_START="<para "; 85 86 87 88 //流水号 89 public static final String ERROR_AAZ999_START="aaz999=\""; 90 public static final String ERROR_AAZ999_END="\" "; 91 92 //时间戳 93 public static final String ERROR_AAE036_START="aae036=\""; 94 public static final String ERROR_AAE036_END="\""; 95 96 /** 97 * 构造要发送的SOAP消息 98 * 99 * @param head 100 * SOAP验证头 101 * @param env 102 * 消息体 103 * @return 104 */ 105 public static String buildSOAP(String head, String env) { 106 StringBuffer soapBuf = new StringBuffer(); 107 { 108 soapBuf.append(XML_PI); 109 soapBuf.append(ENVELOP_START); 110 111 soapBuf.append(HEAD_START); 112 113 soapBuf.append(head); 114 115 soapBuf.append(HEAD_END); 116 117 soapBuf.append(BODY_START); 118 soapBuf.append(getSoapBody(env)); 119 soapBuf.append(BODY_END); 120 121 soapBuf.append(ENVELOP_END); 122 } 123 124 return soapBuf.toString(); 125 } 126 127 /** 128 * 获取消息信息体 129 * 130 * @param envelop 131 * 消息信封 132 * @return 返回消息体 133 */ 134 public static String getSoapBody(String envelop) { 135 if (null == envelop) { 136 return ""; 137 } 138 if (envelop.indexOf(BODY_START) < 0) 139 return ""; 140 141 return envelop.substring(envelop.indexOf(BODY_START) 142 + BODY_START.length(), envelop.indexOf(BODY_END)); 143 } 144 }
1 package com.socialsecurity.util; 2 3 4 import com.socialsecurity.domain.soap.FileDataBody; 5 import com.socialsecurity.domain.soap.ResultDataBody; 6 import com.socialsecurity.domain.soap.SoapDatagram; 7 import org.apache.commons.lang3.StringUtils; 8 9 import java.util.List; 10 import java.util.Map; 11 12 public class SoapUtil { 13 14 public static String readerSoapXMlOut(SoapDatagram datagram) { 15 //构建标准Soap输出规范 16 StringBuffer reXML = new StringBuffer(); 17 reXML.append(SoapEnvelop.XML_PI) 18 .append(SoapEnvelop.ENVELOP_START) //信封 19 .append(SoapEnvelop.HEAD_START) //soap:header 20 .append(SoapEnvelop.HEAD_END) 21 .append(SoapEnvelop.BODY_START); //soap:body 22 //业务数据 23 reXML.append(SoapEnvelop.BUSINESS_START); //out:business 24 reXML.append(SoapEnvelop.Single_Row); 25 reXML.append(SoapEnvelop.Retrieve_START); 26 27 //业务出参集 28 ResultDataBody resultDataBody = datagram.getOutdata(); 29 if (null != resultDataBody) { 30 reXML.append("<row "); 31 reXML.append("resultCode =\"" + resultDataBody.getResultCode() + "\" "); 32 reXML.append("resultMessage =\"" + resultDataBody.getResultMessage() + "\" "); 33 reXML.append("resultBody =\"" + resultDataBody.getResultBody() + "\" "); 34 reXML.append(" />"); 35 } 36 37 38 //数据集 39 if ("1".equals(datagram.getFileFlag())) { 40 List<FileDataBody> files = datagram.getFiles(); 41 for (FileDataBody file : files) { 42 String cloudPath = file.getCloudPath(); 43 String fileType = file.getFileType(); 44 String fileTradeNo = file.getFileTradeNo(); 45 String fileDesc = file.getFileDesc(); 46 reXML.append("<row "); 47 reXML.append("cloudPath =\"" + cloudPath + "\" "); 48 reXML.append("fileType =\"" + fileType + "\" "); 49 reXML.append("fileTradeNo =\"" + fileTradeNo + "\" "); 50 reXML.append("fileDesc =\"" + fileDesc + "\" "); 51 reXML.append(" />"); 52 } 53 } 54 55 56 reXML.append(SoapEnvelop.Retrieve_END) 57 .append(SoapEnvelop.BUSINESS_END); 58 59 60 //系统公共响应参数 61 reXML.append(SoapEnvelop.FAULT_START) //soap:fault 62 .append(SoapEnvelop.FAULT_CODE_START) //faultcode 63 .append(datagram.getFaultCode()) 64 .append(SoapEnvelop.FAULT_CODE_END) 65 .append(SoapEnvelop.FAULT_STR_START) //faultstring 66 // .append(SoapEnvelop.ERROR_MSG_START) //error msg 67 .append(datagram.getFaultstring()) 68 // .append(SoapEnvelop.ERROR_MSG_END) 69 .append(SoapEnvelop.FAULT_STR_END) 70 .append("<channelNo>" + datagram.getChannelNo() + "</channelNo>") 71 .append("<signNo>" + datagram.getSignNo() + "</signNo>") 72 .append("<aac002>" + datagram.getAac002() + "</aac002>") 73 .append("<aaz999>" + datagram.getAaz999() + "</aaz999>") 74 .append("<aaz333>" + datagram.getAaz333() + "</aaz333>") 75 .append("<aae036>" + datagram.getAae036() + "</aae036>") 76 .append("<pageSize>" + StringUtils.defaultString(datagram.getPageSize()) + "</pageSize>") 77 .append("<cPage>" + StringUtils.defaultString(datagram.getcPage()) + "</cPage>") 78 .append("<fileFlag>" + datagram.getFileFlag() + "</fileFlag>") 79 80 81 .append(SoapEnvelop.FAULT_END); 82 83 reXML.append(SoapEnvelop.BODY_END) 84 .append(SoapEnvelop.ENVELOP_END); 85 return reXML.toString(); 86 } 87 88 public static String readerSoapXMlOutList(String[] structs, List value) { 89 //构建标准Soap输出规范 90 StringBuffer reXML = new StringBuffer(); 91 reXML.append(SoapEnvelop.XML_PI) 92 .append(SoapEnvelop.ENVELOP_START) //信封 93 .append(SoapEnvelop.HEAD_START) //soap:header 94 .append(SoapEnvelop.HEAD_END) 95 .append(SoapEnvelop.BODY_START); //soap:body 96 reXML.append(SoapEnvelop.BUSINESS_START); //out:business 97 reXML.append(SoapEnvelop.Single_Row); 98 reXML.append(SoapEnvelop.Retrieve_START); 99 if (!(null == value) && value.size() > 0) { 100 for (int j = 0; j < value.size(); j++) { 101 reXML.append("<row "); 102 Map<String, Object> valueMap = (Map) value.get(j); 103 for (int i = 0; i < structs.length; i++) { 104 reXML.append(structs[i] + "=\"" + (valueMap.get(structs[i]) == null ? "" : valueMap.get(structs[i])) + "\" "); 105 } 106 reXML.append(" />"); 107 } 108 } 109 reXML.append(SoapEnvelop.Retrieve_END) 110 .append(SoapEnvelop.BUSINESS_END) 111 .append(SoapEnvelop.BODY_END) 112 .append(SoapEnvelop.ENVELOP_END); 113 return reXML.toString(); 114 } 115 116 public static String BuildSoapXMlError(String errorCode, String errorMsg) { 117 //构建标准Soap输出规范 118 StringBuffer reXML = new StringBuffer(SoapEnvelop.XML_PI); 119 reXML.append(SoapEnvelop.ENVELOP_START) //信封 120 .append(SoapEnvelop.HEAD_START) //soap:header 121 .append(SoapEnvelop.HEAD_END) 122 .append(SoapEnvelop.BODY_START); //soap:body 123 reXML.append(SoapEnvelop.FAULT_START) //soap:fault 124 .append(SoapEnvelop.FAULT_CODE_START) //faultcode 125 .append(errorCode) 126 .append(SoapEnvelop.FAULT_CODE_END) 127 .append(SoapEnvelop.FAULT_STR_START) //faultstring 128 .append(SoapEnvelop.ERROR_MSG_START) //error msg 129 .append(errorMsg) 130 .append(SoapEnvelop.ERROR_MSG_END) 131 .append(SoapEnvelop.FAULT_STR_END) 132 .append(SoapEnvelop.FAULT_END); 133 reXML.append(SoapEnvelop.BODY_END) 134 .append(SoapEnvelop.ENVELOP_END); 135 return reXML.toString(); 136 } 137 138 public static String readerSoapXMlOut(String[] structs, String[] value, String structsname[]) { 139 //构建标准Soap输出规范 140 StringBuffer reXML = new StringBuffer(); 141 reXML.append(SoapEnvelop.XML_PI) 142 .append(SoapEnvelop.ENVELOP_START) //信封 143 .append(SoapEnvelop.HEAD_START) //soap:header 144 .append(SoapEnvelop.HEAD_END) 145 .append(SoapEnvelop.BODY_START); //soap:body 146 reXML.append(SoapEnvelop.BUSINESS_START); //out:business 147 reXML.append(SoapEnvelop.Single_Row); 148 reXML.append(SoapEnvelop.Structs_START); 149 for (int i = 0; i < structsname.length; i++) { 150 reXML.append("<row label=\"" + structsname[i] + "\" columnname=\"" + structs[i] + "\" typename=\"Varchar2\" displaysize=\"20\"/>"); 151 } 152 reXML.append(SoapEnvelop.Structs_END); 153 reXML.append(SoapEnvelop.Retrieve_START); 154 reXML.append("<row "); 155 for (int i = 0; i < structs.length; i++) { 156 reXML.append(structs[i] + "=\"" + value[i] + "\" "); 157 } 158 reXML.append(" />"); 159 reXML.append(SoapEnvelop.Retrieve_END) 160 .append(SoapEnvelop.BUSINESS_END) 161 .append(SoapEnvelop.BODY_END) 162 .append(SoapEnvelop.ENVELOP_END); 163 return reXML.toString(); 164 } 165 }
1 package com.socialsecurity.util; 2 3 import com.google.common.collect.Lists; 4 import com.google.common.collect.Maps; 5 import com.eva.core.web.validate.ResponseCode; 6 import com.esb.client.XMLRequest; 7 import org.dom4j.Attribute; 8 import org.dom4j.Document; 9 import org.dom4j.DocumentHelper; 10 import org.dom4j.Element; 11 12 import java.util.*; 13 14 public class EsbUtil { 15 16 private XMLRequest xmlRequest; 17 18 public XMLRequest getXmlRequest() { 19 return xmlRequest; 20 } 21 22 public void setXmlRequest(XMLRequest xmlRequest) { 23 this.xmlRequest = xmlRequest; 24 } 25 26 /** 27 * 返回List 数组 28 * 29 * @param xmlStr 30 * @return 31 */ 32 public static Object parseData(String xmlStr) { 33 // 解析xml文档 34 List allData = new ArrayList(); 35 try { 36 //根据xml文本获取Document对象 37 Document doc = DocumentHelper.parseText(xmlStr); 38 //获取根节点元素对象 39 Element root = doc.getRootElement(); 40 //获取所有的节点 41 List<Element> rows = root.elements(); 42 //获取xml的身体数据 43 List<Element> bodyData = rows.get(1).elements(); 44 String faultcode = ""; 45 String msg = ""; 46 //结构体 47 List<Element> resultsetList = new ArrayList<Element>(); 48 List<Element> resultList = new ArrayList<Element>(); 49 List<String> informationList = new ArrayList<String>(); 50 List<String> errorMsgList = new ArrayList<String>(); 51 for (Element bodyChild : bodyData) { 52 List<Element> business = bodyChild.elements(); 53 for (Element e : business) { 54 //获取其属性 55 if ("result".equals(e.getQName().getName())) { 56 resultList.add(e); 57 } else if ("retrieve".equals(e.attributeValue("name"))) { 58 resultsetList.add(e); 59 } 60 // 提示信息 61 if (e.attribute("information") != null) { 62 informationList.add(e.attribute("information") 63 .getValue()); 64 } 65 // 出错信息 66 if ("faultcode".equals(e.getName())) { 67 faultcode = e.getData().toString(); 68 } 69 if ("faultstring".equals(e.getName())) { 70 List<Element> errorList = e.elements(); 71 for (Element errorElement : errorList) { 72 errorMsgList 73 .add(errorElement.attribute("msg") == null ? null 74 : errorElement.attribute("msg") 75 .getValue()); 76 } 77 } 78 } 79 } 80 if ((informationList.size() >= 0 && errorMsgList.size() == 0)) { 81 for (Element resultset : resultsetList) { 82 int retrieveLen = 0; 83 // 解析每个字段名 84 for (Iterator<?> retrieveRow = resultset.elementIterator(); retrieveRow 85 .hasNext(); retrieveLen++) // 记录数--行数 86 { 87 Element roweValueElement = (Element) retrieveRow.next();// soap 88 // 结构体 89 // retrieve 90 HashMap<String, Object> data = new HashMap<String, Object>(); 91 List<Attribute> listAttr = roweValueElement.attributes(); 92 for (Attribute attribute : listAttr) { 93 data.put(attribute.getName(), attribute.getValue()); 94 } 95 allData.add(data); 96 } 97 } 98 } 99 for (String object : errorMsgList) { 100 msg += object; 101 } 102 if (!"".equals(msg)) { 103 HashMap map = new HashMap(); 104 map.put("code", faultcode); 105 map.put("msg", msg); 106 allData.add(map); 107 } 108 return allData; 109 } catch (Exception e) { 110 HashMap map = new HashMap(); 111 map.put("code", "800"); 112 map.put("msg", "报文解析错误"); 113 allData.add(map); 114 return allData; 115 } 116 } 117 118 /** 119 * 分页解析 120 * 121 * @param xmlStr 122 * @return Map key为 page 值为map{‘cpage’,‘pages’,‘rowCount’} 123 * key 为 msg 值为map{‘code’,‘msg} 124 * key 为 data 值为list 125 */ 126 public static Map parsePageData(String xmlStr) { 127 // 解析xml文档 128 Map allData = Maps.newHashMap(); 129 try { 130 //根据xml文本获取Document对象 131 Document doc = DocumentHelper.parseText(xmlStr); 132 //获取根节点元素对象 133 Element root = doc.getRootElement(); 134 //获取所有的节点 135 List<Element> rows = root.elements(); 136 //获取xml的身体数据 137 List<Element> bodyData = rows.get(1).elements(); 138 //分页对象 139 Map<String, String> pageMap = Maps.newHashMap(); 140 String faultcode = ""; 141 String msg = ""; 142 //结构体 143 List<Element> resultsetList = new ArrayList<Element>(); 144 List<Element> resultList = new ArrayList<Element>(); 145 List<String> informationList = new ArrayList<String>(); 146 List<String> errorMsgList = new ArrayList<String>(); 147 for (Element bodyChild : bodyData) { 148 List<Element> business = bodyChild.elements(); 149 for (Element e : business) { 150 //获取其属性 151 if ("result".equals(e.getQName().getName())) { 152 resultList.add(e); 153 } else if ("retrieve".equals(e.attributeValue("name"))) { 154 resultsetList.add(e); 155 } 156 // 提示信息 157 if (e.attribute("information") != null) { 158 informationList.add(e.attribute("information") 159 .getValue()); 160 errorMsgList.add(e.attribute("information") 161 .getValue()); 162 } 163 // 出错信息 164 if ("faultcode".equals(e.getName())) { 165 faultcode = String.valueOf(e.getData()); 166 } 167 if ("faultstring".equals(e.getName())) { 168 List<Element> errorList = e.elements(); 169 for (Element errorElement : errorList) { 170 errorMsgList 171 .add(errorElement.attribute("msg") == null ? null 172 : errorElement.attribute("msg") 173 .getValue()); 174 } 175 } 176 } 177 } 178 if (resultList.size() > 0) { 179 for (Element element : resultList) { 180 List<Attribute> list = element.attributes(); 181 for (Attribute attribute : list) { 182 pageMap.put(attribute.getName(), attribute.getValue()); 183 } 184 } 185 allData.put("page", pageMap); 186 } 187 if ((informationList.size() >= 0 && errorMsgList.size() == 0)) { 188 allData.put("code", "0"); 189 allData.put("msg", "调用成功"); 190 List list = Lists.newArrayList(); 191 for (Element resultset : resultsetList) { 192 // 解析每个字段名 193 for (Iterator<?> retrieveRow = resultset.elementIterator(); retrieveRow 194 .hasNext(); ) // 记录数--行数 195 { 196 Element roweValueElement = (Element) retrieveRow.next(); 197 // 结构体 198 // retrieve 199 HashMap data = Maps.newHashMap(); 200 List<Attribute> listAttr = roweValueElement.attributes(); 201 for (Attribute attribute : listAttr) { 202 data.put(attribute.getName(), attribute.getValue()); 203 } 204 list.add(data); 205 } 206 } 207 allData.put("data", list); 208 } 209 for (String object : errorMsgList) { 210 msg += object; 211 } 212 if (!"".equals(msg)) { 213 allData.put("code", faultcode); 214 allData.put("msg", msg); 215 } 216 return allData; 217 } catch (Exception e) { 218 allData.put("code", "800"); 219 allData.put("msg", "报文解析错误"); 220 return allData; 221 } 222 } 223 224 /** 225 * 解析返回一条数据 226 * 227 * @param xmlStr 228 * @return 229 */ 230 public static Map parseOne(String xmlStr) { 231 Map map = parsePageData(xmlStr); 232 if (ResponseCode.SUCCESS.equals(map.get("code"))) { 233 List data = (List) map.get("data"); 234 Map oneMap = (Map) data.get(0); 235 map.put("data", oneMap); 236 } 237 map.remove("page"); 238 return map; 239 } 240 241 242 public static Map parseDataV2(String xml) { 243 // 解析xml文档 244 Map allData = new LinkedHashMap(); 245 try { 246 //根据xml文本获取Document对象 247 Document doc = DocumentHelper.parseText(xml); 248 //获取根节点元素对象 249 Element root = doc.getRootElement(); 250 //获取所有的节点 251 List<Element> rows = root.elements(); 252 //获取xml的身体数据 253 List<Element> bodyData = rows.get(1).elements(); 254 //结构体 255 List<Element> resultsetList = new ArrayList<Element>(); 256 List<Element> resultList = new ArrayList<Element>(); 257 Map<String, String> errorMap = new LinkedHashMap<>(); 258 for (Element bodyChild : bodyData) { 259 List<Element> business = bodyChild.elements(); 260 for (Element e : business) { 261 //获取其属性 262 if ("result".equals(e.getQName().getName())) { 263 resultList.add(e); 264 } else if ("retrieve".equals(e.attributeValue("name"))) { 265 resultsetList.add(e); 266 } else { 267 // 出错信息 268 if ("faultcode".equals(e.getName())) { 269 String faultcode = String.valueOf(e.getData()); 270 allData.put("faultCode", faultcode); 271 } 272 if ("faultstring".equals(e.getName())) { 273 String faultstring = String.valueOf(e.getData()); 274 allData.put("faultString", faultstring); 275 } 276 if ("channelNo".equals(e.getName())) { 277 String channelNo = String.valueOf(e.getData()); 278 allData.put("channelNo", channelNo); 279 } 280 if ("signNo".equals(e.getName())) { 281 String signNo = String.valueOf(e.getData()); 282 allData.put("signNo", signNo); 283 } 284 if ("aac002".equals(e.getName())) { 285 String aac002 = String.valueOf(e.getData()); 286 allData.put("aac002", aac002); 287 } 288 if ("aaz999".equals(e.getName())) { 289 String aaz999 = String.valueOf(e.getData()); 290 allData.put("aaz999", aaz999); 291 } 292 if ("aaz333".equals(e.getName())) { 293 String aaz333 = String.valueOf(e.getData()); 294 allData.put("aaz333", aaz333); 295 } 296 if ("aae036".equals(e.getName())) { 297 String aae036 = String.valueOf(e.getData()); 298 allData.put("aae036", aae036); 299 } 300 if ("pageSize".equals(e.getName())) { 301 String pageSize = String.valueOf(e.getData()); 302 allData.put("pageSize", pageSize); 303 } 304 if ("cPage".equals(e.getName())) { 305 String cPage = String.valueOf(e.getData()); 306 allData.put("cPage", cPage); 307 } 308 if ("fileFlag".equals(e.getName())) { 309 String fileFlag = String.valueOf(e.getData()); 310 allData.put("fileFlag", fileFlag); 311 } 312 } 313 314 } 315 } 316 317 318 // retrieve 319 List list = Lists.newArrayList(); 320 for (Element resultset : resultsetList) { 321 // 解析每个字段名 322 for (Iterator<?> retrieveRow = resultset.elementIterator(); retrieveRow 323 .hasNext(); ) // 记录数--行数 324 { 325 Element roweValueElement = (Element) retrieveRow.next(); 326 // 结构体 327 // retrieve 328 Map data = new LinkedHashMap(); 329 List<Attribute> listAttr = roweValueElement.attributes(); 330 for (Attribute attribute : listAttr) { 331 data.put(attribute.getName(), attribute.getValue()); 332 } 333 list.add(data); 334 } 335 } 336 337 // allData.put("faultCode", faultcode); 338 // for (Map.Entry<String, String> entry : errorMap.entrySet()) { 339 // allData.put(entry.getKey(), entry.getValue()); 340 // } 341 342 if (list.size() != 0) { 343 Map outData = (Map) list.get(0); 344 allData.put("outData", outData); 345 346 list.remove(0); 347 allData.put("files", list); 348 } 349 350 return allData; 351 } catch (Exception e) { 352 allData.put("faultCode", "800"); 353 allData.put("faultString", "报文解析错误"); 354 return allData; 355 } 356 } 357 }
测试结果:
1 <?xml version="1.0" encoding="GBK"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><soap:Header><out:system xmlns:out="http://www.ylzinfo.com/"></out:system></soap:Header><soap:Body><out:business xmlns:out="http://www.ylzinfo.com/"><result showtype="1"/><resultset name="retrieve"><row resultCode ="0" resultMessage ="请求成功" resultBody ="null" /><row cloudPath ="www.baidu.com" fileType ="txt" fileTradeNo ="12345678" fileDesc ="测试" /></resultset></out:business><soap:Fault><faultcode>0</faultcode><faultstring>请求成功</faultstring><channelNo>1400000001</channelNo><signNo>1111111111111111111</signNo><aac002>373738383927711231</aac002><aaz999>1234567890</aaz999><aaz333>12312312312</aaz333><aae036>1578988937192</aae036><pageSize></pageSize><cPage></cPage><fileFlag>1</fileFlag></soap:Fault></soap:Body></soap:Envelope> 2 {"faultCode":"0","faultString":"请求成功","channelNo":"1400000001","signNo":"1111111111111111111","aac002":"373738383927711231","aaz999":"1234567890","aaz333":"12312312312","aae036":"1578988937192","pageSize":"","cPage":"","fileFlag":"1","outData":{"resultCode":"0","resultMessage":"请求成功","resultBody":"null"},"files":[{"cloudPath":"www.baidu.com","fileType":"txt","fileTradeNo":"12345678","fileDesc":"测试"}]}