JavaWeb企業級專案中接入順豐官方API實現物流實時查詢(親測有效)
阿新 • • 發佈:2018-11-16
由於現在順豐與快遞100鬧掰了,所以使用快遞一百已經查不到順豐的物流資訊了,包括快遞鳥等,現在想要在專案中實現順豐快遞的物流查詢只能用順豐官方API來查詢,然而這個官方的API並沒有快遞一百那些介面那麼容易,需要很複雜的一套流程,並且順豐用的返回形式都是XML檔案,就需要設計到XML檔案的修改與寫入,想要接入順豐API需要先下載順豐的Java端的介面文件(裡面包含了介面程式碼和jar包),然後匯入到專案中,包括jar包!
注意:查詢物流的路由資訊需要先在豐橋上接入API並且申請一個月結卡號!接入成功後,通過Api只能查詢月結卡號下的訂單!
其他的訂單會查不到!(雖然很複雜,但是沒辦法,順豐查詢只能這樣啦)
(關於更多介面中的具體的引數請——點選檢視)
首先看看順豐API文件的裡的結構
然後進入第一個資料夾
把這些都匯入到專案中,java-demo中提供了查詢的方法,並且順豐官方提供一個測試賬號,可以在main方法中使用測試賬號查詢測試訂單,如果沒有問題就進入後面的步驟!
(有的人匯入後會發現
CallExpressServiceTools client = CallExpressServiceTools.getInstance()
方法報錯,解決方法——點選檢視
)
還有個要注意的地方就是,我把xml檔案放在了專案中resources資原始檔夾中,所以寫路徑的時候要注意點,具體的書寫方式參考下面的程式碼~
下面就把我寫的介面程式碼貢獻出來~
package com.sixmac.platform.service.impl; import com.sf.csim.express.service.CallExpressServiceTools; import com.sixmac.platform.service.ShunFengService; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; import org.json.JSONObject; import org.json.XML; import org.springframework.stereotype.Service; import java.io.*; import java.util.List; @Service public class ShunFengServiceImpl implements ShunFengService { //resources資源目錄下 tomcat中對應的路徑 String path = this.getClass().getClassLoader().getResource("./txt/shunfeng.xml").getPath(); /** * * @param num 傳過來的運單號 * @return * @throws IOException * @throws DocumentException */ @Override public String findShunFeng(String num) throws IOException, DocumentException { String reqXml = ""; xmlUpdate(num); try { InputStream is = new FileInputStream(path);//路由查詢-通過訂單號 System.out.println(); byte[] bs = new byte[is.available()]; is.read(bs); reqXml = new String(bs); JSONObject xmlJSONObj = XML.toJSONObject(reqXml); } catch (Exception e) { } //豐橋平臺公共測試賬號 //SLKJ2019 //FBIqMkZjzxbsZgo7jTpeq7PD8CVzLT4Q String reqURL = "https://bsp-oisp.sf-express.com/bsp-oisp/sfexpressService"; String clientCode = "";//此處替換為您在豐橋平臺獲取的顧客編碼 String checkword = "";//此處替換為您在豐橋平臺獲取的校驗碼 CallExpressServiceTools client = CallExpressServiceTools.getInstance(); String myReqXML = reqXml.replace("SLKJ2019", clientCode);//這裡不需要改動 System.out.println("請求報文:" + myReqXML); String respXml = client.callSfExpressServiceByCSIM(reqURL, myReqXML, clientCode, checkword); if (respXml != null) { System.out.println("---------------------------------------"); System.out.println("返回報文XML格式: " + respXml); System.out.println("返回報文Json: " + XML.toJSONObject(respXml)); System.out.println("---------------------------------------"); } JSONObject json = XML.toJSONObject(respXml); String s = json.toString(); System.out.println(s); return s; } /** * 接收傳過來的運單號,然後對路由查詢的XML檔案進行修改與寫入 * @param num * @throws DocumentException * @throws IOException */ public void xmlUpdate(String num) throws DocumentException, IOException { System.out.println(path); /* 2.java修改xml */ // 建立SAXReader的物件 SAXReader sr = new SAXReader(); // 關聯xml Document document = sr.read(path); // 獲取根元素 Element root = document.getRootElement(); // 獲取Body標籤(不能直接獲取Body下的RouteRequest標籤) Element b = root.element("Body"); System.out.println(b.getName()); System.out.println(b.getStringValue()); // b.addAttribute() 增加屬性 //獲取Body下的RouteRequest標籤 Element req = b.element("RouteRequest"); //獲取RouteRequest標籤的tracking_number屬性 Attribute number = req.attribute("tracking_number"); //設定tracking_number屬性的值(修改XML檔案中的訂單號,改為需要查詢的訂單號) number.setText(num); System.out.println(number.getValue()); System.out.println("成功"); //----------到這裡已經修改完成了,可以直接列印在控制顯示,但是xml的實體並沒有改變,如果想要改變實體檔案內容,需要通過流寫到xml檔案中--------- // 建立輸出流 Writer osWrite = new OutputStreamWriter( new FileOutputStream(path)); OutputFormat format = OutputFormat.createPrettyPrint(); // 獲取輸出的指定格式 format.setEncoding("UTF-8");// 設定編碼 ,確保解析的xml為UTF-8格式 XMLWriter writer = new XMLWriter(osWrite, format);// XMLWriter // 指定輸出檔案以及格式 writer.write(document); // 把document寫入xmlFile指定的檔案(可以為被解析的檔案或者新建立的檔案) writer.flush(); writer.close(); } }
方法裡的程式碼基本上就這麼多,具體的引數自己對號入座就行!
最後附上一張查詢成功後的圖,返回的也是XML格式,但是可以轉換成json格式~
The end !!!