webservice复杂类型实例

 1.准备工作:

概念:SOAP(简单对象访问协议)、WSDL(web服务描述语言)、XML(可扩展标记语言)、axis(阿帕奇可扩展交互系统)

(1)     下载axis1.4,将axis1.4中的axis项目中WEB-INF\lib,将lib下的jar文件拷贝到工程如webservice的WEB-INF\lib下

(2)     将axis1.4中的axis项目,拷贝到tomcat的webapps目录下

(3)   配置Java和Tomcat环境,设置axis环境,类似jdk的配置方法,例如:

AXIS_HOME= E:\apache-tomcat-6.0.37-windows-x86\apache-tomcat-6.0.37\webapps\axis 

AXIS_LIB=%AXIS_HOME%\lib

AXISCLASSPATH=%AXIS_LIB%\axis.jar;%AXIS_LIB%\commons-discovery-0.2.jar;%AXIS_LIB%\commons-logging-1.0.4.jar;%AXIS_LIB%\jaxrpc.jar;%AXIS_LIB%\saaj.jar;%AXIS_LIB%\log4j-1.2.8.jar;AXIS_LIB%\xml-apis.jar;%AXIS_LIB%\xercesImpl.jar     (最后两个可能没有,不是必须的)

 

(4)       配置web.xml添加

 <?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web

Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

<display-name>Apache-Axis</display-name>

<listener>

<listener-class>org.apache.axis.transport.http.AxisHTTPSessionListener</listener-class>

</listener>

<servlet>

<servlet-name>AxisServlet</servlet-name>

<display-name>Apache-Axis Servlet</display-name>

<servlet-class>

org.apache.axis.transport.http.AxisServlet

    </servlet-class>

</servlet>

<servlet>

<servlet-name>AdminServlet</servlet-name>

<display-name>Axis Admin Servlet</display-name>

<servlet-class>

org.apache.axis.transport.http.AdminServlet

    </servlet-class>

<load-on-startup>100</load-on-startup>

</servlet>

<servlet>

<servlet-name>SOAPMonitorService</servlet-name>

<display-name>SOAPMonitorService</display-name>

<servlet-class>

org.apache.axis.monitor.SOAPMonitorService

    </servlet-class>

<init-param>

<param-name>SOAPMonitorPort</param-name>

<param-value>5001</param-value>

</init-param>

<load-on-startup>100</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>AxisServlet</servlet-name>

<url-pattern>/servlet/AxisServlet</url-pattern>

</servlet-mapping>

<servlet-mapping>

<servlet-name>AxisServlet</servlet-name>

<url-pattern>*.jws</url-pattern>

</servlet-mapping>

<servlet-mapping>

<servlet-name>AxisServlet</servlet-name>

<url-pattern>/services/*</url-pattern>

</servlet-mapping>

<servlet-mapping

<servlet-name>SOAPMonitorService</servlet-name>

<url-pattern>/SOAPMonitor</url-pattern>

</servlet-mapping>

<session-config>

<session-timeout>5</session-timeout>

</session-config>

<mime-mapping>

<extension>wsdl</extension>

<mime-type>text/xml</mime-type>

</mime-mapping>

<mime-mapping>

<extension>xsd</extension>

<mime-type>text/xml</mime-type>

</mime-mapping>

<welcome-file-list id="WelcomeFileList">

<welcome-file>index.jsp</welcome-file>

<welcome-file>index.html</welcome-file>

<welcome-file>index.jws</welcome-file>

</welcome-file-list>

</web-app>

 

 

2.实战

1.业务方法类的入参bean

Request.java

package com.webservice.request;

 

import java.util.ArrayList;

import java.util.List;

 

public class Request {

 

    // 基本类型

    private String msg;

 

    // 基本类型 ,list会在客户端生成对象数组Object[]

    private List<Integer> list = new ArrayList<Integer>();

 

    // 不要使用数组,因为要指定大小,应该使用list替代

    // private String[] array;

 

    public List<Integer> getList() {

       return list;

    }

 

    public String getMsg() {

       return msg;

    }

 

    public void setList(List<Integer> list) {

       this.list = list;

    }

 

    public void setMsg(String msg) {

       this.msg = msg;

    }

 

 

}

 

 

2. 业务方法类的回参bean

Response.java

package com.webservice.respone;

 

import java.util.ArrayList;

import java.util.List;

 

public class Response {

 

    // 基本类型

    private int code;

 

    private String errorMsg;

 

    // 基本类型 ,list会在客户端生成对象数组Object[]

    private List<Integer> list = new ArrayList<Integer>();

 

    // 不要使用数组,因为要指定大小,应该使用list替代

    // private String[] array;

 

    public int getCode() {

       return code;

    }

 

    public void setCode(int code) {

       this.code = code;

    }

 

    public String getErrorMsg() {

       return errorMsg;

    }

 

    public void setErrorMsg(String errorMsg) {

       this.errorMsg = errorMsg;

    }

 

    public List<Integer> getList() {

       return list;

    }

 

    public void setList(List<Integer> list) {

       this.list = list;

    }

 

}

 

 

3. 业务方法类service,以上面两个bean作为入参和回参

BusinessService.java

package com.webservice.service;

 

import java.util.List;

 

import com.webservice.request.Request;

import com.webservice.respone.Response;

 

public class BusinessService {

 

     

    public Response send(Request request)

 

    {

       Response response = new Response();

 

       try {

           //接收基本类型信息

           String msg = request.getMsg();

          

           //接收list

           List<Integer> integerList = request.getList();

 

           System.out.println("收到基本类型消息:msg=" + msg);

           System.out.println("收到List<Integer>类型消息:list=" + integerList);

           

           //组织反馈

           if (null != msg && !"".equals(msg)) {

              //设置错误码

              response.setCode(0);

              //设置错误消息

              response.setErrorMsg("调用成功");

              //设置list

              response.setList(request.getList());

 

           } else {

              response.setCode(-1);

              response.setErrorMsg("消息不能为空");

           }

       } catch (Exception e) {

           response.setCode(-1);

           response.setErrorMsg("发生异常:" + e.getMessage());

       }

       return response;

    }

}

 

 

4.在工程的WEB-INF目录下 编写deploy.wsdd

主要用来定义service名字,描述service、入参、回参

<deployment xmlns="http://xml.apache.org/axis/wsdd/"

    xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">

    <service name="businessService" provider="java:RPC">

       <parameter name="className" value="com.webservice.service.BusinessService" />

       <parameter name="allowedMethods" value="*" />

       <beanMapping qname="myNS:Request" xmlns:myNS="urn:BeanService"

           languageSpecificType="java:com.webservice.request.Request" />

       <beanMapping qname="myNS:Response" xmlns:myNS="urn:BeanService"

           languageSpecificType="java:com.webservice.respone.Response" />

    </service>

</deployment>

 

5. 在工程的WEB-INF目录下编写undeploy.wsdd

用来卸载webservice服务

<undeployment xmlns="http://xml.apache.org/axis/wsdd/">

   <service name="businessService"/>

</undeployment>

 

6. 部署项目(启动tomcat),接下来发布接口(关键一步)

进入到tomcat的项目中的WEB-INF目录下,执行下列命令进行发布。会在此WEB-INF下生成server-config.wsdd文件(以后可以在此目录配置其他接口)

java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient -lhttp://192.168.30.143:8080/webservice/servlet/AxisServlet deploy.wsdd

 

Ps:下面命令用于取消一个已经发布的服务

java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient -lhttp://192.168.30.143:8080/webservice/servlet/AxisServlet undeploy.wsdd

 

7.获取wsdl(完成)

(1)重启tomcat,访问工程目录,如:http://192.168.30.143:8080/webservice/servlet/AxisServlet(其中webservice为工程名)

(2)可以看到新的服务方法businessService,点击一下,跳到wsdl内容页面,ctr+A拷贝到文本编辑器,去掉开头的空格,替换去掉所有开头的“-”为空(即将- <替换为<);另存为businessService.wsdl,即可提供其他方使用,注意:如果部署在linux上,要访问linux的网页获取wsdl,不然可能会有问题

-----------------------------------------------至此发布完毕------------------------------------------------

 

 

 

3.测试验证

1.根据wsdl生成客户端代码

(1)首先制作生成代码工具,方便以后重用;

在D:\webservice,放个axis1.4的包,创建一个批处理文件“wsdl生成java客户端文件.bat”,内容如下:根据实际情况配置

set Axis_Lib=D:\webservice\axis1.4\axis\WEB-INF\lib

set Java_Cmd=java -Djava.ext.dirs=%Axis_Lib%

set Output_Path=D:\webservice\Testclient

set Package=com.webservice.client

%Java_Cmd% org.apache.axis.wsdl.WSDL2Java -o%Output_Path% -p%Package% D:\webservice\businessService.wsdl

@echo  -------------OK-----------------

@echo ---The code has been created in the folder: Testclient---

@pause

说明:蓝色部分根据实际情况调整

Axis_Lib 是axis1.4的lib位置

Output_Path是生成客户端代码存放位置

Package 是生成客户端代码所在包

最后是wsdl文件位置

(2)生成客户端代码

将wsdl文件,如businessService.wsdl放到D:\webservice下(上一步应该做了),执行文件“wsdl生成java客户端文件.bat”,即可生成客户端代码,在Testclient目录。

2.测试接口

将生成的客户端带拷贝到java或其他web工程的src下

建立测试java类,如Test.java,测试完成

public class Test {

    public static void main(String[] args) throws RemoteException,

           ServiceException, MalformedURLException

    {

       // BusinessServiceService为接口;BusinessServiceServiceLocator为接口实现类

       BusinessServiceService service = new BusinessServiceServiceLocator();

       // 设置要使用的接口服务地址:多用于动态调用不同服务器上的接口

       URL portAddress = new URL(

               "http://192.168.30.143:8080/webservice/services/businessService");

       BusinessService client = service.getbusinessService(portAddress);

 

       Request requestBean = new Request();

      

       // 传递简单类型

       requestBean.setMsg("wwww.servyou.com.cn");

      

       // 传递数组,最好先由list转化

       List<Integer> list = new ArrayList<Integer>();

       for (int i = 0; i < 10; i++)

       {

           list.add(i);

       }

       requestBean.setList(list.toArray());

      

       // 调用webservice

       Response resp = client.send(requestBean);

       System.out.println("消息已发送...");

 

       //接收返回结果

       int code = resp.getCode();

       String errorMsg = resp.getErrorMsg();

      

       Object[] integerList = resp.getList();

 

       System.out.println("收到消息:");

       System.out.println("错误码 code="+code);

       System.out.println("消息 errorMsg="+errorMsg);

       System.out.println("收到List<Integer>类型消息:list=" + Arrays.toString(integerList));

    }

}

 

 

服务端运行结果:

收到基本类型消息:msg=wwww.servyou.com.cn

收到List<Integer>类型消息:list=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

客户端运行结果:

消息已发送...

收到消息:

错误码 code=0

消息 errorMsg=调用成功

收到List<Integer>类型消息:list=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

posted @ 2013-10-18 16:12  嗨,你的益达~~~  阅读(4632)  评论(0编辑  收藏  举报