ps:

 闲着无聊,调用第三方接口玩玩,接口很简单。

附:快递物流查询API接口-支持1000多家快递公司-高实时-高稳定-高并发

将实现以下三个接口调用: 

项目结构:

请求参数:对象

package com.lucifer.express.dto;import lombok.Data;/*** @author: lucifer* @date: 2019/7/23* @description: 请求参数*/@Data
public class ExpressListReqBody {/* 名称  类型  是否必须    描述expName   STRING  可选  快递/物流公司名称,比如传入 顺丰maxSize LONG    可选  分页时,每页返回的最大记录数page  LONG    可选  分页的页数*/private String expName;private Long maxSize;private Long page;}
package com.lucifer.express.dto;import lombok.Data;/*** @author: lucifer* @date: 2019/7/23* @description: 快递物流节点跟踪 请求参数*/
@Data
public class ExpressInfoReqBody {/* 名称  类型  是否必须    描述com   STRING  必选  快递公司字母简称,可以从接口"快递公司查询" 中查到该信息 如不知道快递公司名,可以使用"auto"代替,此时将自动识别快递单号所属公司(成功率99%,因为一个单号规则可能会映射到多个快递公司。如果识别失败,系统将返回可能的快递公司列表)。不推荐大面积使用auto,建议尽量传入准确的公司编码。nu    STRING  必选  单号receiverPhone STRING  可选  收件人手机号后四位,顺丰需要填写(手机号后四位填一个就行,多填以寄件人为准)senderPhone STRING  可选  寄件人手机号后四位,顺丰需要填写(手机号后四位填一个就行,多填以寄件人为准)*/private String com;private String nu;private String receiverPhone;private String senderPhone;}

ExpressController:控制层 

package com.lucifer.express.controller;import com.alibaba.fastjson.JSONObject;
import com.lucifer.express.dto.ExpressInfoReqBody;
import com.lucifer.express.dto.ExpressListReqBody;
import com.lucifer.express.service.ExpressService;
import org.springframework.web.bind.annotation.*;import javax.annotation.Resource;
import java.io.IOException;/*** @author: lucifer* @date: 2019/7/23* @description: 控制层*/
@RestController
public class ExpressController {@Resourceprivate ExpressService expressService;/*** 快递公司查询** @param reqBody* @return*/@PostMapping(value = "getExpressList")public JSONObject getExpressList(@RequestBody ExpressListReqBody reqBody) {JSONObject jsonObject = null;try {jsonObject = expressService.getExpressList(reqBody);} catch (IOException e) {e.printStackTrace();}return jsonObject;}/*** 快递物流节点跟踪*/@PostMapping(value = "getExpressInfo")public JSONObject getExpressInfo(@RequestBody ExpressInfoReqBody reqBody) {JSONObject jsonObject = null;try {jsonObject = expressService.getExpressInfo(reqBody);} catch (Exception e) {e.printStackTrace();}return jsonObject;}/***单号查快递公司名*/@GetMapping(value = "fetchCom")public JSONObject fetchCom(@RequestParam(value = "nu",required = false) String nu) {JSONObject jsonObject = null;try {jsonObject = expressService.fetchCom(nu);} catch (Exception e) {e.printStackTrace();}return jsonObject;}}

业务层接口 ExpressService:

package com.lucifer.express.service;import com.alibaba.fastjson.JSONObject;
import com.lucifer.express.dto.ExpressInfoReqBody;
import com.lucifer.express.dto.ExpressListReqBody;import java.io.IOException;/*** @author: lucifer* @date: 2019/7/23* @description:*/
public interface ExpressService {/*** 快递公司查询** @param reqBody* @return*/JSONObject getExpressList(ExpressListReqBody reqBody) throws IOException;/*** 快递物流节点跟踪** @param reqBody* @return* @throws IOException*/JSONObject getExpressInfo(ExpressInfoReqBody reqBody) throws Exception;/*** 单号查快递公司名** @param nu* @return*/JSONObject fetchCom(String nu);
}

