JAVA接入順豐快遞

豐橋地址html

下載到項目中後放到lib文件夾下,引入:express

<dependency>
            <groupId>sf.sdk</groupId>
            <artifactId>sf</artifactId>
            <version>1.0</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/resources/lib/SF-CSIM-EXPRESS-SDK-V1.6.jar</systemPath>
 </dependency>

須要從豐橋獲取的參數:app

@Value("${sf.clientCode}")
    private String clientCode;    //商戶號

    @Value("${sf.checkword}")
    private String checkword;    //驗證碼

    @Value("${sf.custid}")
    private String custid;        //月付id 我這裏使用的是寄付月結

順豐使用xml作數據交互,這裏貼一些簡單的轉換工具類
具體的字段含義查看順豐的文檔
快速下單轉換xml方法:工具

public static String getOrderServiceRequestXml(Map<String, String> params) {


        StringBuilder strBuilder = new StringBuilder();
        strBuilder.append("<Request service='OrderService' lang='zh-CN'>");
        strBuilder.append("<Head>" + params.get("clientCode") + "</Head>");
        strBuilder.append("<Body>");
        strBuilder.append("<Order").append(" ");
        strBuilder.append("orderid='" + params.get("orderid") + "'").append(" ");
        //返回順豐運單號
        strBuilder.append("express_type='1'").append(" ");
        //寄件方信息
        strBuilder.append("j_province='" + params.get("j_province") + "'").append(" ");
        strBuilder.append("j_city='" + params.get("j_city") + "'").append(" ");
        strBuilder.append("j_company='" + params.get("j_company") + "'").append(" ");
        strBuilder.append("j_contact='" + params.get("j_contact") + "'").append(" ");
        strBuilder.append("j_tel='" + params.get("j_tel") + "'").append(" ");
        strBuilder.append("j_address='" + params.get("j_address") + "'").append(" ");
        //收件方信息
        strBuilder.append("d_province='" + params.get("d_province") + "'").append(" ");
        strBuilder.append("d_city='" + params.get("d_city") + "'").append(" ");
        strBuilder.append("d_county='" + params.get("d_county") + "'").append(" ");
        strBuilder.append("d_company='" + params.get("d_company") + "'").append(" ");
        strBuilder.append("d_tel='" + params.get("d_tel") + "'").append(" ");
        strBuilder.append("d_contact='" + params.get("d_contact") + "'").append(" ");
        strBuilder.append("d_address='" + params.get("d_address") + "'").append(" ");
        //貨物信息
        strBuilder.append("parcel_quantity='1'").append(" ");
        strBuilder.append("pay_method='3'").append(" ");
        strBuilder.append("custid ='" + params.get("custid") + "'").append(" ");
        strBuilder.append("customs_batchs=''").append(" ");
        strBuilder.append("cargo='服裝'").append(">");
        strBuilder.append("<AddedService name='COD' value='1.01' value1='7551234567' />");
        strBuilder.append("</Order>");
        strBuilder.append("</Body>");
        strBuilder.append("</Request>");

        return strBuilder.toString();
    }

訂單查詢接口是在下單後沒有返回運單號時主動查詢運單號使用的,這裏我一開始理解爲查詢物流信息的接口了 ̄□ ̄||ui

/**
     * 獲取順豐訂單結果查詢接口xml
     *
     * @param params
     * @return
     */
    public static String getOrderSearchServiceRequestXml(Map<String, String> params) {
        String orderNo = params.get("orderNo");
        StringBuilder strBuilder = new StringBuilder();
        strBuilder.append("<Request service='OrderSearchService' lang='zh-CN'>");
        strBuilder.append("<Head>" + params.get("clientCode") + "</Head>");
        strBuilder.append("<Body>");
        strBuilder.append("<OrderSearch").append(" ");
        strBuilder.append("orderid='" + orderNo + "'").append(" /> ");
        strBuilder.append("</Body>");
        strBuilder.append("</Request>");
        return strBuilder.toString();
    }

除了下單接口參數略多一下,其餘接口大體相同
查詢物流信息接口code

/**
     * 獲取順豐路由查詢接口xml
     *
     * @param params
     * @return
     */
    public static String getRouteServiceRequestXml(Map<String, String> params) {
        StringBuilder strBuilder = new StringBuilder();
        strBuilder.append("<Request service='RouteService' lang='zh-CN'>");
        strBuilder.append("<Head>" + params.get("clientCode") + "</Head>");
        strBuilder.append("<Body>");
        strBuilder.append("<RouteRequest").append(" ");
        strBuilder.append("tracking_type='1'").append(" ");
        strBuilder.append("method_type='1'").append(" ");
        strBuilder.append("tracking_number='" + params.get("mailno") + "'").append(" >");
        strBuilder.append("</RouteRequest>");
        strBuilder.append("</Body>");
        strBuilder.append("</Request>");
        return strBuilder.toString();
    }

取消訂單接口xml

/**
     * 取消訂單
     *
     * @param params
     * @return
     */
    public static String getConfirmRequestXml(Map<String, String> params) {
        StringBuilder strBuilder = new StringBuilder();
        strBuilder.append("<Request service='OrderConfirmService' lang='zh-CN'>");
        strBuilder.append("<Head>").append(params.get("clientCode")).append("</Head>");
        strBuilder.append("<Body>");
        strBuilder.append("<OrderConfirm").append(" ");
        strBuilder.append("orderid='").append("orderNo").append("' ");
        strBuilder.append("dealtype='2'>").append(" ");
        strBuilder.append("</OrderConfirm>");
        strBuilder.append("</Body>");
        strBuilder.append("</Request>");
        return strBuilder.toString();
    }

傳入參數獲得拼接好的xml數據後開始請求順豐:htm

public String callSf(String xmlStr) {
        CallExpressServiceTools client = CallExpressServiceTools.getInstance();
        log.info("開始調用順豐接口下單,請求報文:{}", xmlStr);
        String respXml = client.callSfExpressServiceByCSIM(null, xmlStr, clientCode, checkword);
        log.info("請求完成,返回報文:{}", respXml);
        return respXml;
    }

獲得報文後在進行解析便可
整體來講使用順豐新的sdk接入仍是很是方便的。
記錄下代碼,方面下次使用。接口

相關文章
相關標籤/搜索