对接鹰仓使用的Web Services api

参考:

HTTPClient作为客户端调用webservice推送soap_c# httpclient application/soap+xml-CSDN博客

Java发布webservice应用并发送SOAP请求调用 - 霞光里 - 博客园 (cnblogs.com)

Java调用WebService接口的四种方式-CSDN博客

Java 调用 WebService 、java调用Soap请求、Java对接soap接口_java soap-CSDN博客

Java创建Document对象有哪些方法呢?_java document撖寡情-CSDN博客

Java XML解析工具类-CSDN博客


基本的 Web services 平台是 XML + HTTP。

SOAP 指简易对象访问协议
SOAP 是一种通信协议
SOAP 用于应用程序之间的通信
SOAP 是一种用于发送消息的格式
SOAP 被设计用来通过因特网进行通信
SOAP 独立于平台
SOAP 独立于语言
SOAP 基于 XML
SOAP 很简单并可扩展
SOAP 允许您绕过防火墙
SOAP 将作为 W3C 标准来发展

客户端访问webservice

 /**
* HTTPClient 调用 WebService
*
* @param url
* @param soap
* @param SOAPAction
* @return
*/
public static String doPostSoap(String url, String soap, String SOAPAction) {
//请求体
String retStr = "";
// 创建HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// HttpClient
CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
HttpPost httpPost = new HttpPost(url);
try {
httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");
//看情况可以为空,也可以不设置
// httpPost.setHeader("SOAPAction", SOAPAction);
StringEntity data = new StringEntity(soap, Charset.forName("UTF-8"));
httpPost.setEntity(data);
CloseableHttpResponse response = closeableHttpClient.execute(httpPost);
HttpEntity httpEntity = response.getEntity();
if (httpEntity != null) {
// 打印响应内容
retStr = EntityUtils.toString(httpEntity, "UTF-8");
}
// 释放资源
closeableHttpClient.close();
} catch (Exception e) {
e.printStackTrace();
}
//这里还是返回的是xml字符串
System.out.println(retStr);
//将返回的xml转换成Document
Document document = strXmlToDocument(retStr);
//在Document中找到我们要找到那个 <response></response>里面的数据
retStr = getValueByElementName(document, "response");
//在将字符串json数据转成JsonObeject
System.out.println(retStr);
return retStr;
}

/**
* 将返回的数据转换成Document
* @param parseStrXml
* @return
*/
public static Document strXmlToDocument(String parseStrXml) {
StringReader read = new StringReader(parseStrXml);
//创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入
InputSource source = new InputSource(read);
//创建一个新的SAXBuilder
// 新建立构造器
SAXBuilder sb = new SAXBuilder();
Document doc = null;
try {
doc = sb.build(source);
} catch (JDOMException | IOException e) {
e.printStackTrace();
}
return doc;

}

/**
* 根据目标节点名获取值
*
* @param doc 文档结构
* @param finalNodeName 节点名
*/
public static String getValueByElementName(Document doc, String finalNodeName) {
Element root = doc.getRootElement();
HashMap<String, Object> map = new HashMap<String, Object>();
Map<String, Object> resultmap = getChildAllText(doc, root, map);
String result = (String) resultmap.get(finalNodeName);
return result;
}

/**
* 递归获得子节点的值
*
* @param doc 文档结构
* @param e 节点元素
* @param resultmap 递归将值存入map中
*/
public static Map<String, Object> getChildAllText(Document doc, Element e, HashMap<String, Object> resultmap) {
if (e != null) {
//如果存在子节点
if (e.getChildren() != null) {
List<Element> list = e.getChildren();
//循环输出
for (Element el : list) {
//如果子节点还存在子节点,则递归获取
if (el.getChildren().size() > 0) {
getChildAllText(doc, el, resultmap);
} else {
//将叶子节点值压入map
resultmap.put(el.getName(), el.getTextTrim());
}
}
}
}
return resultmap;
}




public static void main(String[] args) throws IOException {
//路径
String url = "http://yz.yunwms.com/default/svc/web-service";
//SOAPAction
String SOAPAction = "";
//SOAP数据
String soapXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"http://www.example.org/Ec/\">" +
" <SOAP-ENV:Body>" +
" <ns1:callService>" +
" <paramsJson>" +
" {" +
" \"pageSize\":10," +
" \"page\":9," +
" \"warehouse_code\":[\"EHL01\"]" +
" }" +
" </paramsJson>" +
"<appToken>xxx</appToken>" +
"<appKey>xxxxx</appKey>" +
" <service>getProductList</service>" +
" </ns1:callService>" +
" </SOAP-ENV:Body>" +
"</SOAP-ENV:Envelope>";
//调用方法
String response = doPostSoap(url,soapXML,SOAPAction);
JSONObject resultObject = JSONObject.parseObject(response);
System.out.println("---->" + resultObject);
}

xml字符串转成jsonObject的工具类

引入pom

<!--解析xml报文-->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1-beta-6</version>
</dependency>

代码工具类

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.*;
import java.util.*;

