4
2
0
2

Springboot整合阿里云快递物流查询

一、Springboot整合

官网: https://market.aliyun.com/products/57124001/cmapi022273.html

image

1.1 引入openfeign依赖

<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-openfeign -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
    <version>3.0.4</version>
</dependency>

1.2 启动 openfeign

主启动类上添加 @EnableFeignClients

1.3 创建远程调用客户端

package com.system.system.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @Author codertl
 * @Date 2022/4/15
 */
@FeignClient(url = "https://wuliu.market.alicloudapi.com", name = "PhysicalSearch")
public interface PhysicalRemoteClient {

    @GetMapping("/kdi")
    String physicalSearch(@RequestParam("no") String no, @RequestHeader("Authorization") String authorization);
}

1.4 application.yml 添加内容

# 物流查询
aliyun:
  physical:
    appcode: #开通服务后 买家中心-查看AppCode

1.5 物流查询service接口

package com.system.system.service;

import com.system.common.core.domain.AjaxResult;

/**
 * 物流查询服务
 * @Author tl
 * @Date 2022/4/15
 */
public interface PhysicalSearchService {

    /**
     * 根据物流编号查询物流信息
     * @param expressNo
     * @return
     */
    AjaxResult search(String expressNo);
}

1.6 物流查询impl实现类

package com.h5system.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.h5system.common.core.domain.AjaxResult;
import com.h5system.system.domain.dto.physical.PhysicalErrorDto;
import com.h5system.system.domain.dto.physical.PhysicalSuccessDto;
import com.h5system.system.feign.PhysicalRemoteClient;
import com.h5system.system.service.PhysicalSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @Author tl
 * @Date 2022/4/15
 */
@Service
public class PhysicalSearchServiceImpl implements PhysicalSearchService {

    @Value("${aliyun.physical.appcode}")
    private String appcode;

    @Autowired
    private PhysicalRemoteClient physicalRemoteClient;

    /**
     * 根据物流编号查询物流信息
     * @param expressNo
     * @return
     */
    @Override
    public AjaxResult search(String expressNo) {
        String physicalSearch = physicalRemoteClient.physicalSearch(expressNo, "APPCODE " + appcode);
        try {
            PhysicalSuccessDto physicalSuccessDto = JSON.parseObject(physicalSearch, PhysicalSuccessDto.class);
            return AjaxResult.success(physicalSuccessDto);
        } catch (Exception e) {
            PhysicalErrorDto physicalErrorDto = JSON.parseObject(physicalSearch, PhysicalErrorDto.class);
            return AjaxResult.error("500", physicalErrorDto);
        }
    }
}

1.7 相关实体类

AjaxResult

package com.system.common.core.domain;

import com.system.common.constant.HttpStatus;
import com.system.common.utils.StringUtils;

import java.util.HashMap;


public class AjaxResult extends HashMap<String, Object>
{
    private static final long serialVersionUID = 1L;

    /** 状态码 */
    public static final String CODE_TAG = "code";

    /** 返回内容 */
    public static final String MSG_TAG = "msg";

    /** 数据对象 */
    public static final String DATA_TAG = "data";

    /**
     * 初始化一个新创建的 AjaxResult 对象,使其表示一个空消息。
     */
    public AjaxResult()
    {
    }

    /**
     * 初始化一个新创建的 AjaxResult 对象
     * 
     * @param code 状态码
     * @param msg 返回内容
     */
    public AjaxResult(int code, String msg)
    {
        super.put(CODE_TAG, code);
        super.put(MSG_TAG, msg);
    }

    /**
     * 初始化一个新创建的 AjaxResult 对象
     * 
     * @param code 状态码
     * @param msg 返回内容
     * @param data 数据对象
     */
    public AjaxResult(int code, String msg, Object data)
    {
        super.put(CODE_TAG, code);
        super.put(MSG_TAG, msg);
        if (StringUtils.isNotNull(data))
        {
            super.put(DATA_TAG, data);
        }
    }

    /**
     * 返回成功消息
     * 
     * @return 成功消息
     */
    public static AjaxResult success()
    {
        return AjaxResult.success("操作成功");
    }

    /**
     * 返回成功数据
     * 
     * @return 成功消息
     */
    public static AjaxResult success(Object data)
    {
        return AjaxResult.success("操作成功", data);
    }
    public static AjaxResult successAndData(Object data)
    {
        return AjaxResult.success("操作成功", data);
    }

