java连接webservice

本文介绍了如何使用HTTP+SOAP方式远程调用WebService,通过引入Hutool库展示了一个Java示例。此外,还详细讲解了在IDEA中生成WebService客户端的步骤,包括选择ApacheAxis作为WS引擎,并展示了调整生成代码后的示例。
摘要由CSDN通过智能技术生成

前言

WebService 也叫XML Web
Service,WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的独立的通讯技术。是通过SOAP在Web上提供的软件服务,使用WSDL文件进行说明,并通过UDDI进行注册。


一、使用HTTP+SOAP方式远程调用

maven引入依赖:

   <!--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>

在线wsdl示例: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;
    }

}

运行即可看到结果:
在这里插入图片描述

二、IDEA生成webservice客户端

1.idea添加webservice客户端模块

由于idea版本差异较大,功能所在不一样,我用的2023.1

设置-项目结构
在这里插入图片描述
模块-新增模块-webservice客户端

在这里插入图片描述

ws引擎选择Apaache Axis,选择之后可能不会出现代码生成的页面,可以先移除模块,然后右键项目
添加框架支持
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

其中生成的WebServiceTest代码貌似不能用,改为这样:

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");
    }
}

因为生成的lib包是外部的,需要加入进项目,或者安装到maven才能使用,右键”添加为库“
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

儿时可乖了

混口饭吃,大佬们,赏点吧

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

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

打赏作者

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

抵扣说明:

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

余额充值