WebService基础入门学习

发布WebService

  • 1.新建一个简单的Java项目 TestWebservice, 作为服务端

  • 2.新建Package com.ch.service

  • 3.新建带有main方法的类HelloService.java,并在类上加@WebService的注解

在main中使用EndPoint类的publish方法:还需要至少提供一个可以发布的方法,(方法不能是静态并且是非final的),只有这样的方法才可被发布.

代码如下:

package com.ch.service;

import javax.jws.WebService;
import javax.xml.ws.Endpoint;

@WebService
public class HelloService {
    public String sayHello(String name){
        System.out.println("Hello  :" + name);
        return "Hello  :" + name;
    }

    public static void main(String[] args) {
        Endpoint.publish("http://localhost:8080/hello", new HelloService());
        System.out.println("Server published !");
    }
}
  • 4.右键HelloService类, 运行该类的main方法,发布service

image.png

  • 5.运行结果

使用EndPoint.publish()方法将会新开启一个线程,所以并不会影响主线程的运行,所以运行主方法时,控制台仍然可以看到有输出:server published 的信息

image.png

服务发布成功后,在客户端调用. 启动服务后,在浏览器中输入绑定的服务地址+”?wsdl”即可查看服务的说明书。wsdl- WebService Description Language,是以XML文件形式来描述WebService的”说明书”,有了说明书,我们才可以知道如何使用或是调用这个服务.

image.png

整体项目如下图:

image.png

开发Webservice客户端调用服务

  • 1.新建一个简单的Java项目 TestWebserviceClient 作为客户端

  • 2.借助jdk的wsimort.exe工具生成客户端代码,wsimort.exe工具位于Jdk的bin目录下,如下图所示:

image.png

  • 3.打开命令行窗口, 进入src目录,执行命令生成客户端代码: wsimport -keep url (url为wsdl文件的路径)

如: wsimport -keep http://localhost:8080/hello?wsdl

image.png

执行命令的过程中没有出现任何错误,那么代码就生成成功了,刷新一下src目录,就可以看到生成的代码了,如下图所示:

image.png

此处注意:是生成而不是下载,服务器上并没有所生成的所有的类和方法。

  • 4.借助生成的代码编写调用HelloService对外提供的方法sayHello

wsimport工具帮我们生成了好几个java类,但我们只需要关心HelloService.class类即可

import com.ch.service.HelloService;
import com.ch.service.HelloServiceService;

public class Main {

    public static void main(String[] args) {
        //创建一个用于产生HelloService实例的工厂,HelloServiceService类是wsimport工具生成的
        HelloServiceService helloServiceService = new HelloServiceService();
        //通过工厂生成一个helloService实例,helloService是wsimport工具生成的
        HelloService helloService = helloServiceService.getHelloServicePort();
        //调用WebService的sayHello方法
        String msg = helloService.sayHello("张三");
        System.out.println(msg);
    }
}
  • 5.客户端调用服务器端的WebService方法运行结果如下:

image.png

客户端调用网络上的Web服务

1.进入http://www.webxml.com.cn/zh_cn/web_services.aspx找到手机号码归属地服务的wsdl

<http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl>

2.通过wsimport生成本地代码,调用网络上的web服务,比如手机号码归属地服务。

3.MobileCodeWS实例化工厂, 创建实例MobileCodeWSSoap,调用getMobileCodeInfo( phoneNum, userId)方法

import cn.com.webxml.MobileCodeWS;
import cn.com.webxml.MobileCodeWSSoap;
import com.ch.service.HelloService;
import com.ch.service.HelloServiceService;

public class Main {

    public static void main(String[] args) {
        /***手机号码归属地服务*/
        MobileCodeWS mobileCodeWS = new MobileCodeWS();
        MobileCodeWSSoap mobileCodeWSSoap = mobileCodeWS.getMobileCodeWSSoap();
        String city = mobileCodeWSSoap.getMobileCodeInfo("130271743**",null);
        System.out.println("手机号码的归属地为:"+city);
    }
}

4.结果如下:获得国内手机号码归属地省份、地区和手机卡类型信息

手机号码的归属地为:130271743**:湖北 武汉 湖北联通如意通卡

如果需要免费的Web服务,可以在网址http://www.webxml.com.cn/zh_cn/web_services.aspx中查询相关服务,点击Endpoint可以进入服务的相关介绍页面

image.png

image.png

猜你喜欢

转载自blog.csdn.net/a1786223749/article/details/79006855