基于Myeclipse+Axis2的WebService开发实录

2.从下载Axis2包,下载 axis2-eclipse-codegen-wizard.zip,下载axis2-eclipse-service-archiver-wizard.zip

3.从下载(axis2-1.7.0-bin.zip,axis2-1.7.0-war.zip,axis2-eclipse-codegen-plugin-1.7.0.zip,axis2-eclipse-service-plugin-1.7.0.zip)

4.下载说明:从步骤1下载出来的Axis2_Codegen_Wizard_1.3.0文件夹,Axis2_Service_Archiver_1.3.0文件夹拷贝到myeclipse安装目录之dropins文件下,重启myeclipse下载到的axis2-1.7.0-war.zip,拷贝文件之tomact/webapps目录下面,重命名axis2.war,刷新文件夹,tomact为解压该部署包,此时可以通过tomact服务访问axis2站,看到相关页面表示成功

5.在步骤4中,注意文件夹axis2\WEB-INF,下面有3个文件夹,conf,modules,services,lib文件夹,稍后我们会用到

6.下面借助于axis2来开发webservice

7.打开myeclipse新建web project项目,将步骤5中conf,modules,services,lib文件夹的拷贝之WebRoot/WEB-INF/下面,并在WEB-INF下面新建Web.xml文件,倘若web.xml存在则打开,添加以下代码配置axis2

8.axis2配置代码如下:

<!-- 加载Axis2 --> 

<servlet> 

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

<servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class> 

<load-on-startup>1</load-on-startup> 

</servlet> 

<servlet-mapping> 

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

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

</servlet-mapping> 

9.在WEB-INF/services/下新建Axis2Service/META-INF/目录,新建文件services.xml,添加如下webservice服务配置代码

<!-- HelloWorld表示您的WebService服务名 -->

<service >

<!-- HelloWorld表示您的WebService服务描述 -->

<description> 

HelloWorld  Service Example 

</description> 

<!-- 这个必须是这个服务的类路径 -->

<parameter>     

cn.homily.action.HelloWorld

</parameter> 

<!-- 这个是这个服务的方法名 -->

<operation>   

<messageReceiver />   

</operation>   

<operation>   

<messageReceiver />   

</operation>   

<operation>   

<!-- 这里要注意,当没有返回值时才用 

org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver,

没有参数还是用RPCMessageReceiver--> 

<messageReceiver />   

</operation>

</service>

10.在src包下面添加子包cn.homily.action,添加HelloWorld.java文件,其代码如下:

package cn.homily.action;

//服务名

public class HelloWorld

{<br>  //服务方法

public String getHello(String name)

{

return "Hello, " + name + ".";

}

//服务方法

public String getWorld(String name)

{

return "World," + name + ".";

}

//服务方法

public String getHelloWorld()

{

return "Hello,World";

}

}

经过以上步骤,我们的WebService基本代码已经全了,现在看看实际效果。

11.现在打包我们的Web-Project,部署我们的Web-Project至tomact,现在我们运行,在浏览器输入如下地址:8080/sayHello2Axis/services/HelloWorld?wsdl。如果在浏览器里面看到了以下代码,说明我们的Web-Service服务Demo开发完成。正确的页面效果如下:

This XML file does not appear to have any style information associated with it. The document tree is shown below.

<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:ns="http://action.homily.cn" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" targetNamespace="http://action.homily.cn">

<wsdl:documentation>HelloWorld</wsdl:documentation>

<wsdl:types>...</wsdl:types>

<wsdl:message>

<wsdl:part element="ns:getHello"/>

</wsdl:message>

<wsdl:message>

<wsdl:part element="ns:getHelloResponse"/>

</wsdl:message>

<wsdl:message>

<wsdl:part element="ns:getHelloWorld"/>

</wsdl:message>

<wsdl:message>

<wsdl:part element="ns:getHelloWorldResponse"/>

</wsdl:message>

<wsdl:message>

<wsdl:part element="ns:getWorld"/>

</wsdl:message>

<wsdl:message>

<wsdl:part element="ns:getWorldResponse"/>

</wsdl:message>

<wsdl:portType>

<wsdl:operation>

<wsdl:input message="ns:getHelloRequest" wsaw:Action="urn:getHello"/>

<wsdl:output message="ns:getHelloResponse" wsaw:Action="urn:getHelloResponse"/>

</wsdl:operation>

<wsdl:operation>

<wsdl:input message="ns:getHelloWorldRequest" wsaw:Action="urn:getHelloWorld"/>

<wsdl:output message="ns:getHelloWorldResponse" wsaw:Action="urn:getHelloWorldResponse"/>

</wsdl:operation>

<wsdl:operation>

<wsdl:input message="ns:getWorldRequest" wsaw:Action="urn:getWorld"/>

<wsdl:output message="ns:getWorldResponse" wsaw:Action="urn:getWorldResponse"/>

</wsdl:operation>

</wsdl:portType>

<wsdl:binding type="ns:HelloWorldPortType">

<soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>

<wsdl:operation>

<soap:operation soapAction="urn:getHello"/>

<wsdl:input>

<soap:body use="literal"/>

</wsdl:input>

<wsdl:output>

<soap:body use="literal"/>

</wsdl:output>

</wsdl:operation>

<wsdl:operation>

<soap:operation soapAction="urn:getHelloWorld"/>

<wsdl:input>

<soap:body use="literal"/>

</wsdl:input>

<wsdl:output>

<soap:body use="literal"/>

</wsdl:output>

</wsdl:operation>

<wsdl:operation>

<soap:operation soapAction="urn:getWorld"/>

<wsdl:input>

<soap:body use="literal"/>

</wsdl:input>

<wsdl:output>

<soap:body use="literal"/>

</wsdl:output>

</wsdl:operation>

</wsdl:binding>

<wsdl:binding type="ns:HelloWorldPortType">

<soap12:binding transport="http://schemas.xmlsoap.org/soap/http"/>

<wsdl:operation>

<soap12:operation soapAction="urn:getHello"/>

<wsdl:input>

<soap12:body use="literal"/>

</wsdl:input>

<wsdl:output>

<soap12:body use="literal"/>

</wsdl:output>

</wsdl:operation>

<wsdl:operation>

<soap12:operation soapAction="urn:getHelloWorld"/>

<wsdl:input>

<soap12:body use="literal"/>

</wsdl:input>

<wsdl:output>

<soap12:body use="literal"/>

</wsdl:output>

</wsdl:operation>

<wsdl:operation>

<soap12:operation soapAction="urn:getWorld"/>

<wsdl:input>

<soap12:body use="literal"/>

</wsdl:input>

<wsdl:output>

<soap12:body use="literal"/>

</wsdl:output>

</wsdl:operation>

</wsdl:binding>

<wsdl:binding type="ns:HelloWorldPortType">

<http:binding verb="POST"/>

<wsdl:operation>

<http:operation location="getHello"/>

<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:operation>

<http:operation location="getHelloWorld"/>

<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:operation>

<http:operation location="getWorld"/>

<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>

<wsdl:port binding="ns:HelloWorldSoap11Binding">

<soap:address location="http://zgc-20150226yxm:8080/sayHello2Axis/services/HelloWorld.HelloWorldHttpSoap11Endpoint/"/>

</wsdl:port>

<wsdl:port binding="ns:HelloWorldSoap12Binding">

<soap12:address location="http://zgc-20150226yxm:8080/sayHello2Axis/services/HelloWorld.HelloWorldHttpSoap12Endpoint/"/>

</wsdl:port>

<wsdl:port binding="ns:HelloWorldHttpBinding">

<http:address location="http://zgc-20150226yxm:8080/sayHello2Axis/services/HelloWorld.HelloWorldHttpEndpoint/"/>

</wsdl:port>

</wsdl:service>

</wsdl:definitions>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/1a0787e014507a2b48f1ce29c3a6d46c.html