對接前置篇:
【SF順豐】順豐開放平臺API對接(注冊、API測試篇)_順豐api接口對接指南-CSDN博客
1.實現效果展示
?2.SF順豐開放平臺,JDK資源下載。
? ? ?下載地址:順豐開放平臺
3.將下載的JDK放入項目中。
4.將JDK資源引入pom.xml文件中。
<!-- 順豐豐橋 SDK -->
<dependency><groupId>com.sf</groupId><artifactId>SF-CSIM-EXPRESS-SDK</artifactId><version>2.1.7</version><scope>system</scope><systemPath>${project.basedir}/lib/SF-CSIM-EXPRESS-SDK-V2.1.7.jar</systemPath>
</dependency>
?注:項目啟動pom.xml文件中配置打包將外部SDK引入項目包中,否則發布后依然是失效。
<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><!-- 在打包時將引用的外部jar引入到當前項目包中 --><fork>true</fork><includeSystemScope>true</includeSystemScope></configuration></plugin></plugins>
</build>
5.測試DEMO
接下來是一個詳細的示例,演示如何使用順豐豐橋API進行路由信息查詢。
? ? 順豐豐橋Api參考文檔:順豐開放平臺
public class SFTestDemo {private static final String CLIENT_CODE = "替換自己應用的顧客編碼"; //顧客編碼//沙箱環境的地址private static final String CALL_URL_BOX = "https://sfapi-sbox.sf-express.com/std/service";//請求接口private static final String CHECK_WORD_BOX = "替換自己應用的沙箱校驗碼";//沙箱校驗碼//生產環境的地址private static final String CALL_URL_PROD = "https://sfapi.sf-express.com/std/service";//請求接口private static final String CHECK_WORD = "替換自己的應用的生產校驗碼";//生產校驗碼/*** 查詢順豐路由信息* <p>* 順豐快遞單號* 收件人手機號后四位** @return* @throws UnsupportedEncodingException*/public static void main(String[] args) throws UnsupportedEncodingException {IServiceCodeStandard standardService = ExpressServiceCodeEnum.EXP_RECE_SEARCH_ROUTES;//路由查詢String timeStamp = String.valueOf(System.currentTimeMillis());//時間戳CallExpressServiceTools tools = CallExpressServiceTools.getInstance();//數字簽名,API文檔有說明// set common headerMap<String, String> params = new HashMap<String, String>();//封裝查詢msgData數據Map<String, Object> map = new HashMap<>();List<String> list = new ArrayList();list.add("替換自己順豐單號");map.put("language", "0");map.put("trackingType", "1");map.put("methodType", "1");map.put("trackingNumber", list);map.put("checkPhoneNo", "替換快遞單手機號后四位");String msgData = JSONObject.toJSONString(map);// params.put("Content-type", "application/x-www-form-urlencoded");params.put("partnerID", CLIENT_CODE); // 顧客編碼params.put("requestID", UUID.randomUUID().toString().replace("-", ""));params.put("serviceCode", standardService.getCode());// 接口服務碼params.put("timestamp", timeStamp);params.put("msgData", msgData);params.put("msgDigest", tools.getMsgDigest(msgData, timeStamp, CHECK_WORD));String result = HttpClientUtil.post(CALL_URL_PROD, params);System.out.println("===調用地址 ===" + CALL_URL_PROD);System.out.println("===顧客編碼 ===" + CLIENT_CODE);System.out.println("===返回結果:" + result);}
}