一、創建webservice的服務器端
整體步驟如下:
1、創建服務的接口
2、創建服務的實現類
3、發佈服務
4、驗證服務是否發佈成功
二、創建webservice的客戶端
整體步驟如下:
1、通過java.net.URL定義連接webservice服務端的URL地址
2、通過javax.xml.namespace.QName設置命名空間和本地服務名稱
3、通過javax.xml.ws.Service創建一個從客戶端到服務器端的連接
4、通過javax.xml.ws.Service的getPort獲取接口類
5、通過接口訪問相應的方法
三、案例演示
(一)、創建webservice的服務器端
1、創建接口類
通過java的註解@WebService標明此接口是一個WebService接口
- package com.sample.server;
- import javax.jws.WebMethod;
- import javax.jws.WebService;
- import com.sample.bean.Student;
- @WebService
- public interface MyService {
- @WebMethod
- public String getStuName(String id);
- public Student getStuInfo(String id);
- }
2、創建接口的實現類
這裏需要注意,一定要設置endpointInterface。它的值爲接口的全稱,一定要帶上包名。
- package com.sample.server;
- import java.util.Date;
- import javax.jws.WebService;
- import com.sample.bean.Student;
- @WebService(endpointInterface="com.sample.server.MyService")
- public class MyServiceImpl implements MyService {
- @Override
- public String getStuName(String id) {
- if("1".equals(id)) {
- System.out.println("getStuName");
- return "張三" ;
- }
- return "";
- }
- @Override
- public Student getStuInfo(String id) {
- Student student = new Student();
- if("1".equals(id)) {
- System.out.println("getStuInfo");
- student.setId("1");
- student.setName("張三");
- student.setPassword("123456");
- student.setAge(12);
- student.setMakedate(new Date());
- }
- return student;
- }
- }
3、發佈服務
可以在這裏創建一個Server類。通過Endpoint的publish方法發佈服務,代碼中的http://localhost:8082/stu爲此服務的發佈地址,new MyServiceImpl()表示服務的實現類。運行main方法即可發佈成功,如果發現有錯誤跑出,查看一下端口號是否可用。
- package com.sample.server;
- import javax.xml.ws.Endpoint;
- public class Server {
- public static void main(String[] args) {
- Endpoint.publish("http://localhost:8082/stu", new MyServiceImpl());
- }
- }
4、驗證服務是否發佈成功
在瀏覽器中輸入http://localhost:8082/stu,如果出現以下內容表示服務已經發布成功
:
Web Services
No JAX-WS context information available.
在瀏覽器中輸入http://localhost:8082/stu?wsdl就可以查看生成的wsdl文件,我的內容如下:
<?xml version="1.0" encoding="UTF-8" ?>
- <!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6.
-->
- <!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6.
-->
- <definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://server.sample.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://server.sample.com/" name="MyServiceImplService">
- <types>
- <xsd:schema>
<xsd:import namespace="http://server.sample.com/" schemaLocation="http://localhost:8082/stu?xsd=1" />
</xsd:schema>
</types>
- <message name="getStuName">
<part name="parameters" element="tns:getStuName" />
</message>
- <message name="getStuNameResponse">
<part name="parameters" element="tns:getStuNameResponse" />
</message>
- <message name="getStuInfo">
<part name="parameters" element="tns:getStuInfo" />
</message>
- <message name="getStuInfoResponse">
<part name="parameters" element="tns:getStuInfoResponse" />
</message>
- <portType name="MyService">
- <operation name="getStuName">
<input message="tns:getStuName" />
<output message="tns:getStuNameResponse" />
</operation>
- <operation name="getStuInfo">
<input message="tns:getStuInfo" />
<output message="tns:getStuInfoResponse" />
</operation>
</portType>
- <binding name="MyServiceImplPortBinding" type="tns:MyService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
- <operation name="getStuName">
<soap:operation soapAction="" />
- <input>
<soap:body use="literal" />
</input>
- <output>
<soap:body use="literal" />
</output>
</operation>
- <operation name="getStuInfo">
<soap:operation soapAction="" />
- <input>
<soap:body use="literal" />
</input>
- <output>
<soap:body use="literal" />
</output>
</operation>
</binding>
- <service name="MyServiceImplService">
- <port name="MyServiceImplPort" binding="tns:MyServiceImplPortBinding">
<soap:address location="http://localhost:8082/stu" />
</port>
</service>
</definitions>
(二)、創建客戶端
1、創建客戶端類MyClient
這裏解釋一下QName的兩個參數:
第一個參數值是上面生成的wsdl文件中targetNamespace的值,我的爲"http://server.sample.com/"。這個值實際上就是包名的反轉。
第二個參數爲:
name="MyServiceImplService">
運行此方法可以在控制檯打印出getStuName,表示訪問成功。
- package com.sample.client;
- import java.net.MalformedURLException;
- import java.net.URL;
- import javax.xml.namespace.QName;
- import javax.xml.ws.Service;
- import com.sample.server.MyService;
- public class MyClient {
- public static void main(String[] args) throws MalformedURLException {
- String address = "http://localhost:8082/stu?wsdl";
- URL url = new URL(address);
- QName qname = new QName("http://server.sample.com/", "MyServiceImplService");
- Service service = Service.create(url,qname);
- MyService tMyservice = service.getPort(MyService.class);
- tMyservice.getStuName("1");
- }
- }
(三)、遺留問題
上面這種訪問方式,我們會發現在客戶端中要依賴MyService這個類,而實際工作中,我們根本無法獲取到MyService,服務商也不可能爲我們提供這個接口,所以,我們將在下一節中通過wsimport來解決這個問題。