业务层接口 实现 ExpressServiceImpl:

package com.lucifer.express.service.impl;import com.alibaba.fastjson.JSONObject;
import com.lucifer.express.dto.ExpressInfoReqBody;
import com.lucifer.express.dto.ExpressListReqBody;
import com.lucifer.express.service.ExpressService;
import com.lucifer.express.utils.HttpUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;import java.util.HashMap;
import java.util.Map;/*** @author: lucifer* @date: 2019/7/23* @description:*/
@Service
public class ExpressServiceImpl implements ExpressService {@Value("${express.appcode}")private String appcode;@Overridepublic JSONObject getExpressList(ExpressListReqBody reqBody) {String host = "https://ali-deliver.showapi.com";String path = "/showapi_expressList";String method = "GET";Map<String, String> headers = new HashMap<>(16);//最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105headers.put("Authorization", "APPCODE " + appcode);Map<String, String> querys = new HashMap<>(16);querys.put("expName", reqBody.getExpName());querys.put("maxSize", String.valueOf(reqBody.getMaxSize()));querys.put("page", String.valueOf(reqBody.getPage()));return getJsonObject(host, path, method, headers, querys);}@Overridepublic JSONObject getExpressInfo(ExpressInfoReqBody reqBody) {String host = "https://ali-deliver.showapi.com";String path = "/showapi_expInfo";String method = "GET";Map<String, String> headers = new HashMap<>(16);//最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105headers.put("Authorization", "APPCODE " + appcode);Map<String, String> querys = new HashMap<>(16);querys.put("com", reqBody.getCom());querys.put("nu", reqBody.getNu());querys.put("receiverPhone", reqBody.getReceiverPhone());querys.put("senderPhone", reqBody.getSenderPhone());return getJsonObject(host, path, method, headers, querys);}@Overridepublic JSONObject fetchCom(String nu) {String host = "https://ali-deliver.showapi.com";String path = "/fetchCom";String method = "GET";Map<String, String> headers = new HashMap<>(16);//最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105headers.put("Authorization", "APPCODE " + appcode);Map<String, String> querys = new HashMap<>(16);querys.put("nu", nu);return getJsonObject(host, path, method, headers, querys);}/**** @param host* @param path* @param method* @param headers* @param querys* @return*/private JSONObject getJsonObject(String host, String path, String method, Map<String, String> headers, Map<String, String> querys) {HttpResponse response;String str = null;try {response = HttpUtils.doGet(host, path, method, headers, querys);str = EntityUtils.toString(response.getEntity());} catch (Exception e) {e.printStackTrace();}JSONObject jsonObject = JSONObject.parseObject(str);return jsonObject;}
}

application.yml:(appcode使用自己的,你买了人家的服务就会提供给你了,这里用的免费的就行,体验一下)

express:appcode: xxxxxxxx

HttpUtils:接口提供的工具类

package com.lucifer.express.utils;import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;/*** @author: lucifer* @date: 2019/7/23* @description:*/
public class HttpUtils {/*** get** @param host* @param path* @param method* @param headers* @param querys* @return* @throws Exception*/public static HttpResponse doGet(String host, String path, String method,Map<String, String> headers,Map<String, String> querys)throws Exception {HttpClient httpClient = wrapClient(host);HttpGet request = new HttpGet(buildUrl(host, path, querys));for (Map.Entry<String, String> e : headers.entrySet()) {request.addHeader(e.getKey(), e.getValue());}return httpClient.execute(request);}/*** post form** @param host* @param path* @param method* @param headers* @param querys* @param bodys* @return* @throws Exception*/public static HttpResponse doPost(String host, String path, String method,Map<String, String> headers,Map<String, String> querys,Map<String, String> bodys)throws Exception {HttpClient httpClient = wrapClient(host);HttpPost request = new HttpPost(buildUrl(host, path, querys));for (Map.Entry<String, String> e : headers.entrySet()) {request.addHeader(e.getKey(), e.getValue());}if (bodys != null) {List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();for (String key : bodys.keySet()) {nameValuePairList.add(new BasicNameValuePair(key, bodys.get(key)));}UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, "utf-8");formEntity.setContentType("application/x-www-form-urlencoded; charset=UTF-8");request.setEntity(formEntity);}return httpClient.execute(request);}/*** Post String** @param host* @param path* @param method* @param headers* @param querys* @param body* @return* @throws Exception*/public static HttpResponse doPost(String host, String path, String method,Map<String, String> headers,Map<String, String> querys,String body)throws Exception {HttpClient httpClient = wrapClient(host);HttpPost request = new HttpPost(buildUrl(host, path, querys));for (Map.Entry<String, String> e : headers.entrySet()) {request.addHeader(e.getKey(), e.getValue());}if (StringUtils.isNotBlank(body)) {request.setEntity(new StringEntity(body, "utf-8"));}return httpClient.execute(request);}/*** Post stream** @param host* @param path* @param method* @param headers* @param querys* @param body* @return* @throws Exception*/public static HttpResponse doPost(String host, String path, String method,Map<String, String> headers,Map<String, String> querys,byte[] body)throws Exception {HttpClient httpClient = wrapClient(host);HttpPost request = new HttpPost(buildUrl(host, path, querys));for (Map.Entry<String, String> e : headers.entrySet()) {request.addHeader(e.getKey(), e.getValue());}if (body != null) {request.setEntity(new ByteArrayEntity(body));}return httpClient.execute(request);}/*** Put String* @param host* @param path* @param method* @param headers* @param querys* @param body* @return* @throws Exception*/public static HttpResponse doPut(String host, String path, String method,Map<String, String> headers,Map<String, String> querys,String body)throws Exception {HttpClient httpClient = wrapClient(host);HttpPut request = new HttpPut(buildUrl(host, path, querys));for (Map.Entry<String, String> e : headers.entrySet()) {request.addHeader(e.getKey(), e.getValue());}if (StringUtils.isNotBlank(body)) {request.setEntity(new StringEntity(body, "utf-8"));}return httpClient.execute(request);}/*** Put stream* @param host* @param path* @param method* @param headers* @param querys* @param body* @return* @throws Exception*/public static HttpResponse doPut(String host, String path, String method,Map<String, String> headers,Map<String, String> querys,byte[] body)throws Exception {HttpClient httpClient = wrapClient(host);HttpPut request = new HttpPut(buildUrl(host, path, querys));for (Map.Entry<String, String> e : headers.entrySet()) {request.addHeader(e.getKey(), e.getValue());}if (body != null) {request.setEntity(new ByteArrayEntity(body));}return httpClient.execute(request);}/*** Delete** @param host* @param path* @param method* @param headers* @param querys* @return* @throws Exception*/public static HttpResponse doDelete(String host, String path, String method,Map<String, String> headers,Map<String, String> querys)throws Exception {HttpClient httpClient = wrapClient(host);HttpDelete request = new HttpDelete(buildUrl(host, path, querys));for (Map.Entry<String, String> e : headers.entrySet()) {request.addHeader(e.getKey(), e.getValue());}return httpClient.execute(request);}private static String buildUrl(String host, String path, Map<String, String> querys) throws UnsupportedEncodingException {StringBuilder sbUrl = new StringBuilder();sbUrl.append(host);if (!StringUtils.isBlank(path)) {sbUrl.append(path);}if (null != querys) {StringBuilder sbQuery = new StringBuilder();for (Map.Entry<String, String> query : querys.entrySet()) {if (0 < sbQuery.length()) {sbQuery.append("&");}if (StringUtils.isBlank(query.getKey()) && !StringUtils.isBlank(query.getValue())) {sbQuery.append(query.getValue());}if (!StringUtils.isBlank(query.getKey())) {sbQuery.append(query.getKey());if (!StringUtils.isBlank(query.getValue())) {sbQuery.append("=");sbQuery.append(URLEncoder.encode(query.getValue(), "utf-8"));}}}if (0 < sbQuery.length()) {sbUrl.append("?").append(sbQuery);}}return sbUrl.toString();}private static HttpClient wrapClient(String host) {HttpClient httpClient = new DefaultHttpClient();if (host.startsWith("https://")) {sslClient(httpClient);}return httpClient;}private static void sslClient(HttpClient httpClient) {try {SSLContext ctx = SSLContext.getInstance("TLS");X509TrustManager tm = new X509TrustManager() {@Overridepublic X509Certificate[] getAcceptedIssuers() {return null;}@Overridepublic void checkClientTrusted(X509Certificate[] xcs, String str) {}@Overridepublic void checkServerTrusted(X509Certificate[] xcs, String str) {}};ctx.init(null, new TrustManager[] { tm }, null);SSLSocketFactory ssf = new SSLSocketFactory(ctx);ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);ClientConnectionManager ccm = httpClient.getConnectionManager();SchemeRegistry registry = ccm.getSchemeRegistry();registry.register(new Scheme("https", 443, ssf));} catch (KeyManagementException ex) {throw new RuntimeException(ex);} catch (NoSuchAlgorithmException ex) {throw new RuntimeException(ex);}}}

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.6.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.lucifer</groupId><artifactId>express</artifactId><version>0.0.1-SNAPSHOT</version><name>express</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.15</version></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.2.1</version></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpcore</artifactId><version>4.2.1</version></dependency><dependency><groupId>commons-lang</groupId><artifactId>commons-lang</artifactId><version>2.6</version></dependency><dependency><groupId>org.eclipse.jetty</groupId><artifactId>jetty-util</artifactId><version>9.3.7.v20160115</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

请求参数、返回信息 示例:

(示例一)物流公司查询

(示例二)快递物流节点跟踪

com:这个参数是快递公司字母简称,如果不填可以选择填auto代替;如:中通简称zhongtong,那么这个参数可以用zhongtong;

注:806649404001521954这个单号是网上找的圆通单号;

(示例三)单号查快递公司名 

SpringBoot用HttpClient调用快递物流查询API接口相关推荐

  1. 国内主流快递物流查询API接口测评对比

    • 快递物流查询API接口 快递查询接口是指快递查询网对外开放的应用程序接口,开发人员能够通过调用该接口与快递查询网进行交互,并基于该接口开发自己的快递查询应用程序. • 应用场景 ① 买家物流查询: ...

  2. 国内主流常用快递物流查询api接口介绍以及demo分享

    快递查询接口API 快递查询接口是指快递查询网对外开放的应用程序接口,开发人员能够通过调用该接口与快递查询网进行交互,并基于该接口开发自己的快递查询应用程序.目前比较常用的第三方接口有快递鸟.一次性可 ...

  3. APISpace 全球快递物流查询API接口 免费好用

    前言 随着我国电子商务的迅猛发展,物流行业也开始突飞猛进,人们的日常生活越来越离不开快递服务,查快递.寄快递的需求越来越大,随之而来,常用快递接口的需求也越来越大. 全国快递查询接口,支持各大快递公司 ...

  4. 阿里云全国快递物流查询api接口

    口地址: https://market.aliyun.com/products/56928004/cmapi021863.html?spm=5176.730005.productlist.d_cmap ...

  5. 免费常用的快递物流查询api接口介绍

    快递查询接口API 快递查询接口是指快递查询网对外开放的应用程序接口,开发人员能够通过调用该接口与快递查询网进行交互,并基于该接口开发自己的快递查询应用程序.目前比较常用的第三方接口有快递鸟. 应用场 ...

  6. 从申请到调用:全国快递物流查询 API 使用教程

    引言 面对越来越多的快递需求和快递公司的日益增多,手动查询快递状态的工作变得愈发繁琐.此时,一个全国快递物流查询 API 的出现能够极大地提高查询的效率和准确性,解决人工查询的问题,为用户提供更加便捷 ...

  7. 快递鸟查询Api接口使用(PHP版)

    前提 项目开发中,有些需求难免会用到关于快递的一些Api接口:本篇主要介绍的是快递的查询Api及与其他各家的不同之处: 常用的提供快递Api接口的有: 快递鸟 快递100 爱查快递  等等 如大家使用 ...

  8. APISpace 跨境国际快递物流查询API

    在这里给大家推荐一个快递物流类的API--APISpace 的 跨境国际快递物流查询,支持900+物流商,提供实时查询和单号订阅API接口.稳定高效,为跨境电商平台.独立站.软件服务商提供优质服务. ...

  9. 如何用PHP对接调用快递鸟物流信息api接口

    博主最近需要做一个物流信息轨迹查询的api接口,就去网上搜索,看到了一个快递鸟的API接口,返回值是以JSON格式,只需要返回是转成数组就能轻松实现各种实例了.真的很方便. 对接流程 快递鸟网站申请接 ...

  10. 免费快递单号查询api接口对接调用demo地址

    应用场景: 最常见的应用场景如下: (1)电商网站:例如B2C.团购.B2B.批发分销站.C2C.本地生活交易等网站. (2)管理系统:订单处理平台.订货平台.发货平台.分销系统.渠道管理系统.客户管 ...

最新文章

  1. window环境Visual Studio配置:OpenCV,Eigen,jsoncpp
  2. ORA-00904: 标识符无效——解决方案
  3. 端口偷窃(Port Stealing)技术
  4. P2689 东南西北
  5. php博客添加live2d,在博客中增加自己的live2d纸片人模型方法
  6. 如何将自定义数据源集成到Apache Spark中
  7. 前端学习(1551):补充cloak
  8. grid赋予oracle磁盘权限,grid 与 Oracle 用户下 Oracle 程序权限不一致导致无法连接 ASM 问题...
  9. 同济大学计算机直博生条件,同济大学攻读博士学位研究生培养工作规定(2016年修订).doc...
  10. 混淆的概念:SIF、CIF、4CIF、D1
  11. 智能音箱---TAS5754M 音频DSP 到Android
  12. 易康(eCognition)对象几何特征--2:几何(Geometry)_ 形状(Shape)
  13. cad放大_左手快捷键,右手鼠标,这就是CAD!
  14. 第一部分 数理逻辑 第三章 命题逻辑的推理理论
  15. 思科交换机配置试题_cisco交换机配置简单教程.doc
  16. 极路由KMS_Activator插件使用教程
  17. C语言:爱因斯坦的数学题
  18. welearn考试切屏会有显示吗_welearn视听说教程4答案截图
  19. .NET Core发送HTTP Post和Get
  20. linux sata硬盘热交换,简单的热交换方法,用于WD西部数据硬盘的SA区服务区域访问问题...

热门文章

  1. win10安装影子系统,导致电脑无限蓝屏,解决总结
  2. 【统计学】三大相关系数之斯皮尔曼相关系数(spearman correlation coefficient)
  3. 电子书格式问题的本质
  4. 工业机器人编程语言c语言,工业机器人编程语言和编程方式
  5. 几款对于学习前端比较好用的软件或网址
  6. 在做模具设计过程中应注意哪些问题
  7. 视频教程-三天掌握三菱FX系列PLC视频教程-单片机/工控
  8. stm8s103k3 周期 捕获_基于stm8s103k3单片机串口UART的正确使用分享
  9. 基础IT必备知识(一)
  10. Android三大动画介绍及使用