【快递100】订阅地图轨迹推送服务并接收推送
快递100官方文档地址:https://api.kuaidi100.com/document/603f47dfa62a19500e19866f
订阅接口
代码实现
SysExpressController.java
/**
* 地图轨迹自动订阅接口
*/
@Scheduled(cron = "0 0/1 * * * ?")
public void autoSubscribeMap() throws Exception {
// 获取距今一个月内,所有未签收,且未订阅推送的快递单
List<SysExpress> allExpress = expressService.selectNotCheckAndNotSign();
if(allExpress != null && allExpress.size() > 0){
logger.info("地图轨迹自动订阅接口,执行时间:{}|可订阅的快递单数量为:{}", DateUtils.dateFull(),allExpress.size());
// 订阅地图轨迹推送服务
expressService.subscribeMap(allExpress);
}
}
@Scheduled
注解是spring boot的定时任务注解。
该注解启用,需要在主启动类上加上@EnableScheduling
。
SysExpressServiceImpl.java
// 授权key
private String key = PropertiesReader.get("key");
private String customer = PropertiesReader.get("customer");
// 订阅推送接口回调地址
private String callbackMapUrl = PropertiesReader.get("callbackMapUrl ");
// 开启行政区域解析
private String resultv2 = PropertiesReader.get("resultv2");
/**
* 订阅快递地图轨迹服务(接口未启用)
*/
@Override
@Transactional
public void subscribeMap(List<SysExpress> allExpress) throws Exception {
SubscribeParam param = getSubscribeParam();
// 获取request请求
SubscribeReq request = getSubscribeReq();
// 设置请求接口
IBaseClient subscribeWithMap = new SubscribeWithMap();
for (SysExpress express : allExpress) {
param.setCompany(express.getCompanyCode());
param.setNumber(express.getExpressNum());
param.setFrom(express.getFromCity());
param.setTo(express.getToCity());
request.setParam(ChangeToJson(param));
// 订阅地图推送
HttpResult httpResult = subscribeWithMap.execute(request);
saveToMysql(request.getParam(),express,httpResult);
}
}
/**
* 封装请求参数
*/
public SubscribeParam getSubscribeParam(){
SubscribeParameters parameter = new SubscribeParameters();
// 设置回调地址,在account.properties中配置
parameter.setCallbackurl(callbackMapUrl );
//开通行政区域解析功能以及物流轨迹增加物流状态值
parameter.setResultv2(resultv2);
// 设置参数(设置key)
SubscribeParam param = new SubscribeParam();
param.setKey(key);
param.setParameters(parameter);
return param;
}
/**
* 封装request请求
*/
public SubscribeReq getSubscribeReq(){
SubscribeReq request = new SubscribeReq();
request.setSchema(ApiInfoConstant.SUBSCRIBE_SCHEMA);
return request;
}
回调接口
代码实现
SysExpressController.java
/**
* 快递100地图轨迹回调接口
*/
@PostMapping("/callbackMap")
public SubscribeResp callBackMap(HttpServletRequest request) throws Exception {
return expressService.handleCallBackMap(request);
}
SysExpressServiceImpl.java
/**
* 处理快递100推送请求并响应
* @param request 快递100的推送请求
*/
@Override
@Transactional
public SubscribeResp handleCallBackMap(HttpServletRequest request) throws Exception {
// 获取参数
String param = request.getParameter("param");
// 解析报文
SubscribeWithMapPushParamResp mapResponse = new Gson().fromJson(param, SubscribeWithMapPushParamResp.class);
// 处理报文
handleResponseMap(param,mapResponse);
return getSubscribeResp();
}
/**
* 业务处理
* @param param 返回的参数
*/
@Transactional
public void handleResponseMap(String param,SubscribeWithMapPushParamResp mapResponse) throws Exception {
// TODO 目前业务不需要地图服务,后期需要再写
}