/**
* xml 解析
*
* @author star
* @date 2021/6/10
*/
public class XmlUtil {
/**
* XML节点转换JSON对象
*
* @param element 节点
* @param object 新的JSON存储
* @return JSON对象
*/
private static JSONObject xmlToJson(Element element, JSONObject object) {
List<Element> elements = element.elements();
for (Element child : elements) {
Object value = object.get(child.getName());
Object newValue;

if (child.elements().size() > 0) {
JSONObject jsonObject = xmlToJson(child, new JSONObject(true));
if (!jsonObject.isEmpty()) {
newValue = jsonObject;
} else {
newValue = child.getText();
}
} else {
newValue = child.getText();
}

List<Attribute> attributes = child.attributes();
if (!attributes.isEmpty()) {
JSONObject attrJsonObject = new JSONObject();
for (Attribute attribute : attributes) {
attrJsonObject.put(attribute.getName(), attribute.getText());
attrJsonObject.put("content", newValue);
}
newValue = attrJsonObject;
}

if (newValue != null) {
if (value != null) {
if (value instanceof JSONArray) {
((JSONArray) value).add(newValue);
} else {
JSONArray array = new JSONArray();
array.add(value);
array.add(newValue);
object.put(child.getName(), array);
}
} else {
object.put(child.getName(), newValue);
}
}
}
return object;
}

/**
* XML字符串转换JSON对象
*
* @param xmlStr XML字符串
* @return JSON对象
*/
public static JSONObject xmlToJson(String xmlStr) {
JSONObject result = new JSONObject(true);
SAXReader xmlReader = new SAXReader();
try {
Document document = xmlReader.read(new StringReader(xmlStr));
Element element = document.getRootElement();
return xmlToJson(element, result);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}

/**
* XML文件转换JSON对象
*
* @param file 文件路径
* @param node 选择节点
* @return JSON对象
*/
public static JSONObject xmlToJson(File file, String node) {
JSONObject result = new JSONObject(true);
SAXReader xmlReader = new SAXReader();
try {
Document document = xmlReader.read(file);
Element element;
if (StringUtils.isBlank(node)) {
element = document.getRootElement();
} else {
element = (Element) document.selectSingleNode(node);
}
return xmlToJson(element, result);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}

public static void main(String[] args) {
System.out.println(xmlToJson(new File("C:\\Users\\star\\Desktop\\PAD\\1 用户登录验证.xml"), "Body/Response"));
}

}

实战代码:

package com.xinghuo.service.api.eagle;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.amazonaws.util.IOUtils;
import com.xinghuo.framework.core.util.HttpUtil;
import com.xinghuo.framework.core.util.ResultData;
import com.xinghuo.service.basic.entity.OutboundDetail;
import com.xinghuo.service.basic.entity.OutboundMaster;
import com.xinghuo.service.basic.entity.ShippingPlanSplitDetail;
import com.xinghuo.service.basic.server.mapper.*;
import com.xinghuo.service.common.util.HttpRequestMethedEnum;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.xml.sax.InputSource;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.util.*;

@RestController
@RequestMapping("/eagel")
public class EagleApi {
@Value("${eagle.appToken}")
private String appToken;
@Value("${eagle.appKey}")
private String appKey;

@Autowired
private static ShippingPlanSplitDetailMapper shippingPlanSplitDetailMapper;
@Autowired
private OrderApiMapper orderApiMapper;
@Autowired
private OrderDetailApiMapper orderDetailApiMapper;
@Autowired
private ReturnApiMapper returnApiMapper;
@Autowired
private ReturnDetailApiMapper returnDetailApiMapper;
@Autowired
private CommonMapper commonMapper;

/**
* 登录OMS账户
*/
@RequestMapping("/logOn")
public ResultData logOn() throws IOException {
//第一步:创建服务地址
URL url = new URL("http://yz.yunwms.com/default/svc/web-service");
//第二步:打开一个通向服务地址的连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//第三步:设置参数
//3.1发送方式设置:POST必须大写
connection.setRequestMethod("POST");
//3.2设置数据格式:content-type
connection.setRequestProperty("content-type", "text/xml;charset=utf-8");
//3.3设置输入输出,因为默认新创建的connection没有读写权限,
connection.setDoInput(true);
connection.setDoOutput(true);

//第四步:组织SOAP数据,发送请求
String soapString = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" "
+ "xmlns:ns1=\"http://www.example.org/Ec/\">"
+ "<SOAP-ENV:Body>"
+ "<ns1:callService>"
+ "<paramsJson>"
+ "{\"user_account\":\"demo\"," +
" \"user_password\":\"123456\"}"
+ "</paramsJson>"
+ "<appToken>d2684ad701d2111628d418a57ea6d1d0</appToken>"
+ "<appKey>1b052c2bd8757e4547234d8bd0ee1d6d</appKey>"
+ "<service>logOn</service>"
+ "</ns1:callService>"
+ "</SOAP-ENV:Body>"
+ "</SOAP-ENV:Envelope>";
//将信息以流的方式发送出去
OutputStream os = connection.getOutputStream();
os.write(soapString.getBytes());
//第五步:接收服务端响应,打印
String result = "";
int responseCode = connection.getResponseCode();
if (200 == responseCode) {//表示服务端响应成功
//获取当前连接请求返回的数据流
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);

StringBuilder sb = new StringBuilder();
String temp = null;
while (null != (temp = br.readLine())) {
sb.append(temp);
}
result = sb.toString();
System.out.println(sb.toString());
Document document = strXmlToDocument(result);
String response = getValueByElementName(document, "response");
HashMap<String, String> hashMap = JSON.parseObject(response, HashMap.class);
System.out.println("---->" + hashMap.get("data"));
System.out.println(URLDecoder.decode(hashMap.get("data")));

is.close();
isr.close();
br.close();
}
os.close();
return ResultData.succeed(result);
}

/**
* 获取仓库
*/
@RequestMapping("/getWarehouse")
public ResultData getWarehouse() throws IOException {
//第一步:创建服务地址
URL url = new URL("http://yz.yunwms.com/default/svc/web-service"); //第二步:打开一个通向服务地址的连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//第三步:设置参数
//3.1发送方式设置:POST必须大写
connection.setRequestMethod("POST");
//3.2设置数据格式:content-type
connection.setRequestProperty("content-type", "text/xml;charset=utf-8");
//3.3设置输入输出,因为默认新创建的connection没有读写权限,
connection.setDoInput(true);
connection.setDoOutput(true);

//第四步:组织SOAP数据,发送请求
String soapXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"http://www.example.org/Ec/\">" +
"<SOAP-ENV:Body>" +
"<ns1:callService>" +
"<paramsJson>" +
"{" +
"}" +
"</paramsJson>" +
"<appToken>" + appToken + "</appToken>" +
"<appKey>" + appKey + "</appKey>" +
"<service>getWarehouse</service>" +
"</ns1:callService>" +
"</SOAP-ENV:Body>" +
"</SOAP-ENV:Envelope>";
//将信息以流的方式发送出去
OutputStream os = connection.getOutputStream();
os.write(soapXML.getBytes());
//第五步:接收服务端响应,打印
int responseCode = connection.getResponseCode();
Object data = null;
if (200 == responseCode) {//表示服务端响应成功
//获取当前连接请求返回的数据流
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);

StringBuilder sb = new StringBuilder();
String temp = null;
while (null != (temp = br.readLine())) {
sb.append(temp);
}

// System.out.println(sb.toString());
Document document = strXmlToDocument(sb.toString());
String response = getValueByElementName(document, "response");
data = JSON.parseObject(response, Map.class);
is.close();
isr.close();
br.close();
}
os.close();
return ResultData.succeed(data);
}

/**
* 获取sku
*/
@RequestMapping("/getSku")
public ResultData getSku() throws IOException {
//第一步:创建服务地址
URL url = new URL("http://yz.yunwms.com/default/svc/web-service"); //第二步:打开一个通向服务地址的连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//第三步:设置参数
//3.1发送方式设置:POST必须大写
connection.setRequestMethod("POST");
//3.2设置数据格式:content-type
connection.setRequestProperty("content-type", "text/xml;charset=utf-8");
//3.3设置输入输出,因为默认新创建的connection没有读写权限,
connection.setDoInput(true);
connection.setDoOutput(true);

//第四步:组织SOAP数据,发送请求
String soapXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"http://www.example.org/Ec/\">" +
" <SOAP-ENV:Body>" +
" <ns1:callService>" +
" <paramsJson>" +
" {" +
" \"pageSize\":10," +
" \"page\":9," +
" \"warehouse_code\":[\"EHL01\"]" +
" }" +
" </paramsJson>" +
" <appToken>" + appToken + "</appToken>\n" +
" <appKey>" + appKey + "</appKey>\n" +
" <service>getProductList</service>" +
" </ns1:callService>" +
" </SOAP-ENV:Body>" +
"</SOAP-ENV:Envelope>";
//将信息以流的方式发送出去
OutputStream os = connection.getOutputStream();
os.write(soapXML.getBytes());
//第五步:接收服务端响应,打印
int responseCode = connection.getResponseCode();
Object data = null;
if (200 == responseCode) {//表示服务端响应成功
//获取当前连接请求返回的数据流
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);

StringBuilder sb = new StringBuilder();
String temp = null;
while (null != (temp = br.readLine())) {
sb.append(temp);
}

System.out.println(sb.toString());
Document document = strXmlToDocument(sb.toString());
String response = getValueByElementName(document, "response");
data = JSON.parseObject(response, Map.class);
System.out.println("---->" + response);

is.close();
isr.close();
br.close();
}
os.close();
return ResultData.succeed(data);
}


/**
* 运输方式
*/
@RequestMapping("/getShippingMethod")
public ResultData getShippingMethod() {
String retStr = "";
String url = "http://yz.yunwms.com/default/svc/web-service";
//组织SOAP数据,发送请求
String soapXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"http://www.example.org/Ec/\">\n" +
" <SOAP-ENV:Body>\n" +
" <ns1:callService>\n" +
" <paramsJson>\n" +
" {\n" +
" \"warehouseCode\":\"EHL01\"\n" +
" }\n" +
" </paramsJson>\n" +
" <appToken>" + appToken + "</appToken>\n" +
" <appKey>" + appKey + "</appKey>\n" +
" <service>getShippingMethod</service>\n" +
" </ns1:callService>\n" +
" </SOAP-ENV:Body>\n" +
"</SOAP-ENV:Envelope>";
System.out.println(soapXML);
// 创建HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// HttpClient
CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
HttpPost httpPost = new HttpPost(url);
try {
httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");
//看情况可以为空,也可以不设置
// httpPost.setHeader("SOAPAction", SOAPAction);
StringEntity data = new StringEntity(soapXML, Charset.forName("UTF-8"));
httpPost.setEntity(data);
CloseableHttpResponse response = closeableHttpClient.execute(httpPost);
HttpEntity httpEntity = response.getEntity();
if (httpEntity != null) {
// 打印响应内容
retStr = EntityUtils.toString(httpEntity, "UTF-8");
}
// 释放资源
closeableHttpClient.close();
} catch (Exception e) {
e.printStackTrace();
}
//这里还是返回的是xml字符串
System.out.println(retStr);
//将返回的xml转换成Document
Document document = strXmlToDocument(retStr);
//在Document中找到我们要找到那个 <response></response>里面的数据
retStr = getValueByElementName(document, "response");
//在将字符串json数据转成JsonObeject
JSONObject jsonObject = JSON.parseObject(retStr);
System.out.println(jsonObject);
return ResultData.succeed(jsonObject);
}

/**
* 创建销售订单
*/
@RequestMapping("/getOrder")
public ResultData getOrder() {
String retStr = "";
String url = "http://yz.yunwms.com/default/svc/web-service";
//组织SOAP数据,发送请求
String soapXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"http://www.example.org/Ec/\">\n" +
" <SOAP-ENV:Body>\n" +
" <ns1:callService>\n" +
" <paramsJson>\n" +
" {\n" +
" \"pageSize\":1,\n" +
" \"page\":1,\n" +
" \"order_code\":\"51326-240219-0008\",\n" +
" \"order_status\":\"\",\n" +
" \"shipping_method\":\"\",\n" +
" \"order_code_arr\":[],\n" +
" \"create_date_from\":\"\",\n" +
" \"create_date_to\":\"\",\n" +
" \"modify_date_from\":\"\",\n" +
" \"modify_date_to\":\"\",\n" +
" \"ship_date_from\":\"\",\n" +
" \"ship_date_to\":\"\"\n" +
" }\n" +
" </paramsJson>\n" +
" <appToken>" + appToken + "</appToken>\n" +
" <appKey>" + appKey + "</appKey>\n" +
" <service>getOrderList</service>\n" +
" </ns1:callService>\n" +
" </SOAP-ENV:Body>\n" +
"</SOAP-ENV:Envelope>\n" +
" ";
System.out.println(soapXML);
// 创建HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// HttpClient
CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
HttpPost httpPost = new HttpPost(url);
try {
httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");
//看情况可以为空,也可以不设置
// httpPost.setHeader("SOAPAction", SOAPAction);
StringEntity data = new StringEntity(soapXML, Charset.forName("UTF-8"));
httpPost.setEntity(data);
CloseableHttpResponse response = closeableHttpClient.execute(httpPost);
HttpEntity httpEntity = response.getEntity();
if (httpEntity != null) {
// 打印响应内容
retStr = EntityUtils.toString(httpEntity, "UTF-8");
}
// 释放资源
closeableHttpClient.close();
} catch (Exception e) {
e.printStackTrace();
}
//这里还是返回的是xml字符串
System.out.println(retStr);
//将返回的xml转换成Document
Document document = strXmlToDocument(retStr);
//在Document中找到我们要找到那个 <response></response>里面的数据
retStr = getValueByElementName(document, "response");
//在将字符串json数据转成JsonObeject
JSONObject jsonObject = JSON.parseObject(retStr);
System.out.println(jsonObject);
return ResultData.succeed(jsonObject);
}

/**
* 创建WALMART销售订单
*/
@RequestMapping("/createWalmartOrder")
public ResultData createWalmartOrder(OutboundMaster outboundMaster) {
List<OutboundDetail> detailList = outboundMaster.getDetailList();
OutboundDetail outboundDetail = detailList.get(0);
if (null == outboundMaster.getShipToAddressTwo()) {
outboundMaster.setShipToAddressTwo("");
}
String retStr = "";
String url = "http://yz.yunwms.com/default/svc/web-service";
//组织SOAP数据,发送请求
String soapXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"http://www.example.org/Ec/\">\n" +
" <SOAP-ENV:Body>\n" +
" <ns1:callService>\n" +
" <paramsJson>\n" +
" {\n" +
" \"platform\":\"WALMART\",\n" +
" \"warehouse_code\":\"EHL01\",\n" +
" \"shipping_method\":\"UPS_3\",\n" +
" \"reference_no\":\"" + outboundMaster.getPo() + "\",\n" +
" \"order_desc\":\"\",\n" +
" \"order_business_type\":\"b2c\",\n" +
" \"country_code\":\"" + outboundMaster.getShipToCountry() + "\",\n" +
" \"province\":\"" + outboundMaster.getShipToState() + "\",\n" +
" \"city\":\"" + outboundMaster.getShipToCity() + "\",\n" +
" \"district\":\"\",\n" +
" \"address1\":\"" + outboundMaster.getShipToAddressOne() + "\",\n" +
" \"address2\":\"" + outboundMaster.getShipToAddressTwo() + "\",\n" +
" \"address3\":\"\",\n" +
" \"zipcode\":\"" + outboundMaster.getShipToZip() + "\",\n" +
" \"license\":\"\",\n" +
" \"doorplate\":\"\",\n" +
" \"company\":\"\",\n" +
" \"name\":\"" + outboundMaster.getShipToName() + "\",\n" +
" \"phone\":\"" + outboundMaster.getShipToPhone() + "\",\n" +
" \"cell_phone\":\"\",\n" +
" \"phone_extension\":\"\",\n" +
" \"email\":\"\",\n" +
" \"order_cod_currency\":\"\",\n" +
" \"assign_time\":\"02\",\n" +
" \"items\":[\n" +
" {\n" +
" \"product_sku\":\"" + outboundDetail.getSku() + "\",\n" +
" \"quantity\":" + outboundDetail.getPoQty() + "\n" +
" }\n" +
" ]\n" +
" }\n" +
" </paramsJson>\n" +
" <appToken>" + appToken + "</appToken>\n" +
" <appKey>" + appKey + "</appKey>\n" +
" <service>createOrder</service>\n" +
" </ns1:callService>\n" +
" </SOAP-ENV:Body>\n" +
"</SOAP-ENV:Envelope>";
System.out.println(soapXML);
// 创建HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// HttpClient
CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
HttpPost httpPost = new HttpPost(url);
try {
httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");
//看情况可以为空,也可以不设置
// httpPost.setHeader("SOAPAction", SOAPAction);
StringEntity data = new StringEntity(soapXML, Charset.forName("UTF-8"));
httpPost.setEntity(data);
CloseableHttpResponse response = closeableHttpClient.execute(httpPost);
HttpEntity httpEntity = response.getEntity();
if (httpEntity != null) {
// 打印响应内容
retStr = EntityUtils.toString(httpEntity, "UTF-8");
}
// 释放资源
closeableHttpClient.close();
} catch (Exception e) {
e.printStackTrace();
}
//这里还是返回的是xml字符串
System.out.println(retStr);
//将返回的xml转换成Document
Document document = strXmlToDocument(retStr);
//在Document中找到我们要找到那个 <response></response>里面的数据
retStr = getValueByElementName(document, "response");
//在将字符串json数据转成JsonObeject
JSONObject jsonObject = JSON.parseObject(retStr);
if (null == jsonObject) {
return ResultData.fail("发送到鹰仓的请求参数异常" + retStr);
} else {
Object error = jsonObject.get("Error");
if (null != error) {
return ResultData.fail(500, "发送到鹰仓的请求体没错,但是数据有误", error);
}
}
System.out.println(jsonObject);
return ResultData.succeed(jsonObject);
}

/**
* 创建WALMART销售订单
*/
@RequestMapping("/createWayfairOrder")
public ResultData createWayfairOrder(OutboundMaster outboundMaster) {
List<OutboundDetail> detailList = outboundMaster.getDetailList();
OutboundDetail outboundDetail = detailList.get(0);
if (null == outboundMaster.getShipToAddressTwo()) {
outboundMaster.setShipToAddressTwo("");
}
String retStr = "";
String url = "http://yz.yunwms.com/default/svc/web-service";
//组织SOAP数据,发送请求
String soapXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"http://www.example.org/Ec/\">\n" +
" <SOAP-ENV:Body>\n" +
" <ns1:callService>\n" +
" <paramsJson>\n" +
" {\n" +
" \"platform\":\"WAYFAIR\",\n" +
" \"warehouse_code\":\"EHL01\",\n" +
" \"shipping_method\":\"ZITI\",\n" +
" \"reference_no\":\"" + outboundMaster.getPo() + "\",\n" +
" \"order_desc\":\"\",\n" +
" \"order_business_type\":\"b2c\",\n" +
" \"country_code\":\"" + outboundMaster.getShipToCountry() + "\",\n" +
" \"province\":\"" + outboundMaster.getShipToState() + "\",\n" +
" \"city\":\"" + outboundMaster.getShipToCity() + "\",\n" +
" \"district\":\"\",\n" +
" \"address1\":\"" + outboundMaster.getShipToAddressOne() + "\",\n" +
" \"address2\":\"" + outboundMaster.getShipToAddressTwo() + "\",\n" +
" \"address3\":\"\",\n" +
" \"zipcode\":\"" + outboundMaster.getShipToZip() + "\",\n" +
" \"license\":\"\",\n" +
" \"doorplate\":\"\",\n" +
" \"company\":\"\",\n" +
" \"name\":\"" + outboundMaster.getShipToName() + "\",\n" +
" \"phone\":\"" + outboundMaster.getShipToPhone() + "\",\n" +
" \"cell_phone\":\"\",\n" +
" \"phone_extension\":\"\",\n" +
" \"email\":\"\",\n" +
" \"order_cod_currency\":\"\",\n" +
" \"items\":[\n" +
" {\n" +
" \"product_sku\":\"" + outboundDetail.getSku() + "\",\n" +
" \"quantity\":" + outboundDetail.getPoQty() + "\n" +
" }\n" +
" ],\n" +
// " \"tracking_no\":\"" + "tracking_no" + "\",\n" +
" \"label\":[\n" +
" {\n" +
" \"file_type\":\"pdf\",\n" +
" \"file_data\":\"\",\n" +
" \"file_size\":\"1680x945\",\n" +
" \"file_name\":\"shippingLabelCS514662380.pdf\" \n" +
" }\n" +
" ]\n" +
" }\n" +
" </paramsJson>\n" +
" <appToken>" + appToken + "</appToken>\n" +
" <appKey>" + appKey + "</appKey>\n" +
" <service>createOrder</service>\n" +
" </ns1:callService>\n" +
" </SOAP-ENV:Body>\n" +
"</SOAP-ENV:Envelope>";
System.out.println(soapXML);
// 创建HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// HttpClient
CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
HttpPost httpPost = new HttpPost(url);
try {
httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");
//看情况可以为空,也可以不设置
// httpPost.setHeader("SOAPAction", SOAPAction);
StringEntity data = new StringEntity(soapXML, Charset.forName("UTF-8"));
httpPost.setEntity(data);
CloseableHttpResponse response = closeableHttpClient.execute(httpPost);
HttpEntity httpEntity = response.getEntity();
if (httpEntity != null) {
// 打印响应内容
retStr = EntityUtils.toString(httpEntity, "UTF-8");
}
// 释放资源
closeableHttpClient.close();
} catch (Exception e) {
e.printStackTrace();
}
//这里还是返回的是xml字符串
System.out.println(retStr);
//将返回的xml转换成Document
Document document = strXmlToDocument(retStr);
//在Document中找到我们要找到那个 <response></response>里面的数据
retStr = getValueByElementName(document, "response");
//在将字符串json数据转成JsonObeject
JSONObject jsonObject = JSON.parseObject(retStr);
if (null == jsonObject) {
return ResultData.fail("发送到鹰仓的请求参数异常" + retStr);
} else {
Object error = jsonObject.get("Error");
if (null != error) {
return ResultData.fail(500, "发送到鹰仓的请求体没错,但是数据有误", error);
}
}
System.out.println(jsonObject);
return ResultData.succeed(jsonObject);
}

/**
* 获取getAsnList
*/
@RequestMapping("/getAsnList")
public ResultData getAsnList() throws IOException {
//第一步:创建服务地址
URL url = new URL("http://yz.yunwms.com/default/svc/web-service");
//第二步:打开一个通向服务地址的连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//第三步:设置参数
//3.1发送方式设置:POST必须大写
connection.setRequestMethod("POST");
//3.2设置数据格式:content-type
connection.setRequestProperty("content-type", "text/xml;charset=utf-8");
//3.3设置输入输出,因为默认新创建的connection没有读写权限,
connection.setDoInput(true);
connection.setDoOutput(true);

//第四步:组织SOAP数据,发送请求
String soapXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"http://www.example.org/Ec/\">" +
" <SOAP-ENV:Body>" +
" <ns1:callService>" +
" <paramsJson>" +
" {" +
" \"pageSize\":20," +
" \"page\":1," +
" \"receiving_code\":\"\"," +
" \"receiving_code_arr\":[]," +
"\"reference_no\":\"\"," +
" \"reference_no_arr\":[]," +
" \"create_date_from\":\"2018-01-08 10:00:00\"," +
" \"create_date_to\":\"2018-02-08 10:00:00\"," +
" \"modify_date_from\":\"2018-01-08 10:00:00\"," +
" \"modify_date_to\":\"2018-02-08 10:00:00\"," +
" \"business_type\":\"\"," +
" \"is_get_inventory_code\":0" +
" }" +
" </paramsJson>" +
" <appToken>" + appToken + "</appToken>\n" +
" <appKey>" + appKey + "</appKey>\n" +
" <service>getAsnList</service>" +
" </ns1:callService>" +
" </SOAP-ENV:Body>" +
"</SOAP-ENV:Envelope>";
//将信息以流的方式发送出去
OutputStream os = connection.getOutputStream();
os.write(soapXML.getBytes());
//第五步:接收服务端响应,打印
int responseCode = connection.getResponseCode();
Object data = null;
if (200 == responseCode) {//表示服务端响应成功
//获取当前连接请求返回的数据流
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);

StringBuilder sb = new StringBuilder();
String temp = null;
while (null != (temp = br.readLine())) {
sb.append(temp);
}

// System.out.println(sb.toString());
Document document = strXmlToDocument(sb.toString());
String response = getValueByElementName(document, "response");
data = JSON.parseObject(response, Map.class);
System.out.println("---->" + response);

is.close();
isr.close();
br.close();
}
os.close();
return ResultData.succeed(data);
}

/**
* 创建订单
*/
@RequestMapping("/createAsn")
public ResultData createAsn() throws IOException {
//第一步:创建服务地址
URL url = new URL("http://yz.yunwms.com/default/svc/web-service");
//第二步:打开一个通向服务地址的连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//第三步:设置参数
//3.1发送方式设置:POST必须大写
connection.setRequestMethod("POST");
//3.2设置数据格式:content-type
connection.setRequestProperty("content-type", "text/xml;charset=utf-8");
//3.3设置输入输出,因为默认新创建的connection没有读写权限,
connection.setDoInput(true);
connection.setDoOutput(true);
List<ShippingPlanSplitDetail> detailList = new ArrayList<>();
ShippingPlanSplitDetail shippingPlanSplitDetail = new ShippingPlanSplitDetail();
shippingPlanSplitDetail.setStatus("5");
shippingPlanSplitDetail.setSmallShippingPlanOrder("SMSP20231121-0002");
detailList = shippingPlanSplitDetailMapper.query(shippingPlanSplitDetail);
Integer boxNo = 1;
List<ShippingPlanSplitDetail> shippingPlanSplitDetailList = new ArrayList<>();
// for (ShippingPlanSplitDetail detail:detailList) {
// for (int i = 0; i < detail.getCtn(); i++) {
// ShippingPlanSplitDetail detail1 = new ShippingPlanSplitDetail();
// detail1.set
// }
// }
//第四步:组织SOAP数据,发送请求
String soapXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"http://www.example.org/Ec/\">" +
"<SOAP-ENV:Body>" +
"<ns1:callService>" +
"<paramsJson>" +
"{" +
"\"reference_no\":\"dfdfd1399866764\"," +
"\"warehouse_code\":\"EHL01\"," +
"\"items\":[" +
"{" +
"\"product_sku\":\"114A006\"," +
"\"quantity\":10," +
"\"box_no\":1," +
"\"product_price\":1.00," +
"\"currency_code\":\"RMB\"," +
" \"associated_barcode\":\"sdgv554561\"," +
"\"inventory_type\":\"1\"" +
"}," +
// " {" +
// "\"product_sku\":\"EA140509201610\"," +
// "\"quantity\":10," +
// "\"currency_code\":\"RMB\"," +
// "\"associated_barcode\":\"sdgv554562\"," +
// "\"inventory_type\":\"1\"" +
// "}," +
"{" +
"\"product_sku\":\"1135\"," +
"\"quantity\":10," +
"\"box_no\":2," +
" \"product_price\":1.00," +
"\"currency_code\":\"RMB\"," +
"\"associated_barcode\":\"sdgv554563\"," +
"\"inventory_type\":\"1\"" +
"}" +
"]" +
"}" +
"</paramsJson>" +
" <appToken>" + appToken + "</appToken>\n" +
" <appKey>" + appKey + "</appKey>\n" +
"<service>createAsn</service>" +
"</ns1:callService>" +
"</SOAP-ENV:Body>" +
"</SOAP-ENV:Envelope>" +
" ";
//将信息以流的方式发送出去
OutputStream os = connection.getOutputStream();
os.write(soapXML.getBytes());
//第五步:接收服务端响应,打印
int responseCode = connection.getResponseCode();
Object data = null;
if (200 == responseCode) {//表示服务端响应成功
//获取当前连接请求返回的数据流
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);

StringBuilder sb = new StringBuilder();
String temp = null;
while (null != (temp = br.readLine())) {
sb.append(temp);
}

// System.out.println(sb.toString());
Document document = strXmlToDocument(sb.toString());
String response = getValueByElementName(document, "response");
data = JSON.parseObject(response, Map.class);
System.out.println("---->" + response);

is.close();
isr.close();
br.close();
}
os.close();
return ResultData.succeed(data);
}

/**
* 修改modifyAsn
*/
@RequestMapping("/modifyAsn")
public ResultData modifyAsn() throws IOException {
//第一步:创建服务地址
URL url = new URL("http://yz.yunwms.com/default/svc/web-service");
//第二步:打开一个通向服务地址的连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//第三步:设置参数
//3.1发送方式设置:POST必须大写
connection.setRequestMethod("POST");
//3.2设置数据格式:content-type
connection.setRequestProperty("content-type", "text/xml;charset=utf-8");
//3.3设置输入输出,因为默认新创建的connection没有读写权限,
connection.setDoInput(true);
connection.setDoOutput(true);

//第四步:组织SOAP数据,发送请求
String soapXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"http://www.example.org/Ec/\">" +
" <SOAP-ENV:Body>" +
" <ns1:callService>" +
" <paramsJson>" +
" {" +
" \"receiving_code\":\"RVA001-200422-0001\"" +
" }" +
" </paramsJson>" +
" <appToken>" + appToken + "</appToken>\n" +
" <appKey>" + appKey + "</appKey>\n" +
" <service>cancelAsn</service>" +
" </ns1:callService>" +
" </SOAP-ENV:Body>" +
"</SOAP-ENV:Envelope>";
//将信息以流的方式发送出去
OutputStream os = connection.getOutputStream();
os.write(soapXML.getBytes());
//第五步:接收服务端响应,打印
int responseCode = connection.getResponseCode();
Object data = null;
if (200 == responseCode) {//表示服务端响应成功
//获取当前连接请求返回的数据流
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);

StringBuilder sb = new StringBuilder();
String temp = null;
while (null != (temp = br.readLine())) {
sb.append(temp);
}

// System.out.println(sb.toString());
Document document = strXmlToDocument(sb.toString());
String response = getValueByElementName(document, "response");
data = JSON.parseObject(response, Map.class);
System.out.println("---->" + response);

is.close();
isr.close();
br.close();
}
os.close();
return ResultData.succeed(data);
}

/**
* cancelAsn
*/
@RequestMapping("/cancelAsn")
public ResultData cancelAsn() throws IOException {
//第一步:创建服务地址
URL url = new URL("http://yz.yunwms.com/default/svc/web-service");
//第二步:打开一个通向服务地址的连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//第三步:设置参数
//3.1发送方式设置:POST必须大写
connection.setRequestMethod("POST");
//3.2设置数据格式:content-type
connection.setRequestProperty("content-type", "text/xml;charset=utf-8");
//3.3设置输入输出,因为默认新创建的connection没有读写权限,
connection.setDoInput(true);
connection.setDoOutput(true);

//第四步:组织SOAP数据,发送请求
String soapXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"http://www.example.org/Ec/\">" +
" <SOAP-ENV:Body>" +
" <ns1:callService>" +
" <paramsJson>" +
" {" +
" \"receiving_code\":\"RVA001-200422-0001\"" +
" }" +
" </paramsJson>" +
" <appToken>" + appToken + "</appToken>\n" +
" <appKey>" + appKey + "</appKey>\n" +
" <service>cancelAsn</service>" +
" </ns1:callService>" +
" </SOAP-ENV:Body>" +
"</SOAP-ENV:Envelope>";
//将信息以流的方式发送出去
OutputStream os = connection.getOutputStream();
os.write(soapXML.getBytes());
//第五步:接收服务端响应,打印
int responseCode = connection.getResponseCode();
Object data = null;
if (200 == responseCode) {//表示服务端响应成功
//获取当前连接请求返回的数据流
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);

StringBuilder sb = new StringBuilder();
String temp = null;
while (null != (temp = br.readLine())) {
sb.append(temp);
}

// System.out.println(sb.toString());
Document document = strXmlToDocument(sb.toString());
String response = getValueByElementName(document, "response");
data = JSON.parseObject(response, Map.class);
System.out.println("---->" + response);

is.close();
isr.close();
br.close();
}
os.close();
return ResultData.succeed(data);
}

public static void main(String[] args) throws IOException {
String ss="https://pic.imgdb.cn/item/656da10ec458853aef86d988.jpg";
byte[] bytes = ss.getBytes();
String s = Base64.getEncoder().encodeToString(bytes);
System.out.println(s);
}

/**
* 将返回的数据转换成Document
*
* @param parseStrXml
* @return
*/
public static Document strXmlToDocument(String parseStrXml) {
StringReader read = new StringReader(parseStrXml);
//创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入
InputSource source = new InputSource(read);
//创建一个新的SAXBuilder
// 新建立构造器
SAXBuilder sb = new SAXBuilder();
Document doc = null;
try {
doc = sb.build(source);
} catch (JDOMException | IOException e) {
e.printStackTrace();
}
return doc;

}

/**
* 根据目标节点名获取值
*
* @param doc 文档结构
* @param finalNodeName 节点名
*/
public static String getValueByElementName(Document doc, String finalNodeName) {
Element root = doc.getRootElement();
HashMap<String, Object> map = new HashMap<String, Object>();
Map<String, Object> resultmap = getChildAllText(doc, root, map);
String result = (String) resultmap.get(finalNodeName);
return result;
}

/**
* 递归获得子节点的值
*
* @param doc 文档结构
* @param e 节点元素
* @param resultmap 递归将值存入map中
*/
public static Map<String, Object> getChildAllText(Document doc, Element e, HashMap<String, Object> resultmap) {
if (e != null) {
//如果存在子节点
if (e.getChildren() != null) {
List<Element> list = e.getChildren();
//循环输出
for (Element el : list) {
//如果子节点还存在子节点,则递归获取
if (el.getChildren().size() > 0) {
getChildAllText(doc, el, resultmap);
} else {
//将叶子节点值压入map
resultmap.put(el.getName(), el.getTextTrim());
}
}
}
}
return resultmap;
}
}