Use the AXIS2 way to release the three ways of WebService

1. Download and installation of Axis2

First you can download the following two zip packages:
axis2-1.6.1-bin.zip
axis2-1.6.1-war.zip < br style="font-family:Arial; font-size:14px; line-height:26px"> where axis2- The 1.6.1-bin.zip file contains all the jar files in Axis2,
The axis2-1.6.1-war.zip file is used to publish the WebService to the Web container.
Unzip the axis2-1.6.1-war.zip file to the corresponding directory, and put the axis2.war file in the directory into the \webapps directory,

and start Tomcat, enter the following URL in the browser address bar:

http://localhost:8080/axis2/

If you see the homepage of axis2 The installation is successful.

2, simple pojo way (no configuration required):

In Axis2, you can directly publish a simple POJO as a WebService without any configuration. All public methods in POJO will be published as WebService methods.
The sample code is as follows:

[java] view plain copy

  1. publicclassHelloService{
  2. publicString sayHello(){
  3. return “hello”;
  4. < span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"> public String sayHelloToPerson (String name){
  5. if(name==null){
  6. name=“nobody”;
  7. }
  8. return “hello,”+name;
  9. }


After compiling the HelloService class, put the HelloService.class file in the \webapps\axis2\WEB-INF\pojo directory (if there is no pojo directory, create the directory) . Now we have successfully published the HelloService class as a WebService.
Enter the following URL in the browser address bar:

http://localhost:8080/axis2/services/listServices

Enter the following two URLs in the browser address bar to test the sayHelloToPerson and sayHello methods:
1.http: //localhost:8080/axis2/services/HelloService/sayHello
2.http://localhost:8080/axis2/services/HelloService/sayHelloToPerson? name=bill

The page displays the following results:

[html] view plain copy

  1. <ns:sayHelloToPersonResponsexmlns:ns< span style="margin:0px; padding:0px; border:none; background-color:inherit">=“http://ws.apache.org/axis2”>
  2. <return>hello,billreturn>
  3. < /ns:sayHelloToPersonResponse>


In writing, publishing and testing Pay attention to the following points in WebService:
1. POJO class cannot use package keyword to declare package.

2. Axis2 can hot publish WebService by default, that is, when copying the .class file of WebService to the pojo directory, Tomcat does not need to be restarted You can automatically publish WebService. If you want to cancel the hot release function of Axis2, you can open \webapps\axis2\WEB-INF\conf\axis2.xml,

found The configuration code is as follows:

[html] view plain cop y

  1. <pa rametername= “hotdeployment”>trueparameter>


change true to false. It should be noted that although Axis2 is a hot release by default, it is not a hot update. That is to say, once the WebService is successfully published, and if you want to update the WebService, you must restart Tomcat. This is very inconvenient for developers to debug WebService. Therefore, when developing WebService, you can set Axis2 as a hot update.
Found in the axis2.xml file

[html] view plain copy

  1. <parametername=“hotupdate”>falseparameter>

Change false to true.
3. When testing WebService in the browser, if the WebService method has Parameter, you need to use URL request parameters to specify the WebService method. The value of the parameter and the name of the request parameter must be consistent with the method parameter name. For example, to test the sayHelloToPerson method, the request parameter name should be name, as shown in the URL above.
4. The pojo directory for publishing WebService is only the default, if readers want To publish WebService in other directories, you can open the axis2.xml file and add the following sub-elements to the element:

[html] view plain copy

< /span>

  1. <deployerextension=“.class”directory=“my”class=“org. apache.axis2.deployment.POJODeployer”/>


< p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"> The above configuration It is allowed to publish WebService in the \webapps\axis2\WEB-INF\my directory. For example, copying HelloService.class in this example to the my directory can also be successfully published (but you need to delete SimpleService.class in the pojo directory, otherwise the WebService will have the same name).

pojo调用的另一篇实例文章

3、打jar包的方式:

用Axis2实现Web Service,虽然可以将POJO类放在axis2\WEB-INF\pojo目录中直接发布成Web Service,  这样做不需要进行任何配置,但这些POJO类不能在任何包中。这似乎有些不方便.  为此,Axis2也允许将带包的POJO类发布成Web Service。先实现一个POJO类,代码如下:

[jav a]  view plain  copy

  1. package com.sino soft.webservice;    
  2. public class HelloServiceNew {              
  3.     public String sayHelloNew(){    
  4.         return “hello”;    
  5.     }               
  6.     public String sayHelloToPersonNew(String name){         
  7.         if(name==null){    
  8.             name = “nobody”;    
  9.         }               
  10.         return “hello,”+name;    
  11.     }    
  12.     public void updateData(String data){    
  13.         System.out.println(data+” 已更新。 “);    
  14.     }    
  15. }    

要想将HelloServiceNew类发布成Web Service,需要一个services.xml文件(该文件要以UTF-8编码格式保存), 这个文件需要放在META-INF目录中,该文件的内容如下:

[html]  view plain  copy

  1. xml version=“1.0” encoding=“UTF-8”?>      
  2.       
  3. <service name=“HelloServiceNew” targetNamespace=“http://ws.apache.org/ax2”>      
  4.       
  5. <schema schemaNamesp ace=“http://sdjxd.com.cn”/>      
  6.       
  7. <description>Web Service实例一description>      
  8.       
  9. <parameter name=“ServiceClass”>com.sinosoft.webservice.HelloServiceNewparameter>      
  10. <messageReceivers>      
  11.       
  12. <messageReceiver mep=“http://www.w3.org/2004/08/wsdl/in-out”      
  13.     class=“org.apache.axis2.rpc.receivers.RPCMessageReceiver” />      
  14. <messageReceiver mep=“http://www.w3.org/2004/08/wsdl/in-only”      
  15.     class=“org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver”/>      
  16. messageReceivers>      
  17. service>      

其中元素用于发布Web Service,一个元素只能发布一个WebService类,name属性表示WebService名,如下面的URL可以获得这个WebService的WSDL内容:

  http://localhost:8080/axis2/services/HelloServiceNew?wsdl 

其中name属性名就是上面URL中”?”和”/”之间的部分。 元素表示当前Web Service的描述,元素用于设置WebService的参数, 在这里用于设置WebService对应的类名。 The most notable thing here is the element, which is used to set the handler for processing the WebService method.例如,sayHelloNew方法有一个返回值,因此,需要使用可处理输入输出的RPCMessageReceiver类,

而updateData方法没有返回值,因此,需要使用只能处理输入的RPCInOnlyMessageReceiver类。 To publish WebService in this way, it must be packaged into an .aar file, which is actually a .jar file with a changed extension. Now two files are created: HelloServiceNew.java and services.xml. Compile HelloServiceNew.java to generate HelloServiceNew.class.
services.xml和HelloServiceNew.class文件的位置如下:

D:\ws\ com\sinosoft\webservice\HelloServiceNew.class
D:\ws\META-INF\services.xml

在windows控制台中进入ws目录,并输入如下的命令生成.aar文件.
 
jar cvf ws.aar .(注意:最后面是空格+小数点)

实际上,.jar文件也可以发布webservice,但axis2官方文档中建议使用.a ar文件发布webservice.最后将ws.aar文件复制到\webapps\axis2\WEB-INF\services目录中,启动Tomcat后,就可以调用这个WebService了。另外services.xml文件中也可以直接指定WebService类的方法,如可以用下面的配置代码来发布WebService

[html]  view plain  copy

  1. <service name=” HelloServiceNew “>    
  2. <description>    
  3.     Web Service例子    
  4. description>    
  5. <parameter name=“ServiceClass”>    
  6.     com.sinosoft.webservice.HelloServiceNew      
  7. parameter>    
  8. <operation name=“sayHello”>    
  9.     <messageReceiver class=“org.apache.axis2.rpc.receivers.RPCMessageReceiver”/>    
  10. operation>    
  11. <operation name=“updateData”>    
  12.     <messageReceiver    
  13.         class=“org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver”/>    
  14.     operation>    
  15. service>   

      上面的配置代码前面的部分和以前的 services.xml文件的内容相同,但后面使用了 元素来指定每一个 WebService方法,并单独指定了处理每一个方法的处理器.对于客户端来说,调用使用这两个 services.xml文件,发布的 WebService并没有太大我区别,只是使用第二个 services.xml文件发布 WebServices后,在使用 wsdl2java命令,或使用 C#、 delphi等生成客户端的 stub时, update方法的 String类型被封装在了 update类中,在传递 update方法的参数时需要建立 update类的对象实例。而使用第一个services.xml文件发布的 WebService在生成 stub时直接可以为update方法传递 String类型的参数。从这一点可以看出,这两种方法生成的 WSDL有一定的区别。但实际上,如果客户端程序使用第一个 services.xml文件发布的 WebService生成 stub类时(这时 update方法的参数是 String),在服务端又改为第二个 services.xml文件来发布WebService,这时客户端并不需要再重新生成 stub类,而可以直接调用 update方法。也就是说,服务端使用什么样的方式发布 WebService,对客户端并没有影响。

如果想发布多个WebService,可以使用元素 

[html]  view plain  copy

  1. <serviceGroup>    
  2. <service name=“myService1”>    
  3.     …    
  4. service>    
  5. <service name=“myService2”>    
  6.     …    
  7. service>    
  8. serviceGroup>  

中间省略的代码同上面services.xml文件的配置。

4、不打jar包的方式:(重点)

有一个最简单的方法就是把axis2.war中的内容作为Web Project的基础, 来进行开发.

不过为了更清楚的了解如何在一个已有的Web Project中嵌入axis2, 那就手动来配置。大致分如下几个步骤:

一、新建Web Project,名为“WebServiceDemo”

二、下载axis2-1.6.1-war.zip包,解压缩

将axis2/WEB-INF/lib 里的jar包拷贝到 WebServiceDemo/WebRoot/WEB-INF/lib/
将axis2.war/axis2-web拷贝至WebServiceDemo/ WebRoot/axis2-web/

三、配置axis2 servlet

打开WebServiceDemo/WebRoot/WEB-INF/web.xml,增加如下配置:

[html]  view plain  copy

  1. <servlet>      
  2.     <servlet-name>AxisServletservlet-name>      
  3.     <servlet-class>org.apache.axis2.transport.http.AxisServletservlet-class>      
  4.     <load-on-startup>1load-on-startup>      
  5. servlet>           
  6. <servlet-mapping>      
  7.            <servlet-name>AxisServletservlet-name>      
  8.            <url-pattern>/services/*url-pattern>      
  9. servlet-mapping>    


四、写一个简单的web服务类

[java]  view plain  copy

  1. package service; //注意包名.类名与service.xml中的配置路径一致  
  2. public class MyService {        
  3.               public String sayHello(String name) {    
  4.                      return name + “says /”Hello!/“”;    
  5.               }           
  6. }   

五、配置Web Service.

由于axis2已嵌入到WebServiceDemo项目中,所以web service就不用打包成aar,而是直接在/WEB-INF目录下创建相应的文件夹和services.xml,目录结构如下图:

六、services.xml

[html]  view plain  copy

  1. xml version=“1.0” encoding=“UTF-8”?>    < /span>
  2. <serviceGroup>    
  3.        <service name=“myService”>    
  4.               <description>Web Service例子description>    
  5.               <parameter name=“ServiceClass”>service.MyServiceparameter>    
  6.               <messageReceivers>    
  7.                      <messageReceiver mep=“http://www.w3.org/2004/08/wsdl/in-out” class=“org.apache.axis2.rpc.receivers.RPCMessageReceiver” />    
  8.                      <messageReceiver mep=“http://www.w3.org/2004/08/wsdl/in-only” class=“org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver” />    
  9.               messageReceivers>    
  10.        service>    
  11. serviceGroup>  

在这里最值得注意的是元素,该元素用于设置处理WebService方法的处理器。

例如,getGreeting方法有一个返回值,因此,需要使用可处理输入输出的RPCMessageReceiver类, 而update方法没有返回值,因此,需要使用只能处理输入的RPCInOnlyMessageReceiver类。

七、浏览器访问

在浏览器中访问http://localhost:8080/WebServiceDemo/services/listServices

可以看到myService服务,说明服务已部署成功
输入http://localhost:8080/WebServiceDemo/services/myService?wsdl
       可以查看到该Web服务的描述文件

其实,axis2-web下面的东西可以不要那么多,我就只保留了index.jsp,httpbase.jsp和listServices.jsp这三个文件,这样集成后的工程就瘦身了

原文地址:http://m.blog.csdn.net/article/details?id=54663605
其他参考:http://m.blog.csdn.net/article/details?id=5627993

package service;

[java]  view plain  copy

  1. public class HelloService {     
  2.     public String sayHello(){    
  3.         return “hello”;    
  4.     }       
  5.     public String sayHelloToPerson(String name){            
  6.         if(name==null){    
  7.             name = “nobody”;    
  8.         }    
  9.         return “hello,”+name;    
  10.     }    
  11. }   

[java]  view plain  copy

[java]  view plain  copy

[html]  view plain  copy

  1. <ns:sayHelloToPersonResponse xmlns:ns=“http://ws.apache.org/axis2”>    
  2.     <return>hello,billreturn>     
  3. ns:sayHelloToPersonResponse>   

[html]  view plain  copy

[html]  view plain  copy

[html]  view plain  copy

  1. <parameter name=“hotdeployment”>trueparameter>    

[html]  view plain  copy

[html]  view plain  copy

[html]  view plain  copy

  1. <parameter name=“hotupdate”>falseparameter>   

[html]  view plain  copy

[html]  view plain  copy

[html]  view plain  copy

  1. <deployer extension=“.class” directory=“my” class=“org.apache.axis2.deployment.POJODeployer”/>   

[html]  view plain  copy

[html]  view plain  copy

[java]  view plain  copy

  1. package com.sinosoft.webservice;    
  2. public class HelloServiceNew {              
  3.     public String sayHelloNew(){    
  4.         return “hello”;    
  5.     }               
  6.     public< span style="margin:0px; padding:0px; border:none; background-color:inherit"> String sayHelloToPersonNew(String name){         
  7.         if(name==null){    
  8.             name = “nobody”;    
  9.         }               
  10.         return “hello,”+name;    
  11.     }    
  12.     public void updateData(String data){    
  13.         System.out.println(data+” 已更新。 “);    
  14.     }    
  15. }    

[java]  view plain  copy

[java]  view plain  copy

[html]  view plain  copy

  1. xml version=“1.0” encoding=“UTF-8”?>      
  2.       
  3. <service name=“HelloServiceNew” targetNamespace=“http://ws.apache.org/ax2”>      
  4.       
  5. <schema schemaNamespace=“http://sdjxd.com.cn”/>      
  6.       
  7. <description>Web Service实例一description>      
  8.       
  9. <parameter name=“ServiceClass” >com.sinosoft.webservice.HelloServiceNewparameter>      
  10. <messageReceivers>      
  11.       
  12. <messageReceiver mep=“http://www.w3.org/2004/08/wsdl/in-out”      
  13.     class=“org.apache.axis2.rpc.receivers.RPCMessageReceiver” />      
  14. <messageReceiver mep=“http://www.w3.org/2004/08/wsdl/in-only”      
  15.     class=“org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver”/>      
  16. messageReceivers>      
  17. service>      

[html]  view plain  copy

[html]  view plain  copy

[html]  view plain  copy

  1. <service name=” HelloServiceNew “>    
  2. <description>    
  3.     Web Service例子    
  4. description>    
  5. <parame ter name=“ServiceClass”>    
  6.     com.sinosoft.webservice.HelloServiceNew      
  7. parameter>    
  8. <operation name=“sayHello”>    
  9.     <messageReceiver class=“org.apache.axis2.rpc.receivers.RPCMessageReceiver”/>    
  10. operation>    
  11. <operation name=“updateData”>    
  12.     <messageReceiver    
  13.         class=“org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver”/>    
  14.     operation>    
  15. service>   

[html]  view plain  copy

[html]  view plain  copy

[html]  view plain  copy

  1. <serviceGroup>    
  2. <service name=“myService1”>    
  3.     …    
  4. service>    
  5. <service name=“myService2”>    
  6.     …    
  7. service>    
  8. serviceGroup>  

[html]  view plain  copy

[html]  view plain  copy

[html]  view plain  copy

  1. <servlet>      
  2.     <servlet-name>AxisServletservlet-name>      
  3.     <servlet-class>org.apache.axis2.transport.http.AxisServletservlet-class>      
  4.     <load-on-startup>1load-on-startup>      
  5. servlet>           
  6. <servlet-mapping>      
  7.            <servlet-name>AxisServletservlet-name>      
  8.            <url-patt ern>/services/*url-pattern>      
  9. servlet-mapping>    

[html]  view plain  copy

[html]  view plain  copy

[java]  view plain  copy

  1. package service; //注意包名.类名与service.xml中的配置路径一致  
  2. public class MyService {        
  3.               public String sayHello(String name) {    
  4.                      return name + “says /”Hello!/“”;    
  5.               }           
  6. }   

[java]  view plain  copy

  1. package service; //注意包名.类名与service.xml中的配置路径一致  
  2. public class MyService {        
  3.               public String sayHello(String name) {    
  4.                      return name + “says /”Hello!/“”;    
  5.               }           
  6. }   

[java]  view plain  copy

[java]  view plain  copy

[html]  view plain  copy

  1. xml version=“1.0” encoding=“UTF-8”?>    
  2. <serviceGroup>    
  3.        <service name=“myService”>    
  4.               <description>Web Service例子description>    
  5.               <parameter name=“ServiceClass”>service.MyServiceparameter>    
  6.               <messageReceivers>    
  7.                      <messageReceiver mep=“http://www.w3.org/2004/08/wsdl/in-out” class=“org.apache.axis2.rpc.receivers.RPCMessageReceiver” />    
  8.                      <messageReceiver mep=“http://www.w3.org/2004/08/wsdl/in-only” class=“org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver” />    
  9.               messageReceivers>    
  10.        service>    
  11. serviceGroup>  

[html]  view plain  copy

[html]  view plain  copy

package service;

Leave a Comment

Your email address will not be published.