SpringBoot实现阿里云快递物流查询的示例代码

 更新时间:2021年10月29日 09:41:14   作者:郑清  
本文将基于springboot实现快递物流查询,物流信息的获取通过阿里云第三方实现,具有一定的参考价值,感兴趣的可以了解一下

一、前言

本文将基于springboot2.4.0实现快递物流查询,物流信息的获取通过阿里云第三方实现

可参考: https://market.aliyun.com/products/57124001/cmapi022273.html?spm=5176.730005.productlist.d_cmapi022273.e8357d36FVX3Eu&innerSource=search#sku=yuncode1627300000

在这里插入图片描述

快递查询API,快递识别单号,快递接口可查询上百家快递公司及物流快递信息包括:顺丰、申通、圆通、韵达、中通、汇通、EMS、天天、国通、德邦、宅急送等几百家快递物流公司单号查询接口。与官网实时同步更新,包含快递送达时间。

二、快递物流查询

注:需要购买快递物流查询接口服务获取AppCode

在这里插入图片描述

工具类

其中http请求工具类自行查看demo源码

@Slf4j
public class LogisticUtil {

    /**
     * 查询物流信息
     *
     * @param params 提交参数
     * @return 物流信息
     * @author zhengqingya
     * @date 2021/10/23 10:48 下午
     */
    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

在这里插入图片描述

本文demo源码

https://gitee.com/zhengqingya/java-workspace

到此这篇关于SpringBoot实现阿里云快递物流查询的示例代码的文章就介绍到这了,更多相关SpringBoot 阿里云快递物流查询内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • java实现图片分割指定大小

    java实现图片分割指定大小

    这篇文章主要为大家详细介绍了java实现图片分割指定大小,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • Spring中@RabbitHandler和@RabbitListener的区别详析

    Spring中@RabbitHandler和@RabbitListener的区别详析

    @RabbitHandler是用于处理消息的方法注解,它与@RabbitListener注解一起使用,这篇文章主要给大家介绍了关于Spring中@RabbitHandler和@RabbitListener区别的相关资料,需要的朋友可以参考下
    2024-02-02
  • JAVA字符串格式化-String.format()的使用

    JAVA字符串格式化-String.format()的使用

    本篇文章主要介绍了JAVA字符串格式化-String.format()的使用,具有一定的参考价值,有需要的可以了解一下。
    2016-11-11
  • SpringBoot中使用@Value注解注入详解

    SpringBoot中使用@Value注解注入详解

    这篇文章主要介绍了SpringBoot中的@Value注入详解,在SpringBoot中,@Value注解可以注入一些字段的普通属性,并且会自动进行类型转换,本文对这些类型进行总结,需要的朋友可以参考下
    2023-08-08
  • java_object的简单使用详解

    java_object的简单使用详解

    下面小编就为大家带来一篇java_object的简单使用详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-06-06
  • Spring中@Configuration注解修改的类生成代理原因解析

    Spring中@Configuration注解修改的类生成代理原因解析

    大家好,本篇文章主要讲的是Spring中@Configuration注解修改的类生成代理原因解析,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下
    2022-02-02
  • 自从在 IDEA 中用了热部署神器 JRebel 之后,开发效率提升了 10(真棒)

    自从在 IDEA 中用了热部署神器 JRebel 之后,开发效率提升了 10(真棒)

    在javaweb开发过程中,使用热部署神器 JRebel可以使class类还是更新spring配置文件都能立马见到效率,本文给大家介绍JRebel的两种安装方法,小编建议使用第二种方法,具体安装步骤跟随小编一起看看吧
    2021-06-06
  • 构建Maven多模块项目的方法

    构建Maven多模块项目的方法

    这篇文章主要介绍了构建Maven多模块项目的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-08-08
  • Java Calendar类常用示例_动力节点Java学院整理

    Java Calendar类常用示例_动力节点Java学院整理

    从JDK1.1版本开始,在处理日期和时间时,系统推荐使用Calendar类进行实现。接下来通过实例代码给大家详细介绍Java Calendar类相关知识,需要的朋友参考下吧
    2017-04-04
  • 使用Java自制一个一个Nacos

    使用Java自制一个一个Nacos

    Nacos是 Dynamic Naming and Configuration Service的首字母简称,一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台,本文将尝试用Java实现一个Nacos,感兴趣的可以了解下
    2024-01-01

最新评论