目录
1.引入依赖
2.定义配置信息
3.模块结构
4.Controller
5.Service实现类
6.返回数据dto以及dto中的数据dto
7.测试运行
今天也是接到了这个任务,官网有小demo,可以下载下来参考test中代码
官方文档地址:
实时快递查询接口技术文档-快递100API开放平台
1.引入依赖
<!--快递100-->
<dependency>
<groupId>com.github.kuaidi100-api</groupId>
<artifactId>sdk</artifactId>
<version>1.0.11</version>
</dependency>
2.定义配置信息
这个要到官网申请 注册-快递100API开放平台|快递接口免费申请
#快递100配置信息
kuaidi100:
key: *******
customer: **********
3.模块结构
具体用到这些类,官方demo里有的我就不展示了
4.Controller
package cn.homed.shop.express100.web;
import cn.homed.common.entity.MsgBean;
import cn.homed.shop.express100.service.Express100Service;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/dim/express100/")
@Tag(name ="快递100", description = "v-1.3 -- 快递100")
public class Express100Controller {
@Autowired
private Express100Service express100Service;
@Operation(summary = "根据快递单号查询物流信息",description = "根据快递单号查询物流信息")
@RequestMapping(value = "/findOrder", method = RequestMethod.GET)
public MsgBean findOrder(
@RequestParam(value = "orderId", required = true) String orderId,
@RequestParam(value = "tplCode", required = true) String tplCode) {
return express100Service.findOrder(orderId,tplCode);
}
}
5.Service实现类
package cn.homed.shop.express100.service.impl;
import cn.homed.common.entity.MsgBean;
import cn.homed.shop.express100.dto.Express100DTO;
import cn.homed.shop.express100.service.Express100Service;
import cn.homed.shop.express100.utils.SignUtils;
import com.google.gson.Gson;
import com.kuaidi100.sdk.api.QueryTrack;
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 org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
/**
* 快递100
* @author yang
* @version 1.0.0
*/
@Service
public class Express100ServiceImpl implements Express100Service {
@Value("${kuaidi100.key}")
private String key;
@Value("${kuaidi100.customer}")
private String customer;
@Override
public MsgBean findOrder(String orderId,String tplCode) {
Express100DTO response = null;
try {
QueryTrackReq queryTrackReq = new QueryTrackReq();
QueryTrackParam queryTrackParam = new QueryTrackParam();
queryTrackParam.setCom(tplCode);
queryTrackParam.setNum(orderId);
String param = new Gson().toJson(queryTrackParam);
queryTrackReq.setParam(param);
queryTrackReq.setCustomer(customer);
queryTrackReq.setSign(SignUtils.querySign(param ,key,customer));
IBaseClient baseClient = new QueryTrack();
HttpResult execute = baseClient.execute(queryTrackReq);
// 对返回的数据进行反序列化处理
Gson gson = new Gson();
String responseBody = execute.getBody();
response = gson.fromJson(responseBody, Express100DTO.class);
} catch (Exception e) {
e.printStackTrace();
}
assert response != null;
return MsgBean.success(response.getData(),"查询物流信息成功");
}
}
6.返回数据dto以及dto中的数据dto
用于反序列化
package cn.homed.shop.express100.dto;
import lombok.Data;
import lombok.ToString;
import java.util.List;
/**
* 快递100DTO
*/
@Data
@ToString
public class Express100DTO {
private String message;
private String nu;
private String ischeck;
private String com;
private String status;
private List<LogisticsInfoDTO> data;
}
package cn.homed.shop.express100.dto;
import lombok.Data;
import lombok.ToString;
/**
* 快递100DTO
*/
@Data
@ToString
public class LogisticsInfoDTO {
private String time;
private String ftime;
private String context;
}
7.测试运行
可以看到也是查询到了数据
这里传快递单号可以参考contant包下的CompanyConstant
点个赞再走吧~