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
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:
- publicclassHelloService{
- publicString sayHello(){
- return “hello”;
- < span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"> public String sayHelloToPerson (String name){
- if(name==null){
- name=“nobody”;
- }
- return “hello,”+name;
- }
After compiling the HelloService class, put the HelloService.class file in the
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:
- <ns:sayHelloToPersonResponsexmlns:ns< span style="margin:0px; padding:0px; border:none; background-color:inherit">=“http://ws.apache.org/axis2”>
- <return>hello,billreturn>
- < /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
found The configuration code is as follows:
div>
- <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
- <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
< /span>
- <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
pojo调用的另一篇实例文章
3、打jar包的方式:
用Axis2实现Web Service,虽然可以将POJO类放在axis2\WEB-INF\pojo目录中直接发布成Web Service, 这样做不需要进行任何配置,但这些POJO类不能在任何包中。这似乎有些不方便. 为此,Axis2也允许将带包的POJO类发布成Web Service。先实现一个POJO类,代码如下:
- package com.sino soft.webservice;
- public class HelloServiceNew {
- public String sayHelloNew(){
- return “hello”;
- }
- public String sayHelloToPersonNew(String name){
- if(name==null){
- name = “nobody”;
- }
- return “hello,”+name;
- }
- public void updateData(String data){
- System.out.println(data+” 已更新。 “);
- }
- }
要想将HelloServiceNew类发布成Web Service,需要一个services.xml文件(该文件要以UTF-8编码格式保存), 这个文件需要放在META-INF目录中,该文件的内容如下:
- span>xml version=“1.0” encoding=“UTF-8”?>
- <service name=“HelloServiceNew” targetNamespace=“http://ws.apache.org/ax2”>
- <schema schemaNamesp ace=“http://sdjxd.com.cn”/>
- <description>Web Service实例一description>
- <parameter name=“ServiceClass”>com.sinosoft.webservice.HelloServiceNewparameter>
- <messageReceivers>
- <messageReceiver span> mep=“http://www.w3.org/2004/08/wsdl/in-out”
- class=“org.apache.axis2.rpc.receivers.RPCMessageReceiver” />
- <messageReceiver mep=“http://www.w3.org/2004/08/wsdl/in-only”
- class=“org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver”/>
- messageReceivers>
- service>
其中
http://localhost:8080/axis2/services/HelloServiceNew?wsdl
其中name属性名就是上面URL中”?”和”/”之间的部分。
而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文件复制到
- <service name=” HelloServiceNew “>
- <description>
- Web Service例子
- description>
- <parameter name=“ServiceClass”>
- com.sinosoft.webservice.HelloServiceNew
- parameter>
- <operation name=“sayHello”>
- <messageReceiver class=“org.apache.axis2.rpc.receivers.RPCMessageReceiver”/>
- operation>
- <operation name=“updateData”>
- <messageReceiver
- class=“org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver”/>
- operation>
- service>
上面的配置代码前面的部分和以前的 services.xml文件的内容相同,但后面使用了
如果想发布多个WebService,可以使用
-
<serviceGroup> - <service name=“myService1”>
- …
- service>
- <service name=“myService2”>
- …
- service>
- 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,增加如下配置:
- <servlet>
- <servlet-name>AxisServletservlet-name>
- <
servlet-class >org.apache.axis2.transport.http.AxisServletservlet-class> - <load-on-startup>1load-on-startup>
- servlet>
- <servlet-mapping>
- <servlet-name>AxisServletservlet-name>
- <url-pattern>/services/*url-pattern>
- servlet-mapping>
四、写一个简单的web服务类
- package service; //注意包名.类名与service.xml中的配置路径一致
- public class MyService {
- public String sayHello(String name) {
- return name + “says /”Hello!/“”;
- }
- }
五、配置Web Service.
由于axis2已嵌入到WebServiceDemo项目中,所以web service就不用打包成aar,而是直接在/WEB-INF目录下创建相应的文件夹和services.xml,目录结构如下图:
六、services.xml
- xml version=“1.0” encoding=“UTF-8”?> < /span>
- <serviceGroup>
- <service name=“myService”>
- <description>Web Service例子description>
- <parameter name=“ServiceClass”>service.MyServiceparameter>
- <messageReceivers>
- <messageReceiver mep=“http://www.w3.org/2004/08/wsdl/in-out” class=“org.apache.axis2.rpc.receivers.RPCMessageReceiver” />
- <messageReceiver mep=“http://www.w3.org/2004/08/wsdl/in-only” class=“org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver” />
- messageReceivers>
- service>
- serviceGroup>
在这里最值得注意的是
例如,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
- public class HelloService {
- public String sayHello(){
- return “hello”;
- }
- public String sayHelloToPerson(String name){
- if(name==null){
- name = “nobody”;
- }
- return “hello,”+name;
- }
- }
[java] view plain copy
- <ns:sayHelloToPersonResponse xmlns:ns=“http://ws.apache.org/axis2”>
- <return>hello,billreturn>
- ns:sayHelloToPersonResponse>
[html] view plain copy
- <parameter name=“hotdeployment”>trueparameter>
[html] view plain copy
- <parameter name=“hotupdate”>falseparameter>
[html] view plain copy
- <deployer extension=“.class” directory=“my” class=“org.apache.axis2.deployment.POJODeployer”/>
[html] view plain copy
- package com.sinosoft.webservice;
- public class HelloServiceNew {
- public String sayHelloNew(){
- return “hello”;
- }
- public< span style="margin:0px; padding:0px; border:none; background-color:inherit"> String sayHelloToPersonNew(String name){
- if(name==null){
- name = “nobody”;
- }
- return “hello,”+name;
- }
- public void updateData(String data){
- System.out.println(data+” 已更新。 “);
- }
- }
[java] view plain copy
- xml version=“1.0” encoding=“UTF-8”?>
- <service name=“HelloServiceNew” targetNamespace=“http://ws.apache.org/ax2”>
- <schema schemaNamespace=“http://sdjxd.com.cn”/>
- <description>Web Service实例一description>
- <parameter name=“ServiceClass” >com.sinosoft.webservice.HelloServiceNewparameter>
- <messageReceivers>
- <messageReceiver mep=“http://www.w3.org/2004/08/wsdl/in-out”
- class=“org.apache.axis2.rpc.receivers.RPCMessageReceiver” />
- <messageReceiver mep=“http://www.w3.org/2004/08/wsdl/in-only”
- class=“org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver”/>
- messageReceivers>
- service>
[html] view plain copy span>
- <service name=” HelloServiceNew “>
- <description>
- Web Service例子
-
description > - <parame ter name=“ServiceClass”>
- com.sinosoft.webservice.HelloServiceNew
- parameter>
- <operation name=“sayHello”>
- <messageReceiver class=“org.apache.axis2.rpc.receivers.RPCMessageReceiver”/>
- operation>
- <operation name=“updateData”>
- <messageReceiver
- class=“org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver”/>
- operation>
- service>
[html] view plain copy
- <serviceGroup>
- <service name=“myService1”>
- …
- service>
- <service name=“myService2”>
- …
- service>
- serviceGroup>
[html] view plain copy
- <servlet>
- <servlet-name>AxisServletservlet-name>
- <servlet-class>org.apache.axis2.transport.http.AxisServletservlet-class>
- <load-on-startup>1load-on-startup>
- servlet>
- <servlet-mapping>
- <servlet-name>AxisServletservlet-name>
- <url-patt ern>/services/*url-pattern>
- servlet-mapping>
[html] view plain copy
- package service; //注意包名.类名与service.xml中的配置路径一致
- public class MyService {
- public String sayHello(String name) {
- return name + “says /”Hello!/“”;
- }
- }
- package service; //注意包名.类名与service.xml中的配置路径一致
- public class MyService {
- public String sayHello(String name) {
- return name + “says /”Hello!/“”;
- }
- }
[java] view plain copy
- xml version=“1.0” encoding=“UTF-8”?>
- <serviceGroup>
- <service name=“myService”>
- <description>Web Service例子 span>description>
- <parameter name=“ServiceClass”>service.MyServiceparameter>
- <messageReceivers> span>
- <messageReceiver mep=“http://www.w3.org/2004/08/wsdl/in-out” class=“org.apache.axis2.rpc.receivers.RPCMessageReceiver” />
- <messageReceiver mep=“http://www.w3.org/2004/08/wsdl/in-only” class=“org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver” />
- messageReceivers>
- service>
- serviceGroup>
[html] view plain copy
package service;
WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 3521 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC