Axis2 WebService的发布和调用说明

来源:互联网 发布:java开发培训学费 编辑:程序博客网 时间:2024/06/05 06:49

准备工作

1、下载:axis2-1.5.4-bin.zip,axis2-1.5.4-war.zip 下载地址:http://axis.apache.org/axis2/java/core/
2、环境变量设置
AXIS2_HOME E:\research\axis2-1.5.4-bin\axis2-1.5.4
JAVA_HOME C:\Program Files\Java\jdk1.6.0_21
3
axis2-1.5.4-war.zip解压,将压缩包内的axis2.war部署到%TOMCAT-HOME%/webapps下,启动tomcat,访问http://localhost:8085/axis2/看是否正常。

ServiceService列表面,当前只有一个Versionhttp://localhost:8085/axis2/services/Version?wsdl
4
、下EclipseAxis2插件: axis2-eclipse-codegen-plugin-1.5.4.zip,axis2-eclipse-service-plugin-1.5.4.zip 后将plugins 复制到%ECLIPSE_HOME%\plugins
http://www.apache.org/dyn/mirrors/mirrors.cgi/axis/axis2/java/core/1.5.4/axis2-eclipse-codegen-plugin-1.5.4.zip
http://www.apache.org/dyn/mirrors/mirrors.cgi/axis/axis2/java/core/1.5.4/axis2-eclipse-service-plugin-1.5.4.zip

安装完插件后,IDE选择new->other中看到“Axis2 Wizards”,说明插件已经安装成功了。

 

AXIS2Web Services 
一、工程文件

1、新建名称为Axis2Service1 java工程。
2
、新建 \Axis2Service1\src\ws\TestWs.java

[java] view plaincopy
  1. package ws;  
  2. public class TestWs {  
  3.  public String showName(String name) {return name; }  
  4.  public String getName() {return "Axis2Service Sample"; }    
  5. }  


 

二、arr部署方式
1
、手打包
新建\Axis2Service1\deploy文件,将\Axis2Service1\bin下的class文件复制来。
新建\Axis2Service1\deploy\META-INF\services.xml文件

[html] view plaincopy
  1. <service name="AxisService">  
  2.  <description>AxisService</description>  
  3.  <parameter name="ServiceClass">ws.TestWs</parameter>  
  4.  <operation name="showName">  
  5.   <messageReceiver  
  6.    class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />  
  7.  </operation>  
  8.  <operation name="getName">  
  9.   <messageReceiver  
  10.    class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />  
  11.  </operation>  
  12. </service>  


 

生成aar \Axis2Service1\deploy>jar cvf AxisService.aar . (注意.)

2、插件打包
IDE
选择New->other->Axis2 Service Archiver,Next;
Class File Location:
选择Axis2Service1\bin,Next;
Skip WSDL,Next;
Service Archiver 
选择jar位置,如果没有jar包就直接点Next;
Generate the service xml automatically 生成service.xml file文件,Next
service name,
:AxisService,然后在class name 中填写要布的(全路径,如:ws.TestWs),load。勾Search declared methods only。点next

output File location,:D:\ ; output File Name,artiver文件的名称 AxisService。点finish
提示 Service Archvie generated successfully! 注册表明,生成成功。


3
AxisService
AxisService.aar
复制到%TOMCAT-HOME%/webapps/axis2/WEB-INF/services下。(不打aar包,\Axis2Service1\deploy下面复制去也是可以)

打开http://localhost:8085/axis2/services/listServices 就可以看到刚才发布的AxisService服务了,下面有两个函数:showName,getName

 

三、独立部署

1、新建java web project工程。
2
、文件复制
%TOMCAT-HOME%\webapps\axis2\WEB-INF\lib 
复制到 \Axis2Service2\WebRoot\WEB-INF\lib 下,并加入工程引用。
%TOMCAT-HOME%\webapps\axis2\WEB-INF\conf 
复制到 \Axis2Service2\WebRoot\WEB-INF\conf 
%TOMCAT-HOME%\webapps\axis2\WEB-INF\modules 
复制到 \Axis2Service2\WebRoot\WEB-INF\modules


3
web.xml 如下

[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app id="wmf" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"  
  3.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  5.  <servlet>  
  6.   <servlet-name>AxisServlet</servlet-name>  
  7.   <servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>  
  8.   <load-on-startup>1</load-on-startup>  
  9.  </servlet>  
  10.  <servlet-mapping>  
  11.   <servlet-name>AxisServlet</servlet-name>  
  12.   <url-pattern>/services/*</url-pattern>  
  13.  </servlet-mapping>  
  14. </web-app>  


 

2、新建 \Axis2Service2\src\ws\TestWs.java

[java] view plaincopy
  1. package ws;  
  2. public class TestWs {  
  3.  public String showName(String name) {return name; }  
  4.  public String getName() {return "Axis2Service Sample"; }    
  5. }  


3、新建\Axis2Service2\WebRoot\WEB-INF\services
4
、新建一个AxisService
AxisService\META-INF\services.xml

[html] view plaincopy
  1. <service name="AxisService">  
  2.  <description>AxisService</description>  
  3.  <parameter name="ServiceClass">ws.TestWs</parameter>  
  4.  <operation name="showName">  
  5.   <messageReceiver  
  6.    class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />  
  7.  </operation>  
  8.  <operation name="getName">  
  9.   <messageReceiver  
  10.    class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />  
  11.  </operation>  
  12. </service>  


 

tomcat后,访问http://localhost:8085/Axis2Service2/services/AxisService?wsdl看是否正常。

 AXIS2Web Services

一、客stub文件生成
1
、脚本生成方式
AXIS2的解bin(%AXIS2_HOME%\bin\)行下面
wsdl2java -uri http://localhost:8085/Axis2Service2/services/AxisService?wsdl -p ws -s -o stub
-p
参数指定了生成的Java的包名
-o
参数指定了生成的一系列文件保存的根目
stub\src\ws生成AxisServiceStub.java

2、插件生成方式
IDE
选择New->other->Axis2 Code Generator,Next;
Generate Java source code from a WSDL file,Next;
WSDL file location,
:http://localhost:8085/Axis2Service2/services/AxisService?wsdl,Next;
如果路径不会提示:Specified WSDL is invalid!, Please select a validated *.wsdl/*.xml file on previous page.
正确的,点next;

指定入路径,Next

提示:All operations completed successfully! 生成成功。在D:\src\ws 生成了stub一系列文件,其中ws是包名。

上面2种方式生成的stub有点不一,脚本生成方式是文件,插件生成方式生成的一系列文件。

二、客
脚本生成方式例子,插件生成的似。

1、新建 java工程 Axis2Client
新建\Axis2Client\lib文件
%AXIS2_HOME%\lib\ 下的所有jar包复制到\Axis2Client\lib,并加入工程引用中
将通脚本生成的AxisServiceStub.java文件加入到src\ws


2
、新建test.TestWs.java 主要代如下

 

[java] view plaincopy
  1. //初始化Sub类  
  2. AxisServiceStub stub = new AxisServiceStub();  
  3. //传递AxisServiceStub.ShowName对象,相关参数在这边赋值。  
  4. AxisServiceStub.ShowName command = new AxisServiceStub.ShowName();  
  5. command.setName("Hello!");  
  6. //取得返回值  
  7. String name = stub.showName(command).get_return();  
  8. System.out.println(name);  


 

 

用成功后控制台出:Hello!

 

 AXIS2REST Web Services

使用http://localhost:8086/Axis2Rest/services/AxisService/showName?name=rest的方式访问刚才发布成功的WebService从上面可以看出个就是restAxis1.0是无法通showName?name=rest取信息的。

2、使用axis

[java] view plaincopy
  1. public class TestRest {  
  2.    
  3.     private static String toEpr = "http://localhost:8086/Axis2Rest/services/AxisService";  
  4.      
  5.     public static void main(String[] args) throws AxisFault {  
  6.         Options options = new Options();  
  7.         options.setTo(new EndpointReference(toEpr));  
  8.          
  9.         //客户端REST方式调用服务跟普通服务的区别,REST调用必须加上下面这个代码。  
  10.         options.setProperty(Constants.Configuration.ENABLE_REST, Constants.VALUE_TRUE);  
  11.         ServiceClient sender = new ServiceClient();  
  12.         //axis2-1.5.4不需要下面这句代码,否则会报错  
  13.         //sender.engageModule(new QName(Constants.MODULE_ADDRESSING));  
  14.         sender.setOptions(options);  
  15.         OMElement result = sender.sendReceive(getPayload());  
  16.         try {  
  17.             XMLStreamWriter writer = XMLOutputFactory.newInstance()  
  18.                     .createXMLStreamWriter(System.out);  
  19.             result.serialize(writer);  
  20.             writer.flush();  
  21.         } catch (XMLStreamException e) {  
  22.             e.printStackTrace();  
  23.         } catch (FactoryConfigurationError e) {  
  24.             e.printStackTrace();  
  25.         }  
  26.     }  
  27.     private static OMElement getPayload() {  
  28.         OMFactory fac = OMAbstractFactory.getOMFactory();  
  29.         OMNamespace omNs = fac.createOMNamespace(  
  30.                 "http://ws""example1");  
  31.         OMElement method = fac.createOMElement("showName", omNs);  
  32.         OMElement value = fac.createOMElement("name", omNs);  
  33.         value.addChild(fac.createOMText(value, "Rest"));  
  34.         method.addChild(value);  
  35.         return method;  
  36.     }  


 

明:
1
sender.engageModule(new QName(Constants.MODULE_ADDRESSING)); axis2-1.5.4不需要下面句代,否报错

2、客REST方式用服跟普通服的区,就是Rest有下面个代
options.setProperty(Constants.Configuration.ENABLE_REST, Constants.VALUE_TRUE);
两者返回的数据都是

[html] view plaincopy
  1. <ns:showNameResponse xmlns:ns="Resthttp://ws"><ns:return>Rest</ns:return></ns:showNameResponse>  


 

3getPayload方法

OMNamespace omNs = fac.createOMNamespace("http://ws", "example1"); 指定命名空,如果没如下错误<faultstring>namespace mismatch require http://ws found http://ws1</faultstring>

OMElement method = fac.createOMElement("showName", omNs); 传递的方法名 "showName"

OMElement value = fac.createOMElement("name", omNs); 传递的参数name

value.addChild(fac.createOMText(value, "Rest"));  传递参数name值为Rest


注:转载http://feiyeguohai.iteye.com/blog/1575700

0 0
原创粉丝点击