webservice简单使用

本文介绍了WebService的基本使用,包括服务端的搭建、客户端的调用,并通过实例演示了如何创建和访问WebService。在实践中遇到了因同一工程下放置服务端和客户端代码导致的问题,以及解决方法。还尝试了访问外部WebService服务,对于遇到的代理问题,通过下载WSDL文件并本地生成客户端代码成功解决。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

webservice简单使用以及问题总结

本人只是学习,总结而已,对自己遇到问题的一个总结!
原文链接参考1https://blog.youkuaiyun.com/c99463904/article/details/76018436
原文链接参考2https://blog.youkuaiyun.com/aqsunkai/article/details/51711087

学习内容:

WebService采用Http协议来在客户端和服务端之间传输数据。WebService使用XML来封装数据,简要包括XML,SOAP和WSDL。

搭建一个服务端进行访问:

服务端代码

1、定义一个接口:

public interface WeatherInterface {

    public String queryWeather(String cityName);
}

2、定义一个实现类,使用@WebService定义需要发布类

@WebService     //@WebService表示该类是一个服务类,需要发布其中的public的方法
public class WeatherInterfaceImpl implements WeatherInterface{
    @Override
    public String queryWeather(String cityName) {
        System.out.println("获取城市名"+cityName);
        String weather="暴雨";
        return weather;
    }
}

3、发布服务

public class WeatherServer {

    public static void main(String[] args) {
        Endpoint.publish("http://127.0.0.1:12345/weather", new WeatherInterfaceImpl());
    }
}

4、启动服务,执行WeatherServer的main方法,访问链接http://127.0.0.1:12345/weather?wsdl看是否出现wsdl xml说明舒,如果成功出现,说明服务端完成了。
在这里插入图片描述

客户端代码

1、使用jdk自带的wsimport来生成客户端代码

wsimport -keep -s . -p com.sun http://127.0.0.1:12345/weather?wsdl

2、拷贝生成的java代码到工程包下
在这里插入图片描述
3、编写客户端代码(WeatherClient)

public class WeatherClient {
    public static void main(String[] args) {
        //创建服务视图,视图是从wsdl文件的service标签的name属性获取
        WeatherInterfaceImplService weatherInterfaceImplService=new WeatherInterfaceImplService();

        //获取服务实现类,实现类从wsdl文件的portType的name属性获取
        WeatherInterfaceImpl weatherInterfaceImpl=weatherInterfaceImplService.getPort(WeatherInterfaceImpl.class);
        //获取查询方法,从portType的operation标签获取
        String weather=weatherInterfaceImpl.queryWeather("北京");
        System.out.println(weather);
    }
}

注意:此处我遇到一个问题,就是把client端也放入和server端一样的工程下,导致出现问题,后来新建一个工程就没问题了,这也属于远程rpc调用!(客户端不要与server端代码放一个工程里

访问其他webservice服务端:

webservice参考网站接口,里面提供了webservice服务

1、访问国内手机号码归属地查询WEB服务服务
在这里插入图片描述
2、生成客户端代码

wsimport -keep -s . -p com.sun.mobile -verbose http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl

但是出现问题:

wsimport -keep -s . -p com.sun.mobile -verbose http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl
正在解析 WSDL...


[ERROR] Can't connect to SOCKS proxy:Connection refused: connect

无法读取 WSDL 文档: http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl, 原因为 1) 找不到文档; 2) 无法读取文档; 3) 文档的根元素不是 <wsdl:definitions>[ERROR] failed.noservice=在提供的 WSDL 中找不到 wsdl:service:

需要至少提供一个 WSDL, 该 WSDL 至少具有一个服务定义。


        无法解析 WSDL。

后来网上找了半天说是internet有代理,去掉就行,但是我这没设置代理,浏览器能正常看到wsdl描述内容,后来看了网上其他大神的东西,这个wsdl链接就是个对weservice描述的说明书,我把它下载下来然后生成代码不也行吗?
右键–》另存为–》MobileCodeWS.xml
再次执行命令;

wsimport -keep -s . -p com.sun.mobile -verbose MobileCodeWS.xml

成功生成代码!
但是得修改下代码里面的东西,把配置文件变成http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl地址。
在这里插入图片描述
3、编写客户端代码

public class MobileWsClient {

    public static void main(String[] args) throws IOException {
        //创建一个MobileCodeWS工厂
        MobileCodeWS factory = new MobileCodeWS();
        //根据工厂创建一个MobileCodeWSSoap对象
        MobileCodeWSSoap mobileCodeWSSoap = factory.getMobileCodeWSSoap();
        //调用WebService提供的getMobileCodeInfo方法查询手机号码的归属地
        String searchResult = mobileCodeWSSoap.getMobileCodeInfo("18614099874", null);
        System.out.println(searchResult);

      // main3();

    }

执行结果如下:
在这里插入图片描述
至此,webservice初步趟雷完成!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值