WebService技术,服务端发布到Tomcat(使用Servlet发布),客户端使用axis2实现(二)

仍是在WebService技术,服务端and客户端JDK-wsimport工具(一)的基础上实现。新建一个包:com.aixs2client。目录结构以下:html

 

1、服务端:java

  一、仍是使用com.webservice包里的WebServiceImp.java 文件,可是不使用本地发布,因此须要删除发布代码。web

  WebServiceImp.java:apache

package com.webservice; import javax.jws.WebMethod; import javax.jws.WebService; @WebService public class WebServiceImp { @WebMethod public String getInfo(String id){ String info=""; if (id.equals("1")) { info="张三"; }else if (id.equals("2")) { info="李四"; }else if(id.equals("3")){ info="王五"; }else if(id.equals("4")) { info="赵六"; }else { info="用户不存在"; } return info; } }

 二、WEB-INF下新建一个xml文件,sun-jaxws.xml,内容以下:浏览器

endpoint 表示使用此配置文件里的参数发布
name:发布的名称,名字能够随意
implementation:发布的服务的实现类url-pattern:访问wsdl文档时的路径
1 <?xml version="1.0" encoding="UTF-8"?> 2 <endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime" version="2.0"> 3 <endpoint name="WebServiceDemo" implementation="com.webservice.WebServiceImp" url-pattern="/WebServiceDemo"> 4 </endpoint> 5 </endpoints>

 

 

三、在WEB-INF下的web.xml中加入servlet配置.内容以下:app

  web.xmljsp

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name></display-name>    
    <servlet>
        <servlet-name>jaxws</servlet-name>
        <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>jaxws</servlet-name>
        <url-pattern>/WebServiceDemo</url-pattern>
    </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
load-on-startup:是否应该在web应用程序启动的时候就加载这个servlet,调用servlet中的init()方法,若是值为正整数或者0时,表示容器在应用启动时就加载并初始化这个servlet,值越小,servlet的优先级越高,就越先被加载。
url-pattern:请求的路径

 

四、配置完成后,将项目部署到Tomcat中,并在浏览器地址栏输入servlet请求的地址:http://127.0.0.1:8080/WebService/WebServiceDemo。工具

能够看到以下页面:测试

点击WSDL后面的url,看到wsdl文档:url

此时服务端完成,服务已经随着Tomcat的启动和关闭,能够使用Soap UI进行测试。

 

2、客户端

一、在com.aixs2client包下新建ClientService.java

 1 package com.aixs2client;  2 import org.apache.axiom.om.OMElement;  3 import org.apache.axis2.AxisFault;  4 import org.apache.axis2.addressing.EndpointReference;  5 import org.apache.axis2.client.Options;  6 import org.apache.axis2.rpc.client.RPCServiceClient;  7 
 8 import javax.xml.namespace.QName;  9 public class ClientService { 10         public static String getServiceInfo(String Id) throws Exception { 11             String result = null; 12             //名称空间
13             String nameSpace="http://webservice.com/"; 14             //服务的方法
15             String method="getInfo"; 16             //wsdl文档地址
17             String Url = "http://127.0.0.1:8080/WebService/WebServiceDemo?wsdl"; 18             QName qname = new QName(nameSpace,method); 19             Object[] param = new Object[] { Id }; 20             try { 21                 //建立客户端实例
22                 RPCServiceClient client = new RPCServiceClient(); 23                 Options options = new Options(); 24                 options.setTo(new EndpointReference(Url)); 25                 options.setAction(nameSpace+method);//调用.net等webservice服务是务必加上
26  client.setOptions(options); 27                 OMElement element = client.invokeBlocking(qname, param); 28                 //获取服务端返回的结果
29                 result = element.getFirstElement().getText(); 30  System.out.println(result); 31             } catch (AxisFault e) { 32  e.printStackTrace(); 33  } 34             return result; 35  } 36         public static void main(String[] args) { 37             try { 38                 getServiceInfo("1"); 39                 getServiceInfo("3"); 40                 getServiceInfo("4"); 41                 getServiceInfo("100"); 42             } catch (Exception e) { 43  e.printStackTrace(); 44  } 45  } 46         
47  } 48 
49

运行客户端代码,调用服务端方法,结果以下:

相关文章
相关标签/搜索