大家好,欢迎来到IT知识分享网。
一,先去圆通开发平台注册开发权限:
开放平台 (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\": \"25\",\"param\": \"{\\\"logisticsNo\\\":\\\"o362rAIY6DO\\\",\\\"senderName\\\":\\\"测试1\\\",\\\"senderProvinceName\\\":\\\"上海\\\",\\\"senderCityName\\\":\\\"上海市\\\",\\\"senderCountyName\\\":\\\"青浦区\\\",\\\"senderAddress\\\":\\\"汇金路100号\\\",\\\"senderMobile\\\":\\\"\\\",\\\"recipientName\\\":\\\"测试\\\",\\\"recipientProvinceName\\\":\\\"重庆\\\",\\\"recipientCityName\\\":\\\"重庆市\\\",\\\"recipientCountyName\\\":\\\"万州区\\\",\\\"recipientAddress\\\":\\\"汇金路100好\\\",\\\"recipientMobile\\\":\\\"021-\\\",\\\"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(""); ytoOrder.setRecipientName("李四"); ytoOrder.setRecipientProvinceName("江苏省"); ytoOrder.setRecipientCityName("徐州市"); ytoOrder.setRecipientCountyName("泉山区"); ytoOrder.setRecipientAddress("湖中路"); ytoOrder.setRecipientMobile("021-"); 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(); 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("");//物流单号 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("YT43");//圆通物流运单号 //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); } }
效果图:
其它部分详细代码请联系(门主::)
感谢大家的支持!!!
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/100282.html

