java connect webservice


foreword

WebService is also called XML Web
Service. WebService is a lightweight and independent communication technology that can receive requests from other systems on the Internet or Intranet. It is a software service provided on the Web through SOAP, described using a WSDL file, and registered through UDDI.


1. Remote call using HTTP+SOAP

Maven imports dependencies:

   <!--webservice 使用hutool的SoapClient -->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.22</version>
        </dependency>
         <dependency>
            <groupId>org.dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>2.1.3</version>
        </dependency>
         <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.7</version>
        </dependency>
          <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>2.1.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis-jaxrpc</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>wsdl4j</groupId>
            <artifactId>wsdl4j</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-core</artifactId>
            <version>3.4.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.4.3</version>
        </dependency>
        <dependency>
            <groupId>commons-discovery</groupId>
            <artifactId>commons-discovery</artifactId>
            <version>0.2</version>
        </dependency>

Online wsdl example: http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl

package com.gcddd.earthquake.controller;

import cn.hutool.http.webservice.SoapClient;
import com.alibaba.fastjson.JSONObject;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.StringReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class WebServiceTest {
    
    


        public static void main(String[] args) {
    
    
            //请求地址
            String soapUrl = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl";
            HashMap<String, Object> map = new HashMap<>();
            map.put("theCityName", "重庆");//请求参数,多个继续put即可
            SoapClient soapClient = SoapClient.create(soapUrl)
                    //请求方法,命名空间
                    .setMethod("web:getWeatherbyCityName", "http://WebXml.com.cn/")
                    .setParams(map);
            String sendSoap = soapClient.send(true);
            System.out.println(sendSoap);
            Map<String, String> map1 = new HashMap<String, String>();
            Map soapMap = XmlMap(sendSoap, map1);
            System.out.println(JSONObject.toJSONString(soapMap));


        }
    public static Map<String, String> XmlMap(String xml, Map<String, String> map) {
    
    
        try {
    
    
            SAXReader reader = new SAXReader();
            Document doc = reader.read(new StringReader(xml));
            Element root = doc.getRootElement();
            String path = "";
            if (map.containsKey(root.getName().trim())) {
    
    
                path = map.get(root.getName().trim());
                map.remove(root.getName().trim());
            }
            for (Iterator i = root.elementIterator(); i.hasNext(); ) {
    
    
                Element element = (Element) i.next();
                if (element.isTextOnly()) {
    
    
                    if (path.length() > 0) {
    
    
                        map.put(path + element.getName().trim(), element.getTextTrim());
                    } else {
    
    
                        map.put(element.getName().trim(), element.getTextTrim());
                    }
                } else {
    
    
                    map.put(element.getName().trim(), path + element.getName().trim() + ".");
                    XmlMap(element.asXML(), map);
                }
            }
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
        return map;
    }

}

Run to see the result:
insert image description here

2. IDEA generates webservice client

1.idea add webservice client module

Due to the large difference in idea versions, the functions are different, I use 2023.1

Settings - project structure
insert image description here
module - new module - webservice client

insert image description here

Choose Apache Axis as the ws engine. After selection, the code generation page may not appear. You can remove the module first, and then right-click the project
to add framework support.
insert image description here
insert image description here
insert image description here
insert image description here

The generated WebServiceTest code seems to be unusable, so change it to this:

package example;

import mypackage.ICEApiService_PortType;
import mypackage.ICEApiService_ServiceLocator;
import mypackage.ResultModel;

import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;

public class HelloWorldClient {
    
    
    public static void main(String[] argv) throws RemoteException, MalformedURLException {
    
    
        ICEApiService_ServiceLocator iceApiServiceServiceLocator = new ICEApiService_ServiceLocator();
        ICEApiService_PortType iceApiServiceImplPort = iceApiServiceServiceLocator.getICEApiServiceImplPort(new URL("http://IP:8808/ws/ice/api?wsdl"));
        ResultModel resultModel = iceApiServiceImplPort.send_open_message_data_new("eHFz", "测试消息推送", "这是摘要!", "这是消息推送正文!!!", "0dsa4906", "0", "https://www.baidu.com/", "1234", "0", "123");
    }
}

Because the generated lib package is external, it needs to be added to the project, or installed in maven before it can be used, right click "add as library"
insert image description here

Guess you like

Origin blog.csdn.net/qq_35222232/article/details/130258430