本实验主要采用myEclipse10+axis2+tomcat6.0发布WebService。
一、配置步骤如下:
tomcat和jdk的安装就不再介绍,本文要在tomcat、jdk、MyEclipse10.10已经装完的前提下安装。
1.下载axis2
可以从如下的网址下载Axis2的最新版本:
因为电脑上装的jdk为1.6.0,所以下载的axis2为1.6.0。读者可以下载如下两个zip包:
axis2-1.6.0-bin.zip
axis2-1.6.0-war.zip
其中axis2-1.6.0-bin.zip文件中包含了Axis2中所有的jar文件, axis2-1.6.0-war.zip文件用于将WebService发布到Web容器中。
将axis2-1.6.0-war.zip文件解压到相应的目录,将目录中的axis2.war文件放到<Tomcat安装目录>\webapps目录中(本文使用的Tomcat的版本是6.x),并启动Tomcat。
在浏览器地址栏中输入如下的URL:
如果在浏览器中显示出如图1所示的页面,则表示Axis2安装成功。
图1
2.MyEclipse10 Axis2插件
1.下载 MyEclipse10 Axis2 插件,http://archive.apache.org/dist/ws/axis2/tools/1_4_1/
下载 axis2-eclipse-codegen-wizard.zip 和 axis2-eclipse-service-archiver-wizard.zip
2.解压插件 ($ECLIPSE_HOME 表示安装的 myEclipse10 主目录 )
将上述两个插件都解压到 $ECLIPSE_HOME\eclipse\plugins 目录中;
Axis2_Codegen_Wizard_1.3.0 和Axis2_Service_Archiver_1.3.0
3. 在 $ECLIPSE_HOME\eclipse\links目录下增加文件 axis-eclipse-plugin.link
写入 path= $ECLIPSE_HOME\eclipse\plugins
4. 重新启动 myEclipse10,在 file->new->other 中即可看到 Axis2 Wizards ,至此, axis2 插件安装成功!!
二、构建服务
在开发环境及 Axis2 环境搭建好后,我们便可着手 Web Services 服务的开发:
1.建立要发布的WebService
(1)在 myEclispse 中添加一个用户库命名为axis2 ,将 axis2\lib 下的包均添加进来。这一步操作的目的是为了以后工程添加 axis2 所需的 jar 文件方便。
(建立用户库的方法:在MyEclipse10的菜单栏中,依次单击“窗口”→“首选项”菜单项,将打开“首选项”对话框。在该对话框中,依次展开“Java”→“构建路径”→“用户库”节点,Add即可)
(2) 建立一个 JavaProject 命名为 MyWS ,将 axis2 用户库加入到 buildpath 中。本项目就是准备发布成webservice的项目。
(3) 现在开始编写要发布的 WebSevice ,在 src 中建包cqu.ws ,建立 IntegralCal类:本类的作用是计算函数ln((1+x)/(1+x^2))在给定区间上的定积分。
package cqu.ws; import java.math.BigDecimal;
public class IntegralCal {
private static final int N=10000; private double function(double x) { double y=0.0; y=Math.log(MathUtils.div(1+x, 1+x*x)); return y;
} public double integral(double a,double b) { double h=MathUtils.div(b-a, N); double sum=0.0,x=0.0; for (int i=0;i<N;i++) { x=MathUtils.add(a, MathUtils.mul(i, h)); sum=MathUtils.add(sum, MathUtils.mul(function(x), h)); } return sum; } }
abstract class MathUtils {
// 默认余数长度 private static final int DIV_SCALE = 10;
public static double div(double value1, double value2) {
BigDecimal big1 = new BigDecimal(String.valueOf(value1)); BigDecimal big2 = new BigDecimal(String.valueOf(value2)); return big1.divide(big2, DIV_SCALE, BigDecimal.ROUND_HALF_UP) .doubleValue(); }
// 受限于DOUBLE长度 public static double add(double value1, double value2) {
BigDecimal big1 = new BigDecimal(String.valueOf(value1)); BigDecimal big2 = new BigDecimal(String.valueOf(value2)); return big1.add(big2).doubleValue(); }
// 大数加法 public static double add(String value1, String value2) {
BigDecimal big1 = new BigDecimal(value1); BigDecimal big2 = new BigDecimal(value2); return big1.add(big2).doubleValue(); }
public static double sub(double value1, double value2) {
BigDecimal big1 = new BigDecimal(String.valueOf(value1)); BigDecimal big2 = new BigDecimal(String.valueOf(value2)); return big1.subtract(big2).doubleValue(); }
public static double returnMax(double value1, double value2) {
BigDecimal big1 = new BigDecimal(value1); BigDecimal big2 = new BigDecimal(value2); return big1.max(big2).doubleValue(); }
public static double mul(double value1, double value2) {
BigDecimal big1 = new BigDecimal(String.valueOf(value1)); BigDecimal big2 = new BigDecimal(String.valueOf(value2)); return big1.multiply(big2).doubleValue(); } }
|
2.发布Web Service
打包要发布的 Service , MyEclipse10 中 New --> File --> Other --> Axis2 wizards --> Axis2Services Archiver ,按照向导选择刚建立的类编译后的class 文件。
(1)选择 class 文件目录,注意,不是 java 源文件,而是 classes 目录,这里需要注意由于你的类是带包cqu.ws的,因此不要选到包这个目录。选到包的上级目录
(2)连按两次“Next>” ,选中“Generate the service xml automatically”
(3)按下一步,输入 service 名称和类名,我这里输入的服务名是:IntService;类名是我们刚刚写的类名:cqu.ws. IntegralCal,这里需要注意加入完整的包名。
(4) 按下一步,输入 service 文件的保存路径和文件名,完成。
选择生成目录为:
C:\ProgramFiles\Tomcat 7.0\webapps\axis2\WEB-INF\services,也可以选择其他目录,然后copy到这一目录下。
3.测试Web Service
通过http://localhost:8080/axis2/services/listServices可以看到已发布的服务。如下:
WSDL文档如下:
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:ns="http://ws.cqu" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl"xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" targetNamespace="http://ws.cqu"> <wsdl:documentation>IntService</wsdl:documentation> <wsdl:types> <xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://ws.cqu"> <xs:element name="integral"> <xs:complexType> <xs:sequence> <xs:element minOccurs="0" name="a" type="xs:double"/> <xs:element minOccurs="0" name="b" type="xs:double"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="integralResponse"> <xs:complexType> <xs:sequence> <xs:element minOccurs="0" name="return" type="xs:double"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> </wsdl:types> <wsdl:message name="integralRequest"> <wsdl:part name="parameters" element="ns:integral"/> </wsdl:message> <wsdl:message name="integralResponse"> <wsdl:part name="parameters" element="ns:integralResponse"/> </wsdl:message> <wsdl:portType name="IntServicePortType"> <wsdl:operation name="integral"> <wsdl:input message="ns:integralRequest" wsaw:Action="urn:integral"/> <wsdl:output message="ns:integralResponse" wsaw:Action="urn:integralResponse"/> </wsdl:operation> </wsdl:portType> <wsdl:binding name="IntServiceSoap11Binding" type="ns:IntServicePortType"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/> <wsdl:operation name="integral"> <soap:operation soapAction="urn:integral" style="document"/> <wsdl:input> <soap:body use="literal"/> </wsdl:input> <wsdl:output> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:binding name="IntServiceSoap12Binding" type="ns:IntServicePortType"> <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/> <wsdl:operation name="integral"> <soap12:operation soapAction="urn:integral" style="document"/> <wsdl:input> <soap12:body use="literal"/> </wsdl:input> <wsdl:output> <soap12:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:binding name="IntServiceHttpBinding" type="ns:IntServicePortType"> <http:binding verb="POST"/> <wsdl:operation name="integral"> <http:operation location="integral"/> <wsdl:input> <mime:content type="application/xml" part="parameters"/> </wsdl:input> <wsdl:output> <mime:content type="application/xml" part="parameters"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="IntService"> <wsdl:port name="IntServiceHttpSoap11Endpoint" binding="ns:IntServiceSoap11Binding"> <soap:address location="http://localhost:8080/axis2/services/IntService.IntServiceHttpSoap11Endpoint/"/> </wsdl:port> <wsdl:port name="IntServiceHttpSoap12Endpoint" binding="ns:IntServiceSoap12Binding"> <soap12:address location="http://localhost:8080/axis2/services/IntService.IntServiceHttpSoap12Endpoint/"/> </wsdl:port> <wsdl:port name="IntServiceHttpEndpoint" binding="ns:IntServiceHttpBinding"> <http:address location="http://localhost:8080/axis2/services/IntService.IntServiceHttpEndpoint/"/> </wsdl:port> </wsdl:service> </wsdl:definitions>
|
通过http://localhost:8080/axis2/services/IntService/integral?a=0&b=1可以查看效果。
三、编写客户端程序(java)
建立一个java project,建立一个包,里面写一个类IntWebTest,类里面写main函数,然后运行。
import javax.xml.namespace.QName;
import org.apache.axis2.AxisFault; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.rpc.client.RPCServiceClient;
public class IntWebTest {
/** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub
try {
RPCServiceClient serviceClient = new RPCServiceClient(); Options options = serviceClient.getOptions(); EndpointReference targetEPR = new EndpointReference( "http://localhost:8080/axis2/services/IntService/"); options.setTo(targetEPR); Object[] opAddEntryArgs = new Object[] { "0", "1" }; // 指定getGreeting方法返回值的数据类型的Class对象 Class[] classes = new Class[] { String.class }; // 指定要调用的getGreeting方法及WSDL文件的命名空间 QName opAddEntry = new QName("http://ws.cqu", "integral"); // 调用getGreeting方法并输出该方法的返回值 System.out.println(serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)[0]);
} catch (AxisFault e) { // TODO Auto-generated catch block e.printStackTrace(); }
}
}
|
运行结果如下: