webservice 接口 请求与返回处理 解析xml,json转map

 

import com.fasterxml.jackson.databind.ObjectMapper;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.soap.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.Map;


/**
 * @ClassName test
 * @author: mr.zhou
 * @create: 2024-04-25 17:21:23
 * @Version 1.0
 **/
public class test {

    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        map.put("Loginid", "MKGsD");
        map.put("Password", "mkgdd");

        Map<String, Object> result = wl("http://183.238.385.362:8100/soap/ssSoap","login",map);
        System.out.println(result.get("Token"));
    }

    public static Map<String, Object> wl(String url, String localName, Map<String, String> map) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 创建SOAP连接
            SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
            SOAPConnection soapConnection = soapConnectionFactory.createConnection();

            // 创建SOAP消息
            MessageFactory messageFactory = MessageFactory.newInstance();
            SOAPMessage soapMessage = messageFactory.createMessage();

            // 创建SOAP部分
            SOAPPart soapPart = soapMessage.getSOAPPart();

            // 创建SOAP信封
            SOAPEnvelope soapEnvelope = soapPart.getEnvelope();

            soapEnvelope.addNamespaceDeclaration("web",url);

            // 创建SOAP主体
            SOAPBody soapBody = soapEnvelope.getBody();

            // 创建SOAP操作
            SOAPElement soapOperation = soapBody.addChildElement(localName, "web");
            for (Map.Entry<String, String> stringObjectEntry : map.entrySet()) {
                System.out.println(stringObjectEntry.getKey()+":key");
                System.out.println(stringObjectEntry.getValue()+":value");
                SOAPElement soapArgument = soapOperation.addChildElement(stringObjectEntry.getKey());
                soapArgument.addTextNode(stringObjectEntry.getValue());
            }

            // 发送SOAP消息并获取响应
            String endpointUrl = url;
            SOAPMessage soapResponse = soapConnection.call(soapMessage, endpointUrl);

            // 处理SOAP响应
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            soapResponse.writeTo(outputStream);
            result = parseXmlString(outputStream.toString());
            // 关闭SOAP连接
            soapConnection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static Map<String, Object> parseXmlString(String xmlString) {
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(new ByteArrayInputStream(xmlString.getBytes("UTF-8")));

            document.getDocumentElement().normalize();

            //获得retrurn节点
            NodeList nodeList = document.getElementsByTagName("return");

            for (int i = 0; i < nodeList.getLength(); i++) {
                Node node = nodeList.item(i);

                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    Element element = (Element) node;

                    ObjectMapper objectMapper = new ObjectMapper();
                    Map<String, Object> map = objectMapper.readValue(element.getTextContent(), Map.class);
                    return map;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

IT周小白

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值