org.apache.cxf
cxf-spring-boot-starter-jaxws
3.2.4
org.apache.cxf
cxf-rt-transports-http-jetty
3.2.4
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
@Component
public class WebserviceClient {
// 动态调用webservice接口
public String callWebSV(String wsdUrl, String operationName, String... params) throws Exception {
// 创建动态客户端
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient(wsdUrl);
// 需要密码的情况需要加上用户名和密码
//client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
Object[] objects;
// invoke("方法名",参数1,参数2,参数3....);
objects = client.invoke(operationName, params);
return objects[0].toString();
}
}
import com.ws.util.XMLFormatUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
@Slf4j
@Component
public class WSServiceImpl implements WSService {
@Autowired
WebserviceClient webserviceClient;
String webUrl = "http://xx.xx.xx.xx:xx/xx/services/MyService?wsdl";
/**
* http://xx.xx.xx.xx:xx/xx/services/test?testCode=&userName=xxxx&password=xxxx
*/
public List
参考:https://www.cnblogs.com/s180947190/p/9487899.html
1、依赖
dom4j
dom4j
1.6.1
2、工具类
import org.dom4j.DocumentHelper;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import java.io.ByteArrayOutputStream;
import java.util.*;
public class XMLFormatUtil {
/***
*XML格式字符串转换成MAP,不包括List
* @param xml
* @return
*/
public static Map xmlToMap(String xml) {
try {
Map map = new HashMap();
org.dom4j.Document document = DocumentHelper.parseText(xml);
org.dom4j.Element nodeElement = document.getRootElement();
List node = nodeElement.elements();
for (Iterator it = node.iterator(); it.hasNext(); ) {
org.dom4j.Element elm = (org.dom4j.Element) it.next();
map.put(elm.getName(), elm.getText());
elm = null;
}
node = null;
nodeElement = null;
document = null;
return map;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/***
* XML格式字符串转换成list
* @param xml
* @return
*/
public static List xmlToList(String xml) {
try {
List list = new ArrayList();
org.dom4j.Document document = DocumentHelper.parseText(xml);
org.dom4j.Element nodesElement = document.getRootElement();
List nodes = nodesElement.elements();
for (Iterator its = nodes.iterator(); its.hasNext(); ) {
org.dom4j.Element nodeElement = (org.dom4j.Element) its.next();
Map map = xmlToMap(nodeElement.asXML());
list.add(map);
map = null;
}
nodes = null;
nodesElement = null;
document = null;
return list;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/***
* XML格式字符串转换成map,包括List
* @param xml
* @return
*/
public static Map xmlToListMap(String xml) {
try {
Map reMap = new HashMap();
org.dom4j.Document document = DocumentHelper.parseText(xml);
org.dom4j.Element nodesElement = document.getRootElement();
List nodes = nodesElement.elements();
for (Iterator its = nodes.iterator(); its.hasNext(); ) {
org.dom4j.Element nodeElement = (org.dom4j.Element) its.next();
if (nodeElement.isTextOnly()) {
reMap.put(nodeElement.getName(), nodeElement.getText());
} else {
List reList = xmlToList(nodeElement.asXML());
reMap.put(nodeElement.getName(), reList);
reList = null;
}
}
nodes = null;
nodesElement = null;
document = null;
return reMap;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/***
* List转xml格式String
* @param list
* @param rootName
* @return
*/
public static String list2xml(List> list, String rootName) {
org.dom4j.Document document = org.dom4j.DocumentHelper.createDocument();
org.dom4j.Element nodesElement = document.addElement(rootName);
list2xml(list, nodesElement);
return doc2String(document);
}
/***
* list的element转xml格式String
* @param list
* @param element
* @return
*/
public static org.dom4j.Element list2xml(List list, org.dom4j.Element element) {
int i = 0;
for (Object o : list) {
org.dom4j.Element nodeElement = element.addElement("Map");
if (o instanceof Map) {
//nodeElement.addAttribute("type", "o");
Map m = (Map) o;
for (Iterator iterator = m.entrySet().iterator(); iterator.hasNext(); ) {
Map.Entry entry = (Map.Entry) iterator.next();
org.dom4j.Element keyElement = nodeElement.addElement(entry.getKey().toString());
if (entry.getValue() instanceof List) {
//keyElement.addAttribute("type", "l");
list2xml((List) entry.getValue(), keyElement);
} else {
//keyElement.addAttribute("type", "s");
keyElement.setText(entry.getValue().toString());
}
}
} else if (o instanceof List) {
//nodeElement.addAttribute("type", "l");
list2xml((List) o, nodeElement);
} else {
org.dom4j.Element keyElement = nodeElement.addElement("value");
keyElement.addAttribute("num", String.valueOf(i));
keyElement.setText(String.valueOf(o));
}
i++;
}
return element;
}
/***
* 文本转换String
* @param document
* @return
*/
public static String doc2String(org.dom4j.Document document) {
String s = "";
try {
// 使用输出流来进行转化
ByteArrayOutputStream out = new ByteArrayOutputStream();
// 使用UTF-8编码
OutputFormat format = new OutputFormat();
format.setSuppressDeclaration(true);
XMLWriter writer = new XMLWriter(out, format);
writer.write(document);
s = out.toString("UTF-8");
} catch (Exception ex) {
ex.printStackTrace();
}
return s;
}
}
参考:https://blog.csdn.net/m0_37499059/article/details/80505567