闲着无聊,调用第三方接口玩玩,接口很简单。
附:快递物流查询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;
}
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 {
@Resource
private 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;
}
}
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);
}
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;
@Override
public JSONObject getExpressList(ExpressListReqBody reqBody) {
String host = "https://ali-deliver.showapi.com";
String path = "/showapi_expressList";
String method = "GET";
Map headers = new HashMap<>(16);
//最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
headers.put("Authorization", "APPCODE " + appcode);
Map 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);
}
@Override
public JSONObject getExpressInfo(ExpressInfoReqBody reqBody) {
String host = "https://ali-deliver.showapi.com";
String path = "/showapi_expInfo";
String method = "GET";
Map headers = new HashMap<>(16);
//最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
headers.put("Authorization", "APPCODE " + appcode);
Map 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);
}
@Override
public JSONObject fetchCom(String nu) {
String host = "https://ali-deliver.showapi.com";
String path = "/fetchCom";
String method = "GET";
Map headers = new HashMap<>(16);
//最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
headers.put("Authorization", "APPCODE " + appcode);
Map 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 headers, Map 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;
}
}
express:
appcode: xxxxxxxx
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 headers,
Map querys)
throws Exception {
HttpClient httpClient = wrapClient(host);
HttpGet request = new HttpGet(buildUrl(host, path, querys));
for (Map.Entry 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 headers,
Map querys,
Map bodys)
throws Exception {
HttpClient httpClient = wrapClient(host);
HttpPost request = new HttpPost(buildUrl(host, path, querys));
for (Map.Entry e : headers.entrySet()) {
request.addHeader(e.getKey(), e.getValue());
}
if (bodys != null) {
List nameValuePairList = new ArrayList();
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 headers,
Map querys,
String body)
throws Exception {
HttpClient httpClient = wrapClient(host);
HttpPost request = new HttpPost(buildUrl(host, path, querys));
for (Map.Entry 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 headers,
Map querys,
byte[] body)
throws Exception {
HttpClient httpClient = wrapClient(host);
HttpPost request = new HttpPost(buildUrl(host, path, querys));
for (Map.Entry 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 headers,
Map querys,
String body)
throws Exception {
HttpClient httpClient = wrapClient(host);
HttpPut request = new HttpPut(buildUrl(host, path, querys));
for (Map.Entry 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 headers,
Map querys,
byte[] body)
throws Exception {
HttpClient httpClient = wrapClient(host);
HttpPut request = new HttpPut(buildUrl(host, path, querys));
for (Map.Entry 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 headers,
Map querys)
throws Exception {
HttpClient httpClient = wrapClient(host);
HttpDelete request = new HttpDelete(buildUrl(host, path, querys));
for (Map.Entry e : headers.entrySet()) {
request.addHeader(e.getKey(), e.getValue());
}
return httpClient.execute(request);
}
private static String buildUrl(String host, String path, Map 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 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() {
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkClientTrusted(X509Certificate[] xcs, String str) {
}
@Override
public 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);
}
}
}
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.6.RELEASE
com.lucifer
express
0.0.1-SNAPSHOT
express
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-web
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
com.alibaba
fastjson
1.2.15
org.apache.httpcomponents
httpclient
4.2.1
org.apache.httpcomponents
httpcore
4.2.1
commons-lang
commons-lang
2.6
org.eclipse.jetty
jetty-util
9.3.7.v20160115
org.springframework.boot
spring-boot-maven-plugin
(示例一)物流公司查询
(示例二)快递物流节点跟踪
com:这个参数是快递公司字母简称,如果不填可以选择填auto代替;如:中通简称zhongtong,那么这个参数可以用zhongtong;
注:806649404001521954这个单号是网上找的圆通单号;
(示例三)单号查快递公司名