本篇内容介绍了“怎么用SpringBoot实现快递物流查询”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
快递物流查询
注:需要购买快递物流查询接口服务获取AppCode
工具类
其中http请求工具类自行查看demo源码
@Slf4jpublic class LogisticUtil { public static LogisticVO getLogisticInfo(LogisticDTO params) { String no = params.getNo(); String type = params.getType(); String appCode = params.getAppCode(); // 请求地址 String requestUrl = String.format("https://kdwlcxf.market.alicloudapi.com/kdwlcx?no=%s&type=%s", no, StringUtils.isBlank(type) ? "" : type); // 发起请求 Map<String, String> headerMap = Maps.newHashMap(); headerMap.put("Authorization", String.format("APPCODE %s", appCode)); String resultJson = HttpUtil.getUrl(requestUrl, headerMap); LogisticApiResult logisticApiResult = JSON.parseObject(resultJson, LogisticApiResult.class); Assert.notNull(logisticApiResult, "参数异常"); Assert.isTrue(logisticApiResult.getStatus() == 0, logisticApiResult.getMsg()); return logisticApiResult.getResult(); }}
请求实体类
@Data@SuperBuilder@NoArgsConstructor@AllArgsConstructor@ApiModel("物流-查询参数")public class LogisticDTO { @ApiModelProperty(value = "快递单号 【顺丰请输入运单号 : 收件人或寄件人手机号后四位。例如:123456789:1234】", required = true, example = "780098068058") private String no; @ApiModelProperty(value = "快递公司代码: 可不填自动识别,填了查询更快【代码见附表】", required = true, example = "zto") private String type; @ApiModelProperty(value = "appCode", required = true, example = "xxx") private String appCode;}
响应实体类
@Data@Builder@NoArgsConstructor@AllArgsConstructor@ApiModel("物流-api响应结果")public class LogisticApiResult { @ApiModelProperty("状态码") private Integer status; @ApiModelProperty("提示信息") private String msg; @ApiModelProperty("结果集") private LogisticVO result;}
@Data@Builder@NoArgsConstructor@AllArgsConstructor@ApiModel("物流-响应参数")public class LogisticVO { @ApiModelProperty("运单编号") private String number; @ApiModelProperty("快递公司编码[见附表]") private String type; @ApiModelProperty("投递状态 0快递收件(揽件)1在途中 2正在派件 3已签收 4派送失败 5.疑难件 6.退件签收") private String deliverystatus; @ApiModelProperty("是否本人签收") private String issign; @ApiModelProperty("快递公司名字") private String expName; @ApiModelProperty("快递公司官网") private String expSite; @ApiModelProperty("快递公司电话") private String expPhone; @ApiModelProperty("快递员") private String courier; @ApiModelProperty("快递员电话") private String courierPhone; @ApiModelProperty("最新轨迹的时间") private String updateTime; @ApiModelProperty("发货到收货耗时(截止最新轨迹)") private String takeTime; @ApiModelProperty("快递公司logo") private String logo; @ApiModelProperty("事件轨迹集") private List<LogisticItem> list; @Data @Builder @NoArgsConstructor @AllArgsConstructor @ApiModel("事件轨迹集") public static class LogisticItem { @ApiModelProperty("时间点") private String time; @ApiModelProperty("事件详情") private String status; }}
测试api
@Slf4j@RestController@RequestMapping("/test")@Api(tags = "测试api")public class TestController { @ApiOperation("查询物流信息") @GetMapping("getLogistic") public LogisticVO getLogistic(@ModelAttribute LogisticDTO params) { return LogisticUtil.getLogisticInfo(params); }}
接口文档 http://127.0.0.1/doc.html
“怎么用SpringBoot实现快递物流查询”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注编程网网站,小编将为大家输出更多高质量的实用文章!