SpringBoot集成物流快递详细教学
一.快递鸟注册账号
注册成功右上角有企业名称,和apikey,这个对应后续商户id和apikey
二.pom文件引入依赖
<!-- Guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.1-jre</version>
</dependency>
<!-- FastJSON -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>
三.application.yml文件配置快递鸟
这里的商户id和api key就是1.注册账号那里获取的,api-url根据你要接入的服务的接口文档修改
# 快递鸟配置
kdniao:
business-id: 11111 # 您的商户ID
api-key: 1111111 # 您的API key
api-url: https://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx # 快递鸟请求地址
四.免费开通服务
随便点击一个服务进去
根据自己要接入的服务在这里申请试用
五.去看相关的接口文档
比如我要接入的是在途监控服务中的即时查询,
我就找到这里,注意这里有一个接口地址,写入application.yml配置的api-url里面
这里的RequestType后面有用,留个眼
六,代码部分
1.目录结构
ruoyi-admin/src/main/java/com/ruoyi/android/customer/
├── config
│ └── KdniaoConfig.java # 快递鸟配置类
├── controller
│ └── ShoppingCentreController.java # 控制器
├── dto
│ └── KdniaoApiDTO.java # 请求参数封装
├── vo
│ └── KdniaoApiVO.java # 响应结果封装
└── util
└── KdniaoUtil.java # 快递鸟API工具类
2.代码实现流程
配置类KdniaoConfig
@Data
@Component
@ConfigurationProperties(prefix = "kdniao")
public class KdniaoConfig {
private String businessId;
private String apiKey;
private String apiUrl;
}
请求参数类DTO基类
@Data
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
@ApiModel("快递鸟-物流-查询base参数")
public class KdniaoApiBaseDTO {
@ApiModelProperty(value = "用户ID", required = true, example = "xx")
private String eBusinessID;
@ApiModelProperty(value = "API key", required = true, example = "xx")
private String apiKey;
@ApiModelProperty(value = "请求url", required = true, example = "https://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx")
private String reqURL;
}
请求参数类DTO(根据对应文档编写)
@Data
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
@ApiModel("快递鸟-物流-查询参数")
public class KdniaoApiDTO extends KdniaoApiBaseDTO {
@ApiModelProperty(value = "快递公司编码", required = true, example = "SF")
private String shipperCode;
@ApiModelProperty(value = "快递单号", required = true, example = "SF1234567890")
private String logisticCode;
@ApiModelProperty(value = "手机号后四位(顺丰必填)", required = false, example = "1234")
private String customerName;
}
响应结果类VO(根据对应文档编写)
有点长截取部分文档图
@Data
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
@ApiModel("快递鸟-物流-响应参数")
public class KdniaoApiVO {
/**
* {@link KdniaoLogisticsStatusEnum }
* 增值物流状态:
* 0-暂无轨迹信息
* 1-已揽收
* 2-在途中
* 201-到达派件城市, 202-派件中, 211-已放入快递柜或驿站,
* 3-已签收
* 301-正常签收, 302-派件异常后最终签收, 304-代收签收, 311-快递柜或驿站签收,
* 4-问题件
* 401-发货无信息, 402-超时未签收, 403-超时未更新, 404-拒收(退件), 405-派件异常, 406-退货签收, 407-退货未签收, 412-快递柜或驿站超时未取
*/
@ApiModelProperty("增值物流状态")
private Integer StateEx;
@ApiModelProperty("增值物流状态名称")
private String statusExName;
@ApiModelProperty("快递单号")
private String LogisticCode;
@ApiModelProperty("快递公司编码")
private String ShipperCode;
@ApiModelProperty("失败原因")
private String Reason;
@ApiModelProperty("事件轨迹集")
private List<TraceItem> Traces;
/**
* {@link KdniaoLogisticsStatusEnum }
*/
@ApiModelProperty("物流状态:0-暂无轨迹信息,1-已揽收,2-在途中,3-签收,4-问题件")
private Integer State;
@ApiModelProperty("状态名称")
private String statusName;
@ApiModelProperty("用户ID")
private String EBusinessID;
@ApiModelProperty("送货人")
private String DeliveryMan;
@ApiModelProperty("送货人电话号码")
private String DeliveryManTel;
@ApiModelProperty("成功与否 true/false")
private String Success;
@ApiModelProperty("所在城市")
private String Location;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@ApiModel("事件轨迹集")
public static class TraceItem {
/**
* {@link KdniaoLogisticsStatusEnum }
*/
@ApiModelProperty("当前状态(同StateEx)")
private Integer Action;
@ApiModelProperty("状态名称")
private String actionName;
@ApiModelProperty("描述")
private String AcceptStation;
@ApiModelProperty("时间")
private String AcceptTime;
@ApiModelProperty("所在城市")
private String Location;
}
public void handleData() {
this.statusName = KdniaoLogisticsStatusEnum.getEnum(this.State).getDesc();
this.statusExName = KdniaoLogisticsStatusEnum.getEnum(this.StateEx).getDesc();
if (CollectionUtils.isEmpty(this.Traces)) {
this.Traces = Lists.newArrayList();
}
this.Traces.forEach(item -> item.actionName = KdniaoLogisticsStatusEnum.getEnum(item.Action).getDesc());
}
}
工具类( 组装应用级参数和 组装系统级参数需要根据接口文档来,其他照抄就完了)
组件系统参数,需要修改RequestType的值,要设置为8001
package com.ruoyi.android.customer.util;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.ruoyi.android.customer.dto.KdniaoApiDTO;
import com.ruoyi.android.customer.entity.KdniaoLogisticsStatusEnum;
import com.ruoyi.android.customer.vo.KdniaoApiVO;
import com.ruoyi.common.utils.sign.Base64;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.client.RestTemplate;
import org.springframework.util.Assert;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ruoyi.android.customer.config.KdniaoConfig;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@Component
public class KdniaoUtil {
private static KdniaoConfig kdniaoConfig;
@Autowired
public void setKdniaoConfig(KdniaoConfig kdniaoConfig) {
KdniaoUtil.kdniaoConfig = kdniaoConfig;
}
/**
* 快递查询接口
*
* @param queryDTO 请求参数
* @return 物流信息
* @author zhengqingya
* @date 2021/10/25 17:39
*/
public static KdniaoApiVO getLogisticInfo(KdniaoApiDTO queryDTO) {
try {
// 设置基础配置信息
queryDTO.setEBusinessID(kdniaoConfig.getBusinessId());
queryDTO.setApiKey(kdniaoConfig.getApiKey());
queryDTO.setReqURL(kdniaoConfig.getApiUrl());
KdniaoApiVO kdniaoApiVO = new KdniaoUtil().getLogisticBase(queryDTO);
if (kdniaoApiVO == null) {
throw new RuntimeException("快递查询接口返回为空");
}
if (!"true".equals(kdniaoApiVO.getSuccess())) {
log.error("快递鸟API调用失败: {}", kdniaoApiVO.getReason());
return kdniaoApiVO;
}
kdniaoApiVO.handleData();
return kdniaoApiVO;
} catch (Exception e) {
log.error("快递查询异常", e);
throw new RuntimeException("快递查询失败:" + e.getMessage());
}
}
/**
* 快递查询接口
*
* @param queryDTO 请求参数
* @return 物流信息
* @author zhengqingya
* @date 2021/10/25 17:39
*/
@SneakyThrows(Exception.class)
private KdniaoApiVO getLogisticBase(KdniaoApiDTO queryDTO) {
String EBusinessID = queryDTO.getEBusinessID();
String ApiKey = queryDTO.getApiKey();
String ReqURL = queryDTO.getReqURL();
// 组装应用级参数
Map<String, Object> requestMap = new HashMap<>();
requestMap.put("OrderCode", ""); // 订单编号可选
requestMap.put("ShipperCode", queryDTO.getShipperCode()); // 快递公司编码
requestMap.put("LogisticCode", queryDTO.getLogisticCode()); // 快递单号
requestMap.put("Sort", "0"); // 轨迹按时间排序,0-升序
// 如果是顺丰快递或跨越快递,必须传入手机号后四位
if ("SF".equals(queryDTO.getShipperCode()) || "KYSY".equals(queryDTO.getShipperCode())) {
if (queryDTO.getCustomerName() == null || queryDTO.getCustomerName().length() != 4) {
throw new RuntimeException(queryDTO.getShipperCode() + "快递必须提供正确的手机号后四位");
}
requestMap.put("CustomerName", queryDTO.getCustomerName());
}
String RequestData = JSON.toJSONString(requestMap);
log.info("[快递鸟] 请求数据: {}", RequestData);
// 组装系统级参数
Map<String, String> params = Maps.newHashMap();
params.put("RequestData", urlEncoder(RequestData, "UTF-8"));
params.put("EBusinessID", EBusinessID);
params.put("RequestType", "8001"); // 即时查询接口指令8001
String dataSign = encrypt(RequestData, ApiKey, "UTF-8");
params.put("DataSign", urlEncoder(dataSign, "UTF-8"));
params.put("DataType", "2");
// 发送请求
String resultJson = this.sendPost(ReqURL, params);
log.info("[快递鸟] 返回结果: {}", resultJson);
if (resultJson == null || resultJson.isEmpty()) {
throw new RuntimeException("快递查询接口返回为空");
}
return JSON.parseObject(resultJson, KdniaoApiVO.class);
}
/**
* MD5加密
* @param str 内容
* @param charset 编码方式
*/
private String MD5(String str, String charset) throws Exception {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(str.getBytes(charset));
byte[] result = md.digest();
StringBuilder sb = new StringBuilder(32);
for (int i = 0; i < result.length; i++) {
int val = result[i] & 0xff;
if (val <= 0xf) {
sb.append("0");
}
sb.append(Integer.toHexString(val));
}
return sb.toString().toLowerCase();
}
/**
* base64编码
* @param str 内容
* @param charset 编码方式
*/
private String base64(String str, String charset) throws UnsupportedEncodingException {
String encoded = Base64.encode(str.getBytes(charset));
return encoded;
}
/**
* URL编码
*/
private String urlEncoder(String str, String charset) throws UnsupportedEncodingException {
return URLEncoder.encode(str, charset);
}
/**
* 电商Sign签名生成
* @param content 内容
* @param keyValue ApiKey
* @param charset 编码方式
* @return DataSign签名
*/
private String encrypt(String content, String keyValue, String charset) throws Exception {
if (keyValue != null) {
return base64(MD5(content + keyValue, charset), charset);
}
return base64(MD5(content, charset), charset);
}
/**
* 向指定 URL 发送POST方法的请求
* url 发送请求的 URL
* params 请求的参数集合
*
* @return 远程资源的响应结果
*/
@SneakyThrows(Exception.class)
private String sendPost(String url, Map<String, String> params) {
OutputStreamWriter out = null;
BufferedReader in = null;
StringBuilder result = new StringBuilder();
HttpURLConnection conn = null;
try {
URL realUrl = new URL(url);
conn = (HttpURLConnection) realUrl.openConnection();
// 设置连接和读取超时
conn.setConnectTimeout(30000); // 30秒连接超时
conn.setReadTimeout(30000); // 30秒读取超时
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
// 获取URLConnection对象对应的输出流
out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
// 发送请求参数
if (params != null) {
StringBuilder param = new StringBuilder();
for (Map.Entry<String, String> entry : params.entrySet()) {
if (param.length() > 0) {
param.append("&");
}
param.append(entry.getKey());
param.append("=");
param.append(entry.getValue());
}
log.info("[快递鸟] 请求URL: {}", url);
log.info("[快递鸟] 请求参数: {}", param);
out.write(param.toString());
}
// flush输出流的缓冲
out.flush();
// 获取响应码
int responseCode = conn.getResponseCode();
log.info("[快递鸟] 响应码: {}", responseCode);
// 读取响应
if (responseCode == HttpURLConnection.HTTP_OK) {
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
} else {
in = new BufferedReader(new InputStreamReader(conn.getErrorStream(), "UTF-8"));
}
String line;
while ((line = in.readLine()) != null) {
result.append(line);
}
log.info("[快递鸟] 响应结果: {}", result);
if (responseCode != HttpURLConnection.HTTP_OK) {
throw new RuntimeException("HTTP请求失败,响应码: " + responseCode + ",响应内容:" + result);
}
} catch (Exception e) {
log.error("[快递鸟] 请求异常", e);
throw new RuntimeException("请求快递鸟接口失败: " + e.getMessage());
} finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
if (conn != null) {
conn.disconnect();
}
} catch (IOException ex) {
log.error("[快递鸟] 关闭连接异常", ex);
}
}
if (result.length() == 0) {
throw new RuntimeException("快递鸟接口返回为空");
}
return result.toString();
}
}
七.测试接口
/**
* 查询物流信息
* @param id
* @return 物流信息
*/
@PostMapping("/logistic/{id}")
public AjaxResult getLogisticInfo(@PathVariable Long id) {
Goods goods=iShoppingCentreService.getProductDetail(id);
System.out.println("goods"+goods);
KdniaoApiDTO kdniaoApiDTO=new KdniaoApiDTO();
kdniaoApiDTO.setShipperCode(goods.getShipperCode());//获取商品的物流公司编码
kdniaoApiDTO.setLogisticCode(goods.getLogisticCode());//获取商品的物流快递单号
System.out.println("物流传入"+kdniaoApiDTO);
KdniaoApiVO logisticInfo = KdniaoUtil.getLogisticInfo(kdniaoApiDTO);
if ("true".equals(logisticInfo.getSuccess())) {
return AjaxResult.success(logisticInfo);
} else {
return AjaxResult.error(logisticInfo.getReason());
}
}
八.总体解析
工具类主要功能
主要功能:
组装请求参数
2.生成签名
发送HTTP请求
解析响应结果
关键方法:
public static KdniaoApiVO getLogisticInfo(KdniaoApiDTO queryDTO) // 对外暴露的查询接口
private KdniaoApiVO getLogisticBase(KdniaoApiDTO queryDTO) // 基础查询实现
private String encrypt(String content, String keyValue, String charset) // 签名生成
private String sendPost(String url, Map<String, String> params) // 发送POST请求
控制层解析
@PostMapping("/logistic/{id}")
public AjaxResult getLogisticInfo(@PathVariable Long id) {
// 1. 获取商品信息
Goods goods = iShoppingCentreService.getProductDetail(id);
// 2. 组装查询参数
KdniaoApiDTO kdniaoApiDTO = new KdniaoApiDTO();
kdniaoApiDTO.setShipperCode(goods.getShipperCode());
kdniaoApiDTO.setLogisticCode(goods.getLogisticCode());
// 3. 调用快递鸟API
KdniaoApiVO logisticInfo = KdniaoUtil.getLogisticInfo(kdniaoApiDTO);
// 4. 处理返回结果
if ("true".equals(logisticInfo.getSuccess())) {
return AjaxResult.success(logisticInfo);
} else {
return AjaxResult.error(logisticInfo.getReason());
}
}
关键技术点
使用 @ConfigurationProperties 自动注入配置
使用 Base64 和 MD5 进行签名加密
使用 HttpURLConnection 发送 POST 请求
使用 FastJSON 处理 JSON 数据
使用 Lombok 简化代码
统一的异常处理和日志记录
注意事项
需要在快递鸟官网注册账号并开通API服务
顺丰快递必须提供手机号后四位
注意正确配置商户ID和API密钥
建议对接口调用进行限流和缓存
需要处理各种异常情况