java对接圆通快递的api接口

一,先去圆通开发平台注册开发权限:

开放平台 (yto.net.cn)

 二,接下来就是代码展示:

1.写了3个接口:(创建订单,取消订单,物流轨迹查询)

测试代码展示:

package com.example.yuantong;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.yuantong.entity.*;
import com.example.yuantong.util.encryptSignForOpen;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

@SpringBootTest
class YuantongApplicationTests {
//    @Resource
//    private YuantongConfig yuantongConfig;

    @Test
    void contextLoads() {
        String jsonStr = "{\"timestamp\": \"1662542701425\",\"param\": \"{\\\"logisticsNo\\\":\\\"o362rAIY6DO\\\",\\\"senderName\\\":\\\"测试1\\\",\\\"senderProvinceName\\\":\\\"上海\\\",\\\"senderCityName\\\":\\\"上海市\\\",\\\"senderCountyName\\\":\\\"青浦区\\\",\\\"senderAddress\\\":\\\"汇金路100号\\\",\\\"senderMobile\\\":\\\"15900521555\\\",\\\"recipientName\\\":\\\"测试\\\",\\\"recipientProvinceName\\\":\\\"重庆\\\",\\\"recipientCityName\\\":\\\"重庆市\\\",\\\"recipientCountyName\\\":\\\"万州区\\\",\\\"recipientAddress\\\":\\\"汇金路100好\\\",\\\"recipientMobile\\\":\\\"021-59815121\\\",\\\"remark\\\":\\\"remark-test\\\",\\\"gotCode\\\":\\\"123\\\",\\\"increments\\\":[{\\\"type\\\":4,\\\"amount\\\":888}],\\\"goods\\\":[{\\\"name\\\":\\\"mobile\\\",\\\"weight\\\":5,\\\"length\\\":10,\\\"width\\\":20,\\\"height\\\":5,\\\"price\\\":100,\\\"quantity\\\":1},{\\\"name\\\":\\\"mobile1\\\",\\\"weight\\\":1,\\\"length\\\":1,\\\"width\\\":1,\\\"height\\\":1,\\\"price\\\":1,\\\"quantity\\\":1}],\\\"startTime\\\":\\\"2022-09-07 17:25:01\\\",\\\"endTime\\\":\\\"2022-09-07 17:25:01\\\",\\\"cstOrderNo\\\":\\\"csorderno\\\",\\\"weight\\\":5,\\\"productCode\\\":\\\"PK\\\"}\",\"sign\": \"Z5n18P5FxnlQ1rTegtirkw==\",\"format\": \"JSON\"}";

        JSONObject jsonObj = JSONObject.parseObject(jsonStr);
        String timestamp = jsonObj.getString("timestamp");
        String param = jsonObj.getString("param");
        String sign = jsonObj.getString("sign");
        String format = jsonObj.getString("format");

        System.out.println("timestamp: " + timestamp);
        System.out.println("param: " + param);
        System.out.println("sign: " + sign);
        System.out.println("format: " + format);
    }
//    @Resource
//    private RestTemplate restTemplate;
    /**
     * 订单创建接口
     * */
    @Test
    void contextLoads2() {

        //参数设置
        YTOOrder ytoOrder = new YTOOrder();
        ytoOrder.setSenderName("张三");
        ytoOrder.setSenderProvinceName("江苏省");
        ytoOrder.setSenderCityName("苏州市");
        ytoOrder.setSenderCountyName("工业园区");
        ytoOrder.setSenderAddress("金鸡湖大道88号");
        ytoOrder.setSenderMobile("15900521555");

        ytoOrder.setRecipientName("李四");
        ytoOrder.setRecipientProvinceName("江苏省");
        ytoOrder.setRecipientCityName("徐州市");
        ytoOrder.setRecipientCountyName("泉山区");
        ytoOrder.setRecipientAddress("湖中路");
        ytoOrder.setRecipientMobile("021-5985121");
        ytoOrder.setRemark("测试");

        List<OrderIncrementDto> increment = new ArrayList<>();
        OrderIncrementDto orderIncrementDto = new OrderIncrementDto();
        orderIncrementDto.setType(4);
        orderIncrementDto.setAmount(new BigDecimal(888));
        increment.add(orderIncrementDto);
        ytoOrder.setIncrements(increment);
        List<OrderGoodsDto> goods = new ArrayList<>();
        OrderGoodsDto goodsDto = new OrderGoodsDto();
        goodsDto.setName("mobile");
        goodsDto.setWeight(new BigDecimal(5));
        goodsDto.setLength(new BigDecimal(10));
        goodsDto.setWidth(new BigDecimal(20));
        goodsDto.setHeight(new BigDecimal(5));
        goodsDto.setPrice(new BigDecimal(100));
        goodsDto.setQuantity(1);
        goods.add(goodsDto);
        ytoOrder.setGoods(goods);
//生成7位随机物流单号
        //ytoOrder.setLogisticsNo(S.newRandomNum(7));
        ytoOrder.setLogisticsNo(5214965);
        Properties PropertiesUtils = new Properties();

        String jsonString = JSON.toJSONString(ytoOrder);

        System.out.println("jsonString=="+jsonString);
        String data = jsonString + YuantongConfig.YTO_Method + YuantongConfig.YTO_Edition;
//签名
        String sign = encryptSignForOpen.encryptSignForOpen(data,YuantongConfig.YTO_Secret);
//封装参数
        Map<String, String> map = new HashMap<>();
//获取时间戳
        map.put("timestamp", String.valueOf(System.currentTimeMillis()));
        map.put("param", jsonString);
        map.put("sign", sign);
        map.put("format", "JSON");

        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.parseMediaType("application/json; charset=UTF-8"));
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity(map, headers);
        ResponseEntity<Map> response = restTemplate.postForEntity(YuantongConfig.YTO_Aip, request, Map.class, new Object[0]);
        Map order = response.getBody();
        System.out.println("order");
        System.out.println(order);
//        return order;

    }
    /**
     * 订单取消接口
     * */
    @Test
    void contextLoads3(){
      //参数设置
        YTOOCancel ytooCancel=new  YTOOCancel();
        ytooCancel.setLogisticsNo("5214965");//物流单号
        ytooCancel.setCancelDesc("不想要");//取消原因
        //JSON
        String jsonString = JSON.toJSONString(ytooCancel);
        System.out.println("jsonString=="+jsonString);
        String data = jsonString + YuantongConfig.YTO_Method_Api + YuantongConfig.YTO_Edition;
        //签名
        String sign = encryptSignForOpen.encryptSignForOpen(data,YuantongConfig.YTO_Secret);
        //封装参数
        Map<String, String> map = new HashMap<>();
        //获取时间戳
        map.put("timestamp", String.valueOf(System.currentTimeMillis()));
        map.put("param", jsonString);
        map.put("sign", sign);
        map.put("format", "JSON");
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.parseMediaType("application/json; charset=UTF-8"));
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity(map, headers);
        ResponseEntity<Map> response = restTemplate.postForEntity(YuantongConfig.YTO_Cancel_Api, request, Map.class, new Object[0]);
        Map orderCancel = response.getBody();
        System.out.println("orderCancel="+orderCancel);

    }
    /**
     *物流轨迹查询接口
     * */
    @Test
    void contextLoads4(){
        //参数设置
        YTOOCancel ytooCancel=new  YTOOCancel();
        ytooCancel.setNumber("YT2819001489443");//圆通物流运单号
        //JSON
        String jsonString = JSON.toJSONString(ytooCancel);
        System.out.println("jsonString=="+jsonString);
        String data = jsonString +YuantongConfig.YTO_Logistics_Method  + YuantongConfig.YTO_Edition;
        //签名
        String sign = encryptSignForOpen.encryptSignForOpen(data,YuantongConfig.YTO_Logistics_Secret);
        //封装参数
        Map<String, String> map = new HashMap<>();
        //获取时间戳
        map.put("timestamp", String.valueOf(System.currentTimeMillis()));
        map.put("param", jsonString);
        map.put("sign", sign);
        map.put("format", "JSON");
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.parseMediaType("application/json; charset=UTF-8"));
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity(map, headers);
        ResponseEntity<Map> response = restTemplate.postForEntity(YuantongConfig.YTO_Logistics_Api, request, Map.class, new Object[0]);
        Map orderCancel = response.getBody();
        System.out.println("orderCancel="+orderCancel);

    }
}

效果图:

 其它部分详细代码请联系(门主:QQ群:7694505522)

感谢大家的支持!!!

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT空门:门主

你的鼓励是我发稿的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值