    /**
     * 返回成功消息
     * 
     * @param msg 返回内容
     * @return 成功消息
     */
    public static AjaxResult success(String msg)
    {
        return AjaxResult.success(msg, null);
    }

    /**
     * 返回成功消息
     * 
     * @param msg 返回内容
     * @param data 数据对象
     * @return 成功消息
     */
    public static AjaxResult success(String msg, Object data)
    {
        return new AjaxResult(HttpStatus.SUCCESS, msg, data);
    }

    /**
     * 返回错误消息
     * 
     * @return
     */
    public static AjaxResult error()
    {
        return AjaxResult.error("操作失败");
    }

    /**
     * 返回错误消息
     * 
     * @param msg 返回内容
     * @return 警告消息
     */
    public static AjaxResult error(String msg)
    {
        return AjaxResult.error(msg, null);
    }

    /**
     * 返回错误消息
     * 
     * @param msg 返回内容
     * @param data 数据对象
     * @return 警告消息
     */
    public static AjaxResult error(String msg, Object data)
    {
        return new AjaxResult(HttpStatus.ERROR, msg, data);
    }

    /**
     * 返回错误消息
     * 
     * @param code 状态码
     * @param msg 返回内容
     * @return 警告消息
     */
    public static AjaxResult error(int code, String msg)
    {
        return new AjaxResult(code, msg, null);
    }

    /**
     * 方便链式调用
     *
     * @param key 键
     * @param value 值
     * @return 数据对象
     */
    @Override
    public AjaxResult put(String key, Object value)
    {
        super.put(key, value);
        return this;
    }
}

物流查询成功

package com.h5system.system.domain.dto.physical;

import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
 * 物流查询成功返回数据
 * @Author tl
 * @Date 2022/4/15
 */
@Data
public class PhysicalSuccessDto implements Serializable {
    private static final long serialVersionUID = 6158565304723260278L;

    @JSONField(name = "status")
    private String status;
    @JSONField(name = "msg")
    private String msg;
    @JSONField(name = "result")
    private ResultDTO result;

    @Data
    public static class ResultDTO implements Serializable {
        @JSONField(name = "number")
        private String number;
        @JSONField(name = "type")
        private String type;
        @JSONField(name = "deliverystatus")
        private String deliverystatus;
        @JSONField(name = "issign")
        private String issign;
        @JSONField(name = "expName")
        private String expName;
        @JSONField(name = "expSite")
        private String expSite;
        @JSONField(name = "expPhone")
        private String expPhone;
        @JSONField(name = "courier")
        private String courier;
        @JSONField(name = "courierPhone")
        private String courierPhone;
        @JSONField(name = "updateTime")
        private String updateTime;
        @JSONField(name = "takeTime")
        private String takeTime;
        @JSONField(name = "list")
        private List<ListDTO> list;

        @Data
        public static class ListDTO implements Serializable {
            @JSONField(name = "time")
            private String time;
            @JSONField(name = "status")
            private String status;
        }
    }
}

物流查询失败

package com.h5system.system.domain.dto.physical;

import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
 * 物流查询失败信息
 * @Author tl
 * @Date 2022/4/15
 */
@Data
public class PhysicalErrorDto implements Serializable {
    private static final long serialVersionUID = -2951802283880438306L;


    @JSONField(name = "status")
    private String status;
    @JSONField(name = "msg")
    private String msg;
    @JSONField(name = "result")
    private ResultDTO result;

    @Data
    public static class ResultDTO implements Serializable {
        @JSONField(name = "number")
        private String number;
        @JSONField(name = "type")
        private String type;
        @JSONField(name = "list")
        private List<?> list;
    }
}

1.8 controller测试

/**
 * 查询物流信息
 */
@ApiImplicitParams({
            @ApiImplicitParam(value = "填写运单号", name = "expressNo", required = true, paramType = "query", dataType = "string", dataTypeClass = String.class)
    })
@ApiOperation(value = "查询物流信息", notes = "查询物流信息", httpMethod = "GET")
@GetMapping("/physicalQuery")
public AjaxResult physicalQuery(@RequestParam("expressNo") String expressNo) {
    return physicalSearchService.search(expressNo);
}
posted @ 2022-04-15 22:32  CoderTL  阅读(265)  评论(0编辑  收藏  举报