下面是我封装的工具类和配置文件:
目录
- 1.中通物流api工具类
- 2.中通物流properties配置文件
- 3.中通物流测试类
1.中通物流api工具类
package com.htcloud.xgsixteen.zto.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.stream.Collectors;
public class ZTOUtil {
final static Logger logger = LoggerFactory.getLogger(ZTOUtil.class);
public static String company = null;
public static String companyProv = null;
public static String companyCity = null;
public static String companyCounty = null;
public static String companyAddress = null;
/**
* 初始化配置文件信息
*/
public static ResourceBundle resourceBundle = null;
static {
resourceBundle = ResourceBundle.getBundle("ztotest");//测试接口
// resourceBundle = ResourceBundle.getBundle("ztodev");//正式接口
try {
//中文乱码处理
company = new String(resourceBundle.getString("company").getBytes("ISO-8859-1"),"GBK");
companyProv = new String(resourceBundle.getString("companyProv").getBytes("ISO-8859-1"),"GBK");
companyCity = new String(resourceBundle.getString("companyCity").getBytes("ISO-8859-1"),"GBK");
companyCounty = new String(resourceBundle.getString("companyCounty").getBytes("ISO-8859-1"),"GBK");
companyAddress = new String(resourceBundle.getString("companyAddress").getBytes("ISO-8859-1"),"GBK");
}catch (Exception e){
logger.error(e.getMessage());
}
}
public static String companyId = resourceBundle.getString("companyId");
public static String key = resourceBundle.getString("key");
public static String createOrderUrl = resourceBundle.getString("createOrderUrl");
public static String cancelOrderUrl = resourceBundle.getString("cancelOrderUrl");
public static String getOrderDetailUrl = resourceBundle.getString("getOrderDetailUrl");
public static String getTraceInterfaceUrl = resourceBundle.getString("getTraceInterfaceUrl");
public static String typeUPDATE = resourceBundle.getString("type_UPDATE");
public static String type_SEARCHBYCODE = resourceBundle.getString("type_SEARCHBYCODE");
public static String typeNEW_TRACES = resourceBundle.getString("type_NEW_TRACES");
public static String companyPhone = resourceBundle.getString("companyPhone");
/**
* 中通物流-创建订单
* @param map :请参考中通接口入参,有些是非必传的都不传就行了
* @return
* @throws Exception
*/
public static String createOrder(Map<String,Object> map)throws Exception{
Map<String, String> parameters = new HashMap<>();
parameters.put("orderGroup",
"{\"partnerCode\":\""+map.get("orderId").toString()+"\"," +
"\"type\":1," +
"\"tradeId\":\""+map.get("orderNo")+"\"," +
"\"sender\":" +
"{" +
"\"id\":\""+map.get("sId")+"\"," +
"\"name\":\""+map.get("sName")+"\"," +
"\"company\":\""+map.get("sCompany")+"\"," +
"\"mobile\":\""+map.get("sMobile")+"\"," +
"\"prov\":\""+map.get("sProv")+"\"," +
"\"city\":\""+map.get("sCity")+"\"," +
"\"county\":\""+map.get("sCounty")+"\"," +
"\"address\":\""+map.get("sAddress")+"\"," +
"}," +
"\"receiver\":" +
"{" +
"\"name\":\""+map.get("rName")+"\"," +
"\"mobile\":\""+map.get("rMobile")+"\"," +
"\"prov\":\""+map.get("rProv")+"\"," +
"\"city\":\""+map.get("rCity")+"\"," +
"\"county\":\""+map.get("rCounty")+"\"," +
"\"address\":\""+map.get("rAddress")+"\"" +
"}}");
String result = ZTOUtil.sendUrl(parameters,createOrderUrl,key);
return result;
}
/**
* 中通物流-预约寄件-订单取消
* @param orderCodes :中通的订单号
* @return
* @throws Exception
*/
public static String cancelOrder(String orderCodes)throws Exception{
Map<String, String> parameters = new HashMap<>();
parameters.put("data", "[{\"orderCode\":\""+orderCodes+"\",\"fieldName\":\"status\",\"fieldValue\":\"cancel\",\"reason\":\"客户取消订单\"}]");
parameters.put("msg_type", typeUPDATE);
parameters.put("company_id", companyId);
String result = ZTOUtil.sendUrl(parameters,cancelOrderUrl,key);
return result;
}
/**
* 中通物流-预约寄件-订单明细查询(用来获取运单号,以便查询物流轨迹)
* @param map
* orderCode :中通的订单号
* sendId : 配置文件内的companyId
* @return
* @throws Exception
*/
public static String getOrderDetail(Map<String,Object> map)throws Exception{
Map<String, String> parameters = new HashMap<>();
parameters.put("data", "{\"orderCode\":[\""+map.get("orderCode")+"\"],\"sendId\":\""+map.get("sendId")+"\"}");
parameters.put("msg_type", type_SEARCHBYCODE);
parameters.put("company_id", companyId);
String result = ZTOUtil.sendUrl(parameters,getOrderDetailUrl,key);
return result;
}
/**
* 中通物流-获取订单轨迹
* @param code :中通的运单号
* @return
* @throws Exception
*/
public static String getTraceInterface(String code)throws Exception{
Map<String, String> parameters = new HashMap<>();
parameters.put("data", "[\""+code+"\"]");
parameters.put("msg_type", typeNEW_TRACES);
parameters.put("company_id", companyId);
String result = ZTOUtil.sendUrl(parameters,getTraceInterfaceUrl,key);
return result;
}
/**
* 发送请求
* @param parameters
* @param Url
* @param appKey
* @return
* @throws Exception
*/
public static String sendUrl(Map<String, String> parameters,String Url,String appKey)throws Exception{
String strToDigest = paramsToQueryString(parameters) + key;
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(strToDigest.getBytes(Charset.forName("UTF-8")));
String dataDigest = Base64.getEncoder().encodeToString(md.digest());
// System.out.println("============dataDigest:"+dataDigest);
URL url = new URL(Url);//https://japi.zto.com/traceInterfaceNewTraces http://58.40.16.122:8080/OpenOrderCreate
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
con.setDoOutput(true);
con.setConnectTimeout(5000);
con.setReadTimeout(5000);
con.setRequestProperty("x-datadigest", dataDigest);
con.setRequestProperty("x-companyid", companyId);
DataOutputStream out = new DataOutputStream(con.getOutputStream());
out.write(paramsToQueryStringUrlencoded(parameters).getBytes(Charset.forName("UTF-8")));
out.flush();
out.close();
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer content = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
in.close();
return content.toString();
}
/**
* 获取签名
*/
public static String getDatadigest(Map<String, String> parameters)throws Exception{
String strToDigest = paramsToQueryString(parameters) + key;
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(strToDigest.getBytes(Charset.forName("UTF-8")));
String dataDigest = Base64.getEncoder().encodeToString(md.digest());
return dataDigest;
}
public static String paramsToQueryString(Map<String, String> params) {
return params.entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.joining("&"));
}
public static String paramsToQueryStringUrlencoded(Map<String, String> params) {
return params.entrySet().stream().map(e -> {
try {
return e.getKey() + "=" + URLEncoder.encode(e.getValue(), "UTF-8");
} catch (UnsupportedEncodingException e1) {
return e.getValue();
}
}).collect(Collectors.joining("&"));
}
}
2.中通物流properties配置文件
*放在resources目录下
共俩个文件:
测试(ztotest.properties):存放请求中通测试接口凭据
正式(ztodev.properties):存放请求中通正式接口凭据
1、ztotest.properties
#合作商编码
companyId = kfpttestCode
#合作商key
key = kfpttestkey==
#中通物流-创建订单api
createOrderUrl = http://58.40.16.122:8080/OpenOrderCreate
#中通物流-取消订单api
cancelOrderUrl = http://58.40.16.122:8080/commonOrderUpdate
#中通物流-获取订单详情api
getOrderDetailUrl = http://58.40.16.122:8080/commonOrderSearchbycode
#中通物流-获取订单轨迹api
getTraceInterfaceUrl = http://58.40.16.122:8080/traceInterfaceNewTraces
#中通物流-取消订单api参数type
type_UPDATE = UPDATE
#中通物流-订单查询api参数type
type_SEARCHBYCODE = SEARCHBYCODE
#中通物流-获取订单轨迹api参数type
type_NEW_TRACES = NEW_TRACES
company = 腾讯科技(深圳)有限公司
companyPhone = 0755-8601****
companyProv = 广东省
companyCity = 深圳市
companyCounty = 南山
companyAddress = 深圳市南山区高新区科技中一路腾讯大厦35层
2、ztodev.properties
#合作商编码
companyId = 你申请中通账号的companyId
#合作商key
key = 你申请中通账号的key
#中通物流-创建订单api
createOrderUrl = http://japi.zto.cn/OpenOrderCreate
#中通物流-取消订单api
cancelOrderUrl = http://japi.zto.cn/commonOrderUpdate
#中通物流-获取订单详情api
getOrderDetailUrl = http://japi.zto.cn/commonOrderSearchbycode
#中通物流-获取订单轨迹api
getTraceInterfaceUrl = http://japi.zto.cn/traceInterfaceNewTraces
#中通物流-取消订单api参数type
type_UPDATE = UPDATE
#中通物流-订单查询api参数type
type_SEARCHBYCODE = SEARCHBYCODE
#中通物流-获取订单轨迹api参数type
type_NEW_TRACES = NEW_TRACES
company = 腾讯科技(深圳)有限公司
companyPhone = 0755-8601****
companyProv = 广东省
companyCity = 深圳市
companyCounty = 南山
companyAddress = 深圳市南山区高新区科技中一路腾讯大厦35层
3.中通物流测试类
public class ZTtest {
/**
* 中通物流-创建订单
* @return
* @throws Exception
*/
@Test
public void createOrder()throws Exception{
Map<String, String> parameters = new HashMap<>();
// parameters.put("orderGroup", "{\"partnerCode\":\"130520142013234\",\"type\":1,\"tradeId\":\"2701843\",\"mailNo\":\"1000000000016\",\"sender\":{\"id\":\"130520142010\",\"name\":\"李琳\",\"company\":\"新南电子商务有限公司\",\"mobile\":\"13912345678\",\"phone\":\"021-87654321\",\"prov\":\"上海市\",\"city\":\"上海市\",\"county\":\"青浦区\",\"address\":\"华新镇华志路123号\",\"zipcode\":\"610012\"},\"receiver\":{\"id\":\"130520142097\",\"name\":\"杨逸嘉\",\"company\":\"逸嘉实业有限公司\",\"mobile\":\"13687654321\",\"phone\":\"010-22226789\",\"prov\":\"四川省\",\"city\":\"成都市\",\"county\":\"武侯区\",\"address\":\"育德路497号\",\"zipcode\":\"610012\"},\"items\":[{\"id\":\"1234567\",\"name\":\"迷你风扇\",\"category\":\"电子产品\",\"material\":\"金属\",\"size\":\"12,11,23\",\"weight\":\"1\",\"unitprice\":\"79\",\"quantity\":\"1\",\"remark\":\"黑色大号\"},{\"name\":\"USB3.0集线器\",\"quantity\":\"1\",\"remark\":\"\"}],\"starttime\":\"2013-05-20 12:00:00\",\"endtime\":\"2013-05-20 15:00:00\",\"weight\":753,\"size\":\"12,23,11\",\"quantity\":2,\"price\":12650,\"freight\":1000,\"premium\":50,\"packCharges\":100,\"otherCharges\":0,\"orderSum\":0,\"collectMoneytype\":\"CNY\",\"collectSum\":1200,\"remark\":\"请勿摔货\"}");
parameters.put("orderGroup","{\n" +
" \"partnerCode\": \"123213\",\n" +
" \"type\": 1,\n" +
" \"tradeId\": \"12321321\",\n" +
" \"mailNo\": \"123213\",\n" +
" \"sender\": {\n" +
" \"id\": \"130520142010\",\n" +
" \"name\": \"李琳\",\n" +
" \"company\": \"新南电子商务有限公司\",\n" +
" \"mobile\": \"13912345678\",\n" +
" \"phone\": \"021-87654321\",\n" +
" \"prov\": \"上海市\",\n" +
" \"city\": \"上海市\",\n" +
" \"county\": \"青浦区\",\n" +
" \"address\": \"华新镇华志路123号\",\n" +
" \"zipcode\": \"610012\"\n" +
" },\n" +
" \"receiver\": {\n" +
" \"id\": \"130520142097\",\n" +
" \"name\": \"杨逸嘉\",\n" +
" \"company\": \"逸嘉实业有限公司\",\n" +
" \"mobile\": \"13687654321\",\n" +
" \"phone\": \"010-22226789\",\n" +
" \"prov\": \"四川省\",\n" +
" \"city\": \"成都市\",\n" +
" \"county\": \"武侯区\",\n" +
" \"address\": \"育德路497号\",\n" +
" \"zipcode\": \"610012\"\n" +
" }}");
String url = "http://58.40.16.122:8080/OpenOrderCreate";
String result = ZTUtil.sendUrl(parameters,url,ZTUtil.key);
System.out.println(result);
}
/**
* 中通物流-预约寄件-订单取消
* @return
* @throws Exception
*/
@Test
public void cancelOrder()throws Exception{
Map<String, String> parameters = new HashMap<>();
parameters.put("data", "[{\"orderCode\":\"100000000001\",\"fieldName\":\"status\",\"fieldValue\":\"cancel\",\"reason\":\"客户取消订单\"},{\"orderCode\":\"100000000002\",\"fieldName\":\"status\",\"fieldValue\":\"cancel\",\"reason\":\"客户取消订单\"}]");
parameters.put("msg_type", "UPDATE");
parameters.put("company_id", ZTUtil.companyId);
String url = "http://58.40.16.122:8080/commonOrderUpdate";
String result = ZTUtil.sendUrl(parameters,url,ZTUtil.key);
System.out.println(result);
}
/**
* 中通物流-预约寄件-订单明细查询
* @return
* @throws Exception
*/
@Test
public void getOrderDetail()throws Exception{
Map<String, String> parameters = new HashMap<>();
parameters.put("data", "{\"orderCode\":[\"1234567\",\"123123123\",\"12312312312\"],\"sendId\":\"123456678\",\"partnerId\":\"123456678\"}");
parameters.put("msg_type", "SEARCHBYCODE");
parameters.put("company_id", ZTUtil.companyId);
String url = "http://58.40.16.122:8080/commonOrderSearchbycode";
String result = ZTUtil.sendUrl(parameters,url,ZTUtil.key);
System.out.println(result);
}
/**
* 中通物流-获取订单轨迹
* @return
* @throws Exception
*/
@Test
public void getTraceInterface()throws Exception{
Map<String, String> parameters = new HashMap<>();
parameters.put("data", "[\"680000000000\"]");
parameters.put("msg_type", "NEW_TRACES");
parameters.put("company_id", ZTUtil.companyId);
String url = "http://58.40.16.122:8080/traceInterfaceNewTraces";
String result = ZTUtil.sendUrl(parameters,url,ZTUtil.key);
System.out.println(result);
}
}
感觉观看,希望能帮到你(#^.^#)