第二篇:丰桥SDK之路由查询【顺丰查询订单的物流信息】

上一篇的文章链接:

https://blog.csdn.net/wildwolf_001/article/details/83410452

丰桥sdk路由查询的关键代码:

package com.test.demo.express.config;

import com.test.demo.express.service.SfExpressService;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: 
 * @Date: 2018/10/22 18:16
 * @Description:
 */
@Configuration
@EnableConfigurationProperties(SfExpressProperties.class)
public class SfExpressAutoConfiguration {
    private final SfExpressProperties expressProperties;

    public SfExpressAutoConfiguration(SfExpressProperties expressProperties) {
        this.expressProperties = expressProperties;
    }

    @Bean
    public SfExpressService expressService(){
        SfExpressService expressService = new SfExpressService();
        expressService.setExpressProperties(expressProperties);
        return expressService;
    }
}
package com.test.demo.express.service;

import com.test.demo.express.config.SfExpressProperties;
import com.test.demo.express.sf.dto.SfExpressResponseDTO;
import com.test.demo.express.sf.param.SfTracesParam;
import com.test.demo.express.sf.util.XmlUtil;
import com.test.demo.storage.util.HttpClientUtil;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.security.MessageDigest;

/**
 * @Author: 
 * @Date: 2018/10/22 18:20
 * @Description:
 */
public class SfExpressService {
    public static final Log logger = LogFactory.getLog(SfExpressService.class);

    private static final String RESULT_OK = "OK";

    private SfExpressProperties expressProperties;

    public SfExpressProperties getExpressProperties() {
        return expressProperties;
    }

    public void setExpressProperties(SfExpressProperties expressProperties) {
        this.expressProperties = expressProperties;
    }

    /**
     * 查询物流信息接口
     * @param params
     * @return
     */
    public SfExpressResponseDTO sfExpressMethod(SfTracesParam params){
        String wayBillNo = params.getWayBillNo();
        String orderSn = params.getOrderSn();

        // 获取订单物流路由
        String myReqXML = getRouteServiceRequestXml(wayBillNo);
        String resultStr= callSfExpressServiceByCSIM(expressProperties.getReqURL(), myReqXML, expressProperties.getClientCode(), expressProperties.getCheckWord());
        logger.info("返回的报文信息:" + resultStr);
        String head = subStringBetween(resultStr,"","").trim();
        boolean contains = resultStr.contains("");
        logger.info("返回的报文Head的状态:" + (head.equals("OK") && !contains ? "返回成功" : "返回失败"));
        // 报文显示成功
        if (RESULT_OK.equals(head) && !contains) {
            // 处理成功的报文,使用xml解析返回的xml报文为对象
            SfExpressResponseDTO responseDTO = XmlUtil.convertToObject(resultStr, SfExpressResponseDTO.class);
            responseDTO.setExpressName(expressProperties.getExpressName());
            responseDTO.setOrderSn(orderSn);
            responseDTO.setWayBillNo(wayBillNo);
            return responseDTO;
        }else {
            // 出错则打印出错误信息
            SfExpressResponseDTO responseDTO = new SfExpressResponseDTO();
            responseDTO.setHead(RESULT_OK);
            responseDTO.setExpressName(expressProperties.getExpressName());
            responseDTO.setOrderSn(orderSn);
            responseDTO.setWayBillNo(wayBillNo);
            return responseDTO;
        }
    }

    /**
     * 获取顺丰路由查询接口xml【请求体的xml文件】
     * @param wayBillNo
     * @return
     */
    private String getRouteServiceRequestXml(String wayBillNo){
        StringBuilder strBuilder = new StringBuilder();
        strBuilder.append("");
        strBuilder.append("" + expressProperties.getClientCode() + "");
        strBuilder.append("");
        strBuilder.append("");
        strBuilder.append("");
        strBuilder.append("");
        return strBuilder.toString();
    }

    /**
     * 取两个字符串之间的
     * @param str
     * @param strStart
     * @param strEnd
     * @return
     */
    private String subStringBetween(String str, String strStart, String strEnd) {
        /* 找出指定的2个字符在 该字符串里面的 位置 */
        int strStartIndex = str.indexOf(strStart);
        int strEndIndex = str.indexOf(strEnd);

        /* index 为负数 即表示该字符串中 没有该字符 */
        if (strStartIndex < 0) {
            return "字符串 :---->" + str + "<---- 中不存在 " + strStart + ", 无法截取目标字符串";
        }
        if (strEndIndex < 0) {
            return "字符串 :---->" + str + "<---- 中不存在 " + strEnd + ", 无法截取目标字符串";
        }
        /* 开始截取 */
        String result = str.substring(strStartIndex, strEndIndex).substring(strStart.length());
        return result;
    }

    private String callSfExpressServiceByCSIM(String reqURL, String reqXML, String clientCode, String checkword) {
        String result = null;
        String verifyCode = md5EncryptAndBase64(reqXML + checkword);
        result = querySFAPIservice(reqURL, reqXML, verifyCode);
        return result;
    }

    private String querySFAPIservice(String url, String xml, String verifyCode) {
        HttpClientUtil httpclient = new HttpClientUtil();
        if (url == null) {
            url = expressProperties.getReqURL();
        }

        String result = null;

        try {
            result = httpclient.postSFAPI(url, xml, verifyCode);
            return result;
        } catch (Exception var6) {
            logger.warn(" " + var6);
            return null;
        }
    }

    private String md5EncryptAndBase64(String str) {
        return encodeBase64(md5Encrypt(str));
    }

    private byte[] md5Encrypt(String encryptStr) {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(encryptStr.getBytes("utf8"));
            return md5.digest();
        } catch (Exception var2) {
            throw new RuntimeException(var2);
        }
    }

    private String encodeBase64(byte[] b) {
        String str = (new Base64()).encodeAsString(b);
        return str;
    }

}

xml文件解析的工具类下一篇文章的链接:

https://blog.csdn.net/wildwolf_001/article/details/83410452

上一篇对于对象及配置文件的封装链接:

https://blog.csdn.net/wildwolf_001/article/details/83411607

 

第二篇:丰桥SDK之路由查询【顺丰查询订单的物流信息】_第1张图片

 

你可能感兴趣的:(Express)