最近在学习webservices,看官网说明,觉得挺简单,写篇文章供大家参考。//具体作用就不说了,大家都懂的。
到apache官网下载axis2http://axis.apache.org/axis2/java/core/download.cgi推荐下载
1.6.2 Release (Mirrored) | Binary Distribution zip |
具体步骤:
1.新建axis2 web工程。
拷贝解压文件下lib内的jar包到工程WEB-INF下的lib文件夹内。//效果如下图,下面说明
找到解压文件的webapp文件夹,拷贝目录下的axis2-web文件到axis2工程下的WebRoot根目录。在工程的WEB-INF下新建services文件夹(名字必须是services),在services下新建axis2文件夹(这个名字没有约束),再在下面新建META-INF文件夹,最后在META-INF下新建services.xml文件,内容如下:
<service name="axis2" scope="application" targetNamespace="http://quickstart.samples/"> <description> Stock Quote Service </description> <messageReceivers> <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only" class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/> <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/> </messageReceivers> <schema schemaNamespace="http://quickstart.samples/xsd"/> <parameter name="ServiceClass">com.jhb.TestAxis2</parameter> </service>
ps:com.jhb.TestAxis2是自己建立的java类,下面说明。
在web.xml文件内增加如下内容:
<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>
新建java类TestAxis2(包名是com.jhb),与<parameter name="ServiceClass">com.jhb.TestAxis2</parameter>里的内容一致。
在TestAxis2内提供一个方法:如下
public String testHello(String name){ return name; }
然后就是部署启动tomcat服务器。在地址栏里输入:http://localhost:8080/axis2/services/listServices回车/* axis2是工程名*/
出现如下界面说明启动成功。
图片中的axis2超链接是services.xml文件内的name名。testHello是是提供的方法名。点击axis2链接,进入一个页面,链接地址为http://localhost:8080/axis2/services/axis2?wsdl
网页内容为标准的xml文件内容。如下:
<?xml version="1.0" encoding="UTF-8"?> -<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:axis2="http://quickstart.samples/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:ns="http://quickstart.samples/xsd" 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://quickstart.samples/"> <wsdl:documentation>axis2</wsdl:documentation> -<wsdl:types> -<xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://quickstart.samples/xsd"> -<xs:element name="testHello"> -<xs:complexType> -<xs:sequence> <xs:element minOccurs="0" name="name" nillable="true" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:element> -<xs:element name="testHelloResponse"> -<xs:complexType> +<xs:sequence> <xs:element minOccurs="0" name="return" nillable="true" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> </wsdl:types> -<wsdl:message name="testHelloRequest"> <wsdl:part name="parameters" element="ns:testHello" /> </wsdl:message> -<wsdl:message name="testHelloResponse"> <wsdl:part name="parameters" element="ns:testHelloResponse" /> </wsdl:message> -<wsdl:portType name="axis2PortType"> -<wsdl:operation name="testHello"> <wsdl:input message="axis2:testHelloRequest" wsaw:Action="urn:testHello" /> <wsdl:output message="axis2:testHelloResponse" wsaw:Action="urn:testHelloResponse" /> </wsdl:operation> </wsdl:portType> -<wsdl:binding name="axis2Soap11Binding" type="axis2:axis2PortType"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" /> -<wsdl:operation name="testHello"> <soap:operation soapAction="urn:testHello" 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="axis2Soap12Binding" type="axis2:axis2PortType"> <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" /> -<wsdl:operation name="testHello"> <soap12:operation soapAction="urn:testHello" 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="axis2HttpBinding" type="axis2:axis2PortType"> <http:binding verb="POST" /> -<wsdl:operation name="testHello"> <http:operation location="axis2/testHello" /> -<wsdl:input> <mime:content type="text/xml" part="testHello" /> </wsdl:input> -<wsdl:output> <mime:content type="text/xml" part="testHello" /> </wsdl:output> </wsdl:operation> </wsdl:binding> -<wsdl:service name="axis2"> -<wsdl:port name="axis2HttpSoap11Endpoint" binding="axis2:axis2Soap11Binding"> <soap:address location="http://localhost:8080/axis2/services/axis2.axis2HttpSoap11Endpoint/" /> </wsdl:port> -<wsdl:port name="axis2HttpSoap12Endpoint" binding="axis2:axis2Soap12Binding"> <soap12:address location="http://localhost:8080/axis2/services/axis2.axis2HttpSoap12Endpoint/" /> </wsdl:port> -<wsdl:port name="axis2HttpEndpoint" binding="axis2:axis2HttpBinding"> <http:address location="http://localhost:8080/axis2/services/axis2.axis2HttpEndpoint/" /> </wsdl:port> </wsdl:service> </wsdl:definitions>
下面新建解析这个webservices的工程。
新建工程名为client的工程。添加jar包到classpath内(和上面一样)。新建一个Util类。内容如下。
i
mport org.apache.axis.wsdl.WSDL2Java; public class Util { public static void main(String[] args) { String [] s = new String[]{ "-u","http://localhost:8080/axis2/services/axis2?wsdl", "-o", "client", "-S", "false" }; /* -u是地址,就是上面点击链接生成的URL,-o是一会要输出的文件夹名字。 */ WSDL2Java.main(s); } }
运行这个main函数,刷新工程,会发现在工程下多了一个client文件夹。展开client文件夹,复制samples到src下。如下图
在cn.jhb包下新建Test类。内容如下:
public class Test { public static void main(String[] args) { try { //URL内的地址是刚才生成的,即上面的 - //<wsdl:port name="axis2HttpSoap11Endpoint" binding="axis2:axis2Soap11Binding"> //<soap:address location="http://localhost:8080/axis2/services/axis2.axis2HttpSoap11Endpoint/" /> //</wsdl:port> - //<wsdl:port name="axis2HttpSoap12Endpoint" binding="axis2:axis2Soap12Binding"> //<soap12:address location="http://localhost:8080/axis2/services/axis2.axis2HttpSoap12Endpoint/" /> // </wsdl:port> - //<wsdl:port name="axis2HttpEndpoint" binding="axis2:axis2HttpBinding"> ///<http:address location="http://localhost:8080/axis2/services/axis2.axis2HttpEndpoint/" /> // </wsdl:port> //提供了三个地址,任选一个即可。 Axis2Soap12BindingStub stub = new Axis2Soap12BindingStub(new URL("http://localhost:8080/axis2/services/axis2.axis2HttpSoap12Endpoint/"), null); String result = stub.testHello(" ni hao webservices"); System.out.println(result); } catch (AxisFault e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
运行这个main函数,发现在控制台输出ni hao webservices。
是不是觉得跨平台,跨项目,甚至跨语言编程不那么难了?具体细节自己找资料看吧。