jax-ws创建和访问webservice

目前webservice在各个行业中的应用都十分的火。市面上目前流行的关于webservice的框架也有很多,今天我们不套用外部的框架,而是通过纯java来一步一步实现webservice的服务端的创建和客户端的访问。 

一、创建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接口 

Java代码 
  1. package com.sample.server;  
  2.   
  3. import javax.jws.WebMethod;  
  4. import javax.jws.WebService;  
  5.   
  6. import com.sample.bean.Student;  
  7.   
  8. @WebService  
  9. public interface MyService {  
  10.   
  11.     @WebMethod  
  12.     public String getStuName(String id);  
  13.       
  14.     public Student getStuInfo(String id);  
  15. }  






2、创建接口的实现类 

这里需要注意,一定要设置endpointInterface。它的值为接口的全称,一定要带上包名。 

Java代码 
  1. package com.sample.server;  
  2.   
  3. import java.util.Date;  
  4.   
  5. import javax.jws.WebService;  
  6.   
  7. import com.sample.bean.Student;  
  8. @WebService(endpointInterface="com.sample.server.MyService")  
  9. public class MyServiceImpl implements MyService {  
  10.   
  11.     @Override  
  12.     public String getStuName(String id) {  
  13.         if("1".equals(id)) {  
  14.             System.out.println("getStuName");  
  15.             return "张三" ;  
  16.         }  
  17.         return "";  
  18.     }  
  19.   
  20.     @Override  
  21.     public Student getStuInfo(String id) {  
  22.         Student student = new Student();  
  23.         if("1".equals(id)) {  
  24.             System.out.println("getStuInfo");  
  25.             student.setId("1");  
  26.             student.setName("张三");  
  27.             student.setPassword("123456");  
  28.             student.setAge(12);  
  29.             student.setMakedate(new Date());  
  30.         }  
  31.         return student;  
  32.     }  
  33.   
  34. }  


3、发布服务 

可以在这里创建一个Server类。通过Endpoint的publish方法发布服务,代码中的http://localhost:8082/stu为此服务的发布地址,new MyServiceImpl()表示服务的实现类。运行main方法即可发布成功,如果发现有错误跑出,查看一下端口号是否可用。 

Java代码 
  1. package com.sample.server;  
  2.   
  3. import javax.xml.ws.Endpoint;  
  4.   
  5.   
  6. public class Server {  
  7.   
  8.     public static void main(String[] args) {  
  9.         Endpoint.publish("http://localhost:8082/stu"new MyServiceImpl());  
  10.     }  
  11. }  


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,表示访问成功。 

Java代码 
  1. package com.sample.client;  
  2.   
  3. import java.net.MalformedURLException;  
  4. import java.net.URL;  
  5.   
  6. import javax.xml.namespace.QName;  
  7. import javax.xml.ws.Service;  
  8.   
  9. import com.sample.server.MyService;  
  10.   
  11.   
  12. public class MyClient {  
  13.   
  14.     public static void main(String[] args) throws MalformedURLException {  
  15.         String address = "http://localhost:8082/stu?wsdl";  
  16.         URL url = new URL(address);  
  17.         QName qname = new QName("http://server.sample.com/""MyServiceImplService");  
  18.         Service service = Service.create(url,qname);  
  19.         MyService tMyservice = service.getPort(MyService.class);  
  20.         tMyservice.getStuName("1");  
  21.     }  
  22. }  


(三)、遗留问题 

上面这种访问方式,我们会发现在客户端中要依赖MyService这个类,而实际工作中,我们根本无法获取到MyService,服务商也不可能为我们提供这个接口,所以,我们将在下一节中通过wsimport来解决这个问题。 

版权声明:本文为gxsenjoy原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。