WebService的编写发布,被广泛应用于SOA架构设计中,目前用的比较多的有 CXF,JAX-RPC,XFire,Axis2等Java服务开发框架,其中Axis2是其中的佼佼者。以下是中移动合同服务的服务端和客户端的开发实例:
开发软件以及版本下载,安装,配置
最基本的myeclipse,jdk,tomcat的下载,安装,不再介绍。
axis2在myeclipse中开发用到的插件和在tomcat中部署用到的插件下载
官网:http://axis.apache.org/axis2/java/core/download.cgi
点击Tools下载以下工具包
axis2-eclipse-codegen-plugin-1.6.2.zip
axis2-eclipse-service-plugin-1.6.2.zip
点击Releases下载以下axis2相关的war包,jar包,源码,以及开发说明书(见附件图片)
axis2-1.6.2-src.zip(源码包,可不下载,便于关联调试用)
myeclipse插件安装
将axis2-eclipse-codegen-plugin-1.6.2.zip和axis2-eclipse-service-plugin-1.6.2.zip解压
D:\Users\Administrator\AppData\Local\MyEclipse为我的myeclipse安装的根目录
将解压后的plugins中的jar包复制到myeclipse的安装目录下的common中的plugins中(只需jar包即可)如:
D:\Users\Administrator\AppData\Local\MyEclipse\Common\plugins
进入到myeclipse安装目录中的org.eclipse.equinox.simpleconfigurator中,如下:
D:\Users\Administrator\AppData\Local\MyEclipse\MyEclipse 8.6\configuration\org.eclipse.equinox.simpleconfigurator
用记事本打开org.eclipse.equinox.simpleconfigurator 中的bundles.info文件
项bundles.info文件中添加下面内容
org.apache.axis2.eclipse.codegen.plugin,1.6.2,file:/d:/Users/Administrator/AppData/Local/MyEclipse/Common/plugins/org.apache.axis2.eclipse.codegen.plugin_1.6.2.jar,4,false
org.apache.axis2.eclipse.service.plugin,1.6.2,file:/d:/Users/Administrator/AppData/Local/MyEclipse/Common/plugins/org.apache.axis2.eclipse.service.plugin_1.2.0.jar,4,false
启动myeclipse后 点击newother后如果能看到Axis2 Wizards文件夹下有两个选项表示安装成功。
将 axis2-1.6.2-war.zip解压后将axis2.war拷贝到tomcat的webapps中,启动tomcat
打开 http://localhost:8080/axis2 如果出现如下界面表示配置OK
现在,我们来根据已有的wsdl文件(这个wsdl文件是用 JDeveloper Studio 11.1.1.6.0开发提取的)
SB_SC_CMS_PageInquiryContractInfoSrv.wsdl 这个wsdl文件
找到我们刚才下载的 axis2-1.6.2-bin.zip 解压的目录,用cmd命令生成服务端和客户端代码。
例如我得目录是: E:\JAR\axis2-1.6.2\bin
生成服务端的java代码:E:\JAR\axis2-1.4.1\bin>wsdl2java -g -ss -sd -uri SB_SC_CMS_PageInquiryContractInfoSrv.wsdl的文件路径
生成客户端的java代码:E:\JAR\axis2-1.4.1\bin>wsdl2java -uri SB_SC_CMS_PageInquiryContractInfoSrv.wsdl的文件路径 -o src
服务端代码生成后在myeclipse中建相关的Java project项目,找到 SB_SC_CMS_PageInquiryContractInfoSrvSkeleton.java 进行数据装配,简单处理代码如下:
/**
* SB_SC_CMS_PageInquiryContractInfoSrvSkeleton.java
*
* This file was auto-generated from WSDL
* by the Apache Axis2 version: 1.6.2 Built on : Apr 17, 2012 (05:33:49 IST)
*/
package com.cmcc.mss.sb_sc_cms_pageinquirycontractinfosrv;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
/**
* SB_SC_CMS_PageInquiryContractInfoSrvSkeleton java skeleton for the axisService
*/
public class SB_SC_CMS_PageInquiryContractInfoSrvSkeleton{
/**
* Auto generated method signature
*
* @param sB_SC_CMS_PageInquiryContractInfoSrvRequest
* @return sB_SC_CMS_PageInquiryContractInfoSrvResponse
*/
public com.cmcc.mss.sb_sc_cms_pageinquirycontractinfosrv.SB_SC_CMS_PageInquiryContractInfoSrvResponseE process
(
com.cmcc.mss.sb_sc_cms_pageinquirycontractinfosrv.SB_SC_CMS_PageInquiryContractInfoSrvRequestE sB_SC_CMS_PageInquiryContractInfoSrvRequest
)
{
SB_SC_CMS_PageInquiryContractInfoSrvResponseE se=new SB_SC_CMS_PageInquiryContractInfoSrvResponseE();
SB_SC_CMS_PageInquiryContractInfoSrvResponse response=new SB_SC_CMS_PageInquiryContractInfoSrvResponse();
SB_SC_CMS_PageInquiryContractInfoSrvOutputCollection collection=new SB_SC_CMS_PageInquiryContractInfoSrvOutputCollection();
List<SB_SC_CMS_PageInquiryContractInfoSrvOutputItem> IItem=new ArrayList<SB_SC_CMS_PageInquiryContractInfoSrvOutputItem>();
SB_SC_CMS_PageInquiryContractInfoSrvOutputItem item=new SB_SC_CMS_PageInquiryContractInfoSrvOutputItem();
ContractLinesCollection contractLinesCollection=new ContractLinesCollection();
PaymentScheduleCollection paymentScheduleCollection=new PaymentScheduleCollection();
ExecutionLinesCollection executionLinesCollection=new ExecutionLinesCollection();
ContractLinesItem contractLinesItem=new ContractLinesItem();
contractLinesItem.setACTIVITY_CODE("11");
PaymentScheduleItem paymentScheduleItem=new PaymentScheduleItem();
paymentScheduleItem.setACTIVITY_CODE("1111");
ExecutionLinesItem executionLinesItem=new ExecutionLinesItem();
executionLinesItem.setTAX_AMOUNT(BigDecimal.valueOf(1l));
contractLinesCollection.setContractLinesItem(new ContractLinesItem[]{contractLinesItem});
paymentScheduleCollection.setPaymentScheduleItem(new PaymentScheduleItem[]{paymentScheduleItem});
executionLinesCollection.setExecutionLinesItem(new ExecutionLinesItem[]{executionLinesItem});
item.setBUYER_ID("11");
item.setEXECUTION_LINES(executionLinesCollection);
item.setPAYMENT_SCHEDULE(paymentScheduleCollection);
item.setCONTRACT_LINES(contractLinesCollection);
IItem.add(item);
collection.setSB_SC_CMS_PageInquiryContractInfoSrvOutputItem(IItem.toArray(new SB_SC_CMS_PageInquiryContractInfoSrvOutputItem[IItem.size()]));
response.setErrorFlag("Y");
response.setSB_SC_CMS_PageInquiryContractInfoSrvOutputCollection(collection);
se.setSB_SC_CMS_PageInquiryContractInfoSrvResponse(response);
return se;
//throw new java.lang.UnsupportedOperationException("Please implement " + this.getClass().getName() + "#process");
}
}
然后将服务端代码导成jar包
然后把你的jar文件打开,把生成的服务端代码里面services文件夹下的 services.xml以及用来开发的SB_SC_CMS_PageInquiryContractInfoSrv.wsdl同时拷贝到jar里面的META-INF文件夹下。将inquiry.jar 放入如下路径
E:\JAR\apache-tomcat-7.0.12\webapps\axis2\WEB-INF\services 启动tomcat打开axis2的地址,点开Services,会发现我们部署的服务。
点开即是我们发布的服务wsdl地址了。
http://localhost:8080/axis2/services/SB_SC_CMS_PageInquiryContractInfoSrv?wsdl
然后我们可以在soapUI中测试我们发布的服务,验证是否能返回我们赋值的数据,如下:
下面我们用客户端对我们发布的服务进行调用,将我们生成的客户端代码放在一个Java project中,创建一个测试类,SB_SC_CMS_PageInquiryContractInfoSrvStub.java 结尾的类是接口类,简单进行测试:
import java.rmi.RemoteException;
import com.cmcc.mss.msgheader.MsgHeader;
import com.cmcc.mss.sb_sc_cms_pageinquirycontractinfosrv.SB_SC_CMS_PageInquiryContractInfoSrv;
import com.cmcc.mss.sb_sc_cms_pageinquirycontractinfosrv.SB_SC_CMS_PageInquiryContractInfoSrvRequest;
import com.cmcc.mss.sb_sc_cms_pageinquirycontractinfosrv.SB_SC_CMS_PageInquiryContractInfoSrvRequestE;
import com.cmcc.mss.sb_sc_cms_pageinquirycontractinfosrv.SB_SC_CMS_PageInquiryContractInfoSrvStub;
public class Test {
public static void main(String[] args) throws RemoteException {
SB_SC_CMS_PageInquiryContractInfoSrv contract=new SB_SC_CMS_PageInquiryContractInfoSrvStub();
SB_SC_CMS_PageInquiryContractInfoSrvRequestE request=new SB_SC_CMS_PageInquiryContractInfoSrvRequestE();
SB_SC_CMS_PageInquiryContractInfoSrvRequest re=new SB_SC_CMS_PageInquiryContractInfoSrvRequest();
re.setPROJECT_NUMBER("111");
MsgHeader msgHeader=new MsgHeader();
msgHeader.setUSERID("1111");
re.setMsgHeader(msgHeader);
request.setSB_SC_CMS_PageInquiryContractInfoSrvRequest(re);
System.out.println(contract.process(request).getSB_SC_CMS_PageInquiryContractInfoSrvResponse().getErrorFlag());
System.out.println(contract.process(request).getSB_SC_CMS_PageInquiryContractInfoSrvResponse().getSB_SC_CMS_PageInquiryContractInfoSrvOutputCollection().getSB_SC_CMS_PageInquiryContractInfoSrvOutputItem()[0].getBUYER_ID());
}
}