1) 搭建WebService服务端的要素及步骤
1. 需要建立一个接口
2. 需要为接口建立一个对应的实现类
3. 调用endpoint对象的publish方法发布webservice(若干方法之一)
2) 使用Java作为客户端访问WebService的要素及步骤
1. 使用wsdl2java工具将对应的wsdl文件转换为客户端的java类文件
2. 类文件中找到实现类的服务工厂类,例如java实现类名为DemoImpl则对应的服务工厂类名为DemoImplService
3. 获取对应的类对象实例通过对应的getport方法,然后使用类的对象实例的方法进行远程调用WebService函数
以下通过实例进行说明
建立服务端项目名称为WS_Server
1. 建立HelloWorld接口
package it.heima;
import javax.jws.WebService;
@WebService //标记此接口为WebService使用的注解,此行必不可少,没有它WebService将无法正常工作
public interface HelloWorld {
public String SayHi(String name);
}
2. 建立HelloWorld接口的实现类HelloWorldImpl
package it.heima;
import javax.jws.WebService;
@WebService //标记此实现类为WebService使用的注解,此行必不可少,没有它WebService将无法正常工作
public class HelloWorldImpl implements HelloWorld {
@Override
public String SayHi(String name) {
// TODO Auto-generated method stub
return "Hello " + name;
}
}
3. 建立一个测试类用来建立WebService
package it.heima;
import javax.xml.ws.Endpoint;
public class WebServiceDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
//发布WebService使用http://192.168.0.117:666/hello地址进行发布
Endpoint.publish("http://192.168.0.117:666/hello", new HelloWorldImpl());
}
}
对应的项目中增加CXF框架的引用
输入框架名称CXF 然后点击“Add External JARs”导入jar文件列表
然后运行测试类,运行结果如下
打开浏览器访问http://192.168.0.117:666/hello?wsdl 可以得到如下xml文档片段
<?xml version='1.0' encoding='UTF-8'?><wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://heima.it/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="HelloWorldImplService" targetNamespace="http://heima.it/">
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://heima.it/" elementFormDefault="unqualified" targetNamespace="http://heima.it/" version="1.0">
<xs:element name="SayHi" type="tns:SayHi"/>
<xs:element name="SayHiResponse" type="tns:SayHiResponse"/>
<xs:complexType name="SayHi">
<xs:sequence>
<xs:element minOccurs="0" name="arg0" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="SayHiResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="SayHi">
<wsdl:part element="tns:SayHi" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:message name="SayHiResponse">
<wsdl:part element="tns:SayHiResponse" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:portType name="HelloWorld">
<wsdl:operation name="SayHi">
<wsdl:input message="tns:SayHi" name="SayHi">
</wsdl:input>
<wsdl:output message="tns:SayHiResponse" name="SayHiResponse">
</wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="HelloWorldImplServiceSoapBinding" type="tns:HelloWorld">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="SayHi">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="SayHi">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="SayHiResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="HelloWorldImplService">
<wsdl:port binding="tns:HelloWorldImplServiceSoapBinding" name="HelloWorldImplPort">
<soap:address location="http://192.168.0.117:666/hello"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
至此服务端开发暂告一段落。以下进行客户端的程序测试
首先将CXF框架bin的目录加入至系统path环境变量中
1. 使用wsdl2java工具将wsdl转换成对应的java类文件
2. 建立ClientDemo测试类
package it.heima;
public class ClientDemo {
public static void main(String[] args) {
//生成服务工厂
HelloWorldImplService factory = new HelloWorldImplService();
//产生服务对象
HelloWorld hw = factory.getHelloWorldImplPort();
//打印调用sayHi方法的结果
System.out.println(hw.sayHi("zhangsan"));
}
}
运行结果如下图:
至此一个最简单的WebService建立完毕