**
**springboot集成京东物流查询快递,运费,标价运费
**
上篇是对接快递100的文章,接下来就是对接京东物流了,相信各位看官肯定也有对接京东物流的;接下来讲讲流程
第一个还是得注册那个相关的信息;
京东物流跟哪个其他平台不一样,分线上环境跟测试环境,这个时候需要申请哪个token是要两个的,一个是线上,另一个是测试;
写代码时是线上环境,注意别搞错了!
接下来开始正题
其实对接京东快递是比较简单的,相信大部分人可能无法下手;但是我准确的告诉你是最简单的了,别人提供了代码给你,你只要封装成一个工具类就可以了;
jdkuaidi.properties
jdkuaidi.access_token=**********************
jdkuaidi.appKey=**********************
jdkuaidi.appSecret=**********************
package com.lihua.utils;
import com.lihua.config.ApiCodeConstant;
import com.lihua.config.JdKuaidiConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.*;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.time.LocalDateTime;
import java.time.OffsetTime;
import java.time.format.DateTimeFormatter;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import static java.nio.charset.StandardCharsets.UTF_8;
/**
* 京东物流工具类
*
* @author 一只会飞的田园犬
*/
@Component
public class JdUtils {
@Autowired
private JdKuaidiConfig jdKuaidiConfig;
private static final String HEX_CHARACTERS = "0123456789ABCDEF";
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
public Map<String, Object> getResult(String path, String body) throws Exception {
String baseUri = "https://api.jdl.com";
String domain = "ECAP";
String method = "POST";
String algorithm = "md5-salt";
String appKey = jdKuaidiConfig.getAppKey();
String appSecret = jdKuaidiConfig.getAppSecret();
String accessToken = jdKuaidiConfig.getAccessToken();
String timestamp = DATE_TIME_FORMATTER.format(LocalDateTime.now());
String content = String.join("", new String[]{
appSecret,
"access_token", accessToken,
"app_key", appKey,
"method", path,
"param_json", body,
"timestamp", timestamp,
"v", "2.0",
appSecret});
String sign = sign(algorithm, content.getBytes(UTF_8), accessToken.getBytes(UTF_8));
String uri = baseUri + path;
Map<String, String> query = new HashMap<>(12);
query.put("LOP-DN", domain);
query.put("access_token", accessToken);
query.