下载到项目中后放到lib文件夹下,引入:
<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>
需要从丰桥获取的参数:
@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();
}
订单查询接口是在下单后没有返回运单号时主动查询运单号使用的,这里我一开始理解为查询物流信息的接口了 ̄□ ̄||
/**
* 获取顺丰订单结果查询接口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();
}
除了下单接口参数略多一下,其他接口大致相同
查询物流信息接口
/**
* 获取顺丰路由查询接口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();
}
取消订单接口
/**
* 取消订单
*
* @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数据后开始请求顺丰:
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接入还是非常方便的。
记录下代码,方面下次使用。