目录
一、注册快递100
二、技术文档地址
三、需要认证的key和comcumer
四、spring boot 整合快递 100使用
4.1 引入快递100和hutool的依赖
4.2 将key和comcumer写入application.properties文件中
4.3 新建一个modle,用于将查出来的json数据转成对象
4.4 新建一个controller
4.5 结果显示
五、源码参考
一、注册快递100
https://api.kuaidi100.com/manager/v2/query/overview
二、技术文档地址
https://api.kuaidi100.com/document/5f0ffb5ebc8da837cbd8aefc
三、需要认证的key和comcumer
登录到快递100的后台
key:uxdpeXvk7416
customer: A04A2C3A70259125790AE81164145FE2
四、spring boot 整合快递 100使用
4.1 引入快递100和hutool的依赖
<!-- 添加快递100的依赖 --><dependency><groupId>com.github.kuaidi100-api</groupId><artifactId>sdk</artifactId><version>1.0.11</version></dependency><!--添加hutool的依赖 --><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.18</version></dependency>
4.2 将key和comcumer写入application.properties文件中
# 定义出快递100 key和customer
kuaidi100.key=uxdpeXvk7416
kuaidi100.customer=A04A2C3A70259125790AE81164145FE2
4.3 新建一个modle,用于将查出来的json数据转成对象
/** Copyright (c) 2020, 2024, webrx.cn All rights reserved.**/
package com.beiyou.model;import lombok.Data;import java.util.List;@Data
public class KuaidiInfo {private String message;private List<Step> data;@Dataclass Step {private String time;private String ftime;private String context;}
}
4.4 新建一个controller
/** Copyright (c) 2020, 2024, webrx.cn All rights reserved.**/
package com.beiyou.controller;import cn.hutool.json.JSONUtil;
import com.beiyou.model.KuaidiInfo;
import com.google.gson.Gson;
import com.kuaidi100.sdk.api.QueryTrack;
import com.kuaidi100.sdk.contant.CompanyConstant;
import com.kuaidi100.sdk.core.IBaseClient;
import com.kuaidi100.sdk.pojo.HttpResult;
import com.kuaidi100.sdk.request.QueryTrackParam;
import com.kuaidi100.sdk.request.QueryTrackReq;
import com.kuaidi100.sdk.utils.SignUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/api/kd100")
public class KD100Controller {/*** 定义出快递100 的customer和key*/@Value("${kuaidi100.customer}")private String customer;@Value("${kuaidi100.key}")private String key;@GetMappingpublic KuaidiInfo queryTrack() throws Exception {QueryTrackReq queryTrackReq = new QueryTrackReq();QueryTrackParam queryTrackParam = new QueryTrackParam();queryTrackParam.setCom(CompanyConstant.ZT);queryTrackParam.setNum("78414126296826");String param = new Gson().toJson(queryTrackParam);queryTrackReq.setParam(param);queryTrackReq.setCustomer("A04A2C3A70259125790AE81164145FE2");queryTrackReq.setSign(SignUtils.querySign(param, key, customer));IBaseClient baseClient = new QueryTrack();HttpResult h = baseClient.execute(queryTrackReq);String body = h.getBody();/*** 1. 使用hutooL工具将json字符串转换为对象*/KuaidiInfo kuaidiInfo = JSONUtil.toBean(body, KuaidiInfo.class);/*** 通过debug可以得出 无论输入的信息是否有误 都会返回200* 所以我们可以根据message来判断是否成功*/return kuaidiInfo;}}
4.5 结果显示
还差一个state状态
我们可以新建一个枚举 用于定义状态
package com.beiyou.emul;import lombok.Getter;
import lombok.Setter;import java.util.Arrays;
import java.util.Optional;//轨迹当前状态,如:0在途,1揽收,2疑难,3签收,4退签,5派件,6退回,7转单,10待清关,11清关中,12已清关,13清关异常,14收件人拒签等
public enum KDstate {SALE(0, "在途"),OFF_SALE(1, "揽收"),DELETE(5, "派件");@Setter@Getterprivate Integer code; //状态@Setter@Getterprivate String desc; //状态描述KDstate(Integer code, String desc) {this.code = code;this.desc = desc;}public static KDstate findByCode(Integer code) {//jdk1.8提供了哪些新特性Optional<KDstate> optional = Arrays.stream(KDstate.values()).filter(item -> item.getCode().equals(code)).findFirst();/*if (optional.isPresent()){return optional.get();}return null;*/return optional.orElse(null);}
}
修改最开始的KuaidiInfo
/** Copyright (c) 2020, 2024, webrx.cn All rights reserved.**/
package com.beiyou.model;import com.beiyou.emul.KDstate;
import lombok.Data;import java.util.List;@Data
public class KuaidiInfo {private String message;private List<Step> data;// 定义快递状态private String state;/*** 根据传来的state 返回对应的描述* @return*/public String getStateX() {return KDstate.findByCode(Integer.parseInt(this.state)).getDesc();}private String stateX;@Dataclass Step {private String time;private String ftime;private String context;}
}
此时前端只需要渲染 stateX的数据即可
五、源码参考
源码我已经放到了云效,有兴趣可以参考
https://codeup.aliyun.com/62858d45487c500c27f5aab5/SpringBoot-KD100-Huang.git