第二篇:豐橋SDK之路由查詢【順豐查詢訂單的物流資訊】
阿新 • • 發佈:2018-12-17
上一篇的文章連結:
豐橋sdk路由查詢的關鍵程式碼:
package com.test.demo.express.config; import com.test.demo.express.service.SfExpressService; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @Author: Mqs * @Date: 2018/10/22 18:16 * @Description: */ @Configuration @EnableConfigurationProperties(SfExpressProperties.class) public class SfExpressAutoConfiguration { private final SfExpressProperties expressProperties; public SfExpressAutoConfiguration(SfExpressProperties expressProperties) { this.expressProperties = expressProperties; } @Bean public SfExpressService expressService(){ SfExpressService expressService = new SfExpressService(); expressService.setExpressProperties(expressProperties); return expressService; } }
package com.test.demo.express.service; import com.test.demo.express.config.SfExpressProperties; import com.test.demo.express.sf.dto.SfExpressResponseDTO; import com.test.demo.express.sf.param.SfTracesParam; import com.test.demo.express.sf.util.XmlUtil; import com.test.demo.storage.util.HttpClientUtil; import org.apache.commons.codec.binary.Base64; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.security.MessageDigest; /** * @Author: Mqs * @Date: 2018/10/22 18:20 * @Description: */ public class SfExpressService { public static final Log logger = LogFactory.getLog(SfExpressService.class); private static final String RESULT_OK = "OK"; private SfExpressProperties expressProperties; public SfExpressProperties getExpressProperties() { return expressProperties; } public void setExpressProperties(SfExpressProperties expressProperties) { this.expressProperties = expressProperties; } /** * 查詢物流資訊介面 * @param params * @return */ public SfExpressResponseDTO sfExpressMethod(SfTracesParam params){ String wayBillNo = params.getWayBillNo(); String orderSn = params.getOrderSn(); // 獲取訂單物流路由 String myReqXML = getRouteServiceRequestXml(wayBillNo); String resultStr= callSfExpressServiceByCSIM(expressProperties.getReqURL(), myReqXML, expressProperties.getClientCode(), expressProperties.getCheckWord()); logger.info("返回的報文資訊:" + resultStr); String head = subStringBetween(resultStr,"<Head>","</Head>").trim(); boolean contains = resultStr.contains("<Body/>"); logger.info("返回的報文Head的狀態:" + (head.equals("OK") && !contains ? "返回成功" : "返回失敗")); // 報文顯示成功 if (RESULT_OK.equals(head) && !contains) { // 處理成功的報文,使用xml解析返回的xml報文為物件 SfExpressResponseDTO responseDTO = XmlUtil.convertToObject(resultStr, SfExpressResponseDTO.class); responseDTO.setExpressName(expressProperties.getExpressName()); responseDTO.setOrderSn(orderSn); responseDTO.setWayBillNo(wayBillNo); return responseDTO; }else { // 出錯則打印出錯誤資訊 SfExpressResponseDTO responseDTO = new SfExpressResponseDTO(); responseDTO.setHead(RESULT_OK); responseDTO.setExpressName(expressProperties.getExpressName()); responseDTO.setOrderSn(orderSn); responseDTO.setWayBillNo(wayBillNo); return responseDTO; } } /** * 獲取順豐路由查詢介面xml【請求體的xml檔案】 * @param wayBillNo * @return */ private String getRouteServiceRequestXml(String wayBillNo){ StringBuilder strBuilder = new StringBuilder(); strBuilder.append("<Request service='RouteService' lang='zh-CN'>"); strBuilder.append("<Head>" + expressProperties.getClientCode() + "</Head>"); strBuilder.append("<Body>"); strBuilder.append("<RouteRequest").append(" "); strBuilder.append("tracking_type='1'").append(" "); strBuilder.append("method_type='1'").append(" "); strBuilder.append("tracking_number='" + wayBillNo.trim() + "" + "'").append(" />"); strBuilder.append("</Body>"); strBuilder.append("</Request>"); return strBuilder.toString(); } /** * 取兩個字串之間的 * @param str * @param strStart * @param strEnd * @return */ private String subStringBetween(String str, String strStart, String strEnd) { /* 找出指定的2個字元在 該字串裡面的 位置 */ int strStartIndex = str.indexOf(strStart); int strEndIndex = str.indexOf(strEnd); /* index 為負數 即表示該字串中 沒有該字元 */ if (strStartIndex < 0) { return "字串 :---->" + str + "<---- 中不存在 " + strStart + ", 無法擷取目標字串"; } if (strEndIndex < 0) { return "字串 :---->" + str + "<---- 中不存在 " + strEnd + ", 無法擷取目標字串"; } /* 開始擷取 */ String result = str.substring(strStartIndex, strEndIndex).substring(strStart.length()); return result; } private String callSfExpressServiceByCSIM(String reqURL, String reqXML, String clientCode, String checkword) { String result = null; String verifyCode = md5EncryptAndBase64(reqXML + checkword); result = querySFAPIservice(reqURL, reqXML, verifyCode); return result; } private String querySFAPIservice(String url, String xml, String verifyCode) { HttpClientUtil httpclient = new HttpClientUtil(); if (url == null) { url = expressProperties.getReqURL(); } String result = null; try { result = httpclient.postSFAPI(url, xml, verifyCode); return result; } catch (Exception var6) { logger.warn(" " + var6); return null; } } private String md5EncryptAndBase64(String str) { return encodeBase64(md5Encrypt(str)); } private byte[] md5Encrypt(String encryptStr) { try { MessageDigest md5 = MessageDigest.getInstance("MD5"); md5.update(encryptStr.getBytes("utf8")); return md5.digest(); } catch (Exception var2) { throw new RuntimeException(var2); } } private String encodeBase64(byte[] b) { String str = (new Base64()).encodeAsString(b); return str; } }
xml檔案解析的工具類下一篇文章的連結:
上一篇對於物件及配置檔案的封裝連結: