<?xml version="1.0" encoding="UTF-8"?>
<Request service="QuerySFWaybillService" lang="zh-CN">
<Head>xiaomiapp</Head>
<Body>
<Waybill type="2" orderId="" waybillNo="SF1310510730738" phone="7289"/>
</Body>
</Request>
请求XML
package com.sf.vsolution.hb.controller;
import com.alibaba.fastjson.JSONObject;
import com.sf.vsolution.hb.util.ExpressApiUtil;
import com.sf.vsolution.hb.util.QiaoConfig;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class QiaoApi {
private static QiaoConfig qiaoConfig;
@Autowired
private void setQiaoConfig(QiaoConfig qiaoConfig) {
QiaoApi.qiaoConfig = qiaoConfig;
}
/**
* 运费查询接口---------------------------JIUSJHSDLJKANFKJLSADH
*
* @param waybillNo 运单号 + 手机号后四位
* @return
*/
public static JSONObject getWaybillFreightByQiaoSF(String waybillNo, String phone) {
log.info("进入运费查询接口:运单号{}", waybillNo);
ExpressApiUtil expressApiUtil = new ExpressApiUtil();
Document doc = DocumentHelper.createDocument();
Element request = doc.addElement("Request");
request.addAttribute("service", "QuerySFWaybillService");
request.addAttribute("lang", "zh-CN");
Element head = request.addElement("Head");
head.setText("xiaomiapp");
Element body = request.addElement("Body");
//order主体以订单号查询
Element order = body.addElement("Waybill");
order.addAttribute("type", "2");
order.addAttribute("orderId", "");
order.addAttribute("waybillNo", waybillNo);
order.addAttribute("phone", phone);
JSONObject jObject = new JSONObject();
Document resultDoc = null;
try {
//小米测试环境
log.info("运费查询请求参数:" + doc.asXML());
String sourcexml = expressApiUtil.expressApi(doc.asXML(), "tSVUYZybpX2DZ5HkiMLN", "xiaomiapp", "http://bsprpcs.intsit.sfdc.com.cn/bsp-rpcs/sfexpressService");
log.info("运费查询响应参数:" + sourcexml);
resultDoc = DocumentHelper.parseText(sourcexml);
} catch (DocumentException e) {
log.error("BSP返回XML解析失败!" + e.getMessage(), e);
}
Element resultRoot = resultDoc.getRootElement();
Element resultHead = resultRoot.element("Head");
String strHead = resultHead.getTextTrim();
try {
if ("OK".equalsIgnoreCase(strHead)) {
Element resultBody = resultRoot.element("Body");
Element routeResponse = resultBody.element("Waybill");
Element fee = routeResponse.element("Fee");
jObject.put("result", "0");
jObject.put("FreightName", fee.attribute("name").getText());
jObject.put("Freight", fee.attribute("value").getText());
log.info(jObject + "");
} else {
Element resultError = resultRoot.element("Error");
jObject.put("result", "1");
jObject.put("error", resultError.getTextTrim());
}
} catch (Exception e) {
log.error("运费查询异常:" + e.getMessage(), e);
jObject.put("result", 1);
jObject.put("error", "BSP取数错误" + waybillNo);
}
return jObject;
}
/**
* 地址:https://bsp-oisp.sf-express.com/bsp-oisp/sfexpressService
* 顾客编码:YJJ_nYdWQ
* 校验码:AxScci5T7LZPnRJfbqt6Fffjj9QPbIOU
* 运单号:SF1314131454847
* 手机号:8195
*
* @param args
*/
public static void main(String[] args) {
JSONObject jsonObject = getWaybillFreightByQiaoSF("SF1310510730738", "7289");
System.out.println("jsonObject = " + jsonObject);
}
}
响应XML
<?xml version='1.0' encoding='UTF-8'?>
<Response service="QuerySFWaybillService" lang="zh_CN">
<Head>OK</Head>
<Body>
<Waybill waybillNo="SF1310510730738" addresseePhone="17782637289" consigneeEmpCode="000212" addresseeContName="顾客退货服务部" addresseeMobile="17782637289" prodName="顺丰标快" addresseeAddr="广东省广州市荔湾区DE##EwA9TkQc0Hli%2F4cCnucFsBAYaUlH1bY5uX2LJto1VSMh18PajsVselD8r%2Bisyu9ehuxmMR94j%2BbYbDF1SWl%2FvwwH%2BkM%3D" consignorContName="唯品会" consignorMobile="15012794312" consignorAddr="DE##EwA9TvFD4FLhu25Lin%2FhO%2FEUKZ5ApIZ5ny2T7y1OLWQbcx8%2BeeJ%2FtaGD6ajwQujzibwcRnp0hN8MaEb0ACAsXQeuihU%3D" consignorPhone="15012794312">
<Fee name="运费" type="1" value="14.0"/>
</Waybill>
</Body>
</Response>