【SF顺丰】顺丰开放平台API对接(Java对接篇)

发布于:2025-05-07 ⋅ 阅读:(16) ⋅ 点赞:(0)

对接前置篇:

【SF顺丰】顺丰开放平台API对接(注册、API测试篇)_顺丰api接口对接指南-CSDN博客

1.实现效果展示

 2.SF顺丰开放平台,JDK资源下载。

     下载地址:顺丰开放平台

3.将下载的JDK放入项目中。

4.将JDK资源引入pom.xml文件中。

<!-- 顺丰丰桥 SDK -->
<dependency>
   <groupId>com.sf</groupId>
   <artifactId>SF-CSIM-EXPRESS-SDK</artifactId>
   <version>2.1.7</version>
   <scope>system</scope>
   <systemPath>${project.basedir}/lib/SF-CSIM-EXPRESS-SDK-V2.1.7.jar</systemPath>
</dependency>

 注:项目启动pom.xml文件中配置打包将外部SDK引入项目包中,否则发布后依然是失效。

<build>
   <plugins>
      <plugin>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-maven-plugin</artifactId>
         <configuration>
            <!-- 在打包时将引用的外部jar引入到当前项目包中	-->
            <fork>true</fork>
            <includeSystemScope>true</includeSystemScope>
         </configuration>
      </plugin>
   </plugins>
</build>

5.测试DEMO

接下来是一个详细的示例,演示如何使用顺丰丰桥API进行路由信息查询。

    顺丰丰桥Api参考文档:顺丰开放平台

public class SFTestDemo {

    private static final String CLIENT_CODE = "替换自己应用的顾客编码";  //顾客编码

    //沙箱环境的地址
    private static final String CALL_URL_BOX = "https://sfapi-sbox.sf-express.com/std/service";//请求接口
    private static final String CHECK_WORD_BOX = "替换自己应用的沙箱校验码";//沙箱校验码

    //生产环境的地址
    private static final String CALL_URL_PROD = "https://sfapi.sf-express.com/std/service";//请求接口
    private static final String CHECK_WORD = "替换自己的应用的生产校验码";//生产校验码


    /**
     * 查询顺丰路由信息
     * <p>
     * 顺丰快递单号
     * 收件人手机号后四位
     *
     * @return
     * @throws UnsupportedEncodingException
     */
    public static void main(String[] args) throws UnsupportedEncodingException {

        IServiceCodeStandard standardService = ExpressServiceCodeEnum.EXP_RECE_SEARCH_ROUTES;//路由查询

        String timeStamp = String.valueOf(System.currentTimeMillis());//时间戳

        CallExpressServiceTools tools = CallExpressServiceTools.getInstance();//数字签名,API文档有说明

        // set common header
        Map<String, String> params = new HashMap<String, String>();

        //封装查询msgData数据
        Map<String, Object> map = new HashMap<>();
        List<String> list = new ArrayList();
        list.add("替换自己顺丰单号");
        map.put("language", "0");
        map.put("trackingType", "1");
        map.put("methodType", "1");
        map.put("trackingNumber", list);
        map.put("checkPhoneNo", "替换快递单手机号后四位");
        String msgData = JSONObject.toJSONString(map);


//        params.put("Content-type", "application/x-www-form-urlencoded");
        params.put("partnerID", CLIENT_CODE);  // 顾客编码
        params.put("requestID", UUID.randomUUID().toString().replace("-", ""));
        params.put("serviceCode", standardService.getCode());// 接口服务码
        params.put("timestamp", timeStamp);
        params.put("msgData", msgData);
        params.put("msgDigest", tools.getMsgDigest(msgData, timeStamp, CHECK_WORD));

        String result = HttpClientUtil.post(CALL_URL_PROD, params);

        System.out.println("===调用地址 ===" + CALL_URL_PROD);
        System.out.println("===顾客编码 ===" + CLIENT_CODE);
        System.out.println("===返回结果:" + result);

    }
}


网站公告

今日签到

点亮在社区的每一天
去签到