package com.example.demo;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.XML;
import com.google.common.collect.Lists;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.axis.message.SOAPHeaderElement;
import org.apache.axis.types.Schema;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.text.StringEscapeUtils;
/**
* @author 900004
* @date 2023/10/24 17:28
*/
public class WebCall {
public static JSONObject webService(String url, String namespace, String methodName, Map<String, Object> params) throws Exception {
String decodedValue = StringEscapeUtils.unescapeHtml4(call(url, namespace, methodName, params));
return convertXmlIntoJSONObject(decodedValue);
}
public static JSONObject convertXmlIntoJSONObject(String response) {
JSONObject jsonObject = XML.toJSONObject(response);
Map<String, String> keyMap = new HashMap<>();
processKeys(jsonObject, keyMap);
return analyseJson(jsonObject, keyMap);
}
public static String call(String url, String namespace, String methodName, Map<String, Object> params) throws Exception {
// log.info("调用 WebService 发送参数==>" + MapperUtils.mapToJson(params));
String soapActionURI = namespace + methodName;
Service service = new Service();
SOAPHeaderElement header = new SOAPHeaderElement(namespace, methodName);
header.setNamespaceURI(namespace);
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(url);
call.setOperationName(new QName(namespace, methodName));
// 添加参数
List<String> parameterList = Lists.newArrayList();
if (params != null) {
Set<String> paramsKey = params.keySet();
for (String key : paramsKey) {
call.addParameter(new QName(namespace, key), XMLType.XSD_STRING, ParameterMode.IN);
String pValue = MapUtils.getString(params, key);
header.addChildElement(key).setValue(pValue);
parameterList.add(pValue);
}
}
call.setUseSOAPAction(true);
call.setSOAPActionURI(soapActionURI);
call.addHeader(header);
// 设置返回类型
call.setReturnType(XMLType.XSD_SCHEMA);
// call.setReturnType(XMLType.SOAP_DOCUMENT);
// 接口返回结果
Schema schemaResult = (Schema) call.invoke(parameterList.toArray());
StringBuilder result = new StringBuilder();
for (int i = 0; i < schemaResult.get_any().length; i++) {
result.append(schemaResult.get_any()[i]);
}
return result.toString();
}
/**
* 对返回xml解析json对象key值修改,(xmlns: 删除,key值包含‘:’号,截取‘:’号后面的字符)
* @date 2023/11/16 13:54
*/
public static void processKeys(JSONObject jsonObject, Map<String, String> keyMap) {
for (String key : jsonObject.keySet()) {
Object value = jsonObject.get(key);
if (key.contains("xmlns:")) {
keyMap.put(key, "del");
} else {
int i = key.indexOf(":");
if (i != -1) {
String processedKey = key.substring(i + 1);
keyMap.put(key, processedKey);
}
}
if (value instanceof JSONObject) {
// 如果值是嵌套的JSON对象,则递归处理
processKeys((JSONObject) value, keyMap);
}
if (value instanceof JSONArray) {
JSONArray arr = (JSONArray) value;
for (Object o : arr) {
processKeys((JSONObject) o, keyMap);
}
}
}
}
public static JSONObject analyseJson(JSONObject jsonObject, Map<String, String> keyMap) {
SortedMap<String, Object> map = new TreeMap<>();
//1. 获取所有的Key,得到key对应的value
for (String key : jsonObject.keySet()) {
//在此判断==》如果没有要替换的值,resKey就为原值,否则为要替换的值
String resKey = keyMap.get(key) == null ? key : keyMap.get(key);
Object value = jsonObject.get(key);
//实现删除功能
if ("del".equals(resKey)) {
map.remove(resKey);
//实现替换key的功能
} else {
//1.1 如果value为JSONArray类型,则解析
if (value instanceof JSONArray) {
JSONArray jsonArray = new JSONArray(new LinkedList<>());
JSONArray array = jsonObject.getJSONArray(key);
for (int i = 0; i < array.size(); i++) {
Object object = array.get(i);
//如果value里面是List: ["aaa","bbb","ccc"]; 或者是value里面是JSONObject[{},{},{}]
if (object instanceof String) {
map.put(resKey, array);
} else {
JSONObject sortJson = analyseJson((JSONObject) object, keyMap);
jsonArray.add(sortJson);
map.put(resKey, jsonArray);
}
}
//2.2 如果value为JSONObject类型,则解析
} else if (value instanceof JSONObject) {
JSONObject sortJson = analyseJson((JSONObject) value, keyMap);
map.put(resKey, sortJson);
//2.3 如果value为JSON类型,则装入map
} else {
map.put(resKey, value);
}
}
}
return new JSONObject(map);
}
}
java 调用webservice 并解析xml为json字符
最新推荐文章于 2024-07-12 14:46:37 发布