Home Database Mysql Tutorial WebService学习笔记5AXIS2基础

WebService学习笔记5AXIS2基础

Jun 07, 2016 pm 03:49 PM
webservice Base study notes

1.AXIS2 简介: Apache Axis2 项目是一个基于 Java 语言的 Webservices 系统服务和客户端的实现。为了从 Apache Axis 1.0 中获取教训, Apache Axis2 提供了一个完整的对象模型和模块化体系结构,这样可以很容易的添加功能以及支持一个新的与 Web services

 

1.AXIS2简介:

Apache Axis2项目是一个基于Java语言的Web services系统服务和客户端的实现。为了从Apache Axis 1.0中获取教训,Apache Axis2提供了一个完整的对象模型和模块化体系结构,这样可以很容易的添加功能以及支持一个新的与Web services相关的说明和建议。Axis2可以让你很容易的执行以下任务:

(1).发送SOAP消息。

(2).接收和处理SOAP消息。

(3).从一个普通的Java类建立Web service

(4).WSDL来建立实现服务和客户端的实现类。

(5).很容易的从一个服务来获取WSDL

(6).发送和接收带有附件的SOAP消息。

(7).建立或者使用基于RESTWeb service

(8).建立或者使用从WS-SecurityWS-ReliableMessagingWS-AddressingWS-CoordinationWS-Atomic Transaction获取优势的服务。

(9).当新建议出现时,使用Axis2的模块化结构来很容易的加入对它的支持。

2.配置AXIS2环境:

(1).下载Axis2,去http://ws.apache.org/axis2/下载axis2的最新版本,一般是两个zip文件,如axis2-1.4.1-bin.zip(包含了Axis2中所有的jar文件)axis2-1.4.1-war.zip(用于将WebService发布到web容器中)
(2).
axis2-1.4.1-war.zip解压,将目录中的axis2.war文件放到Servlet容器的项目发布目录中,以Tomcat为例,放到”%Tomcat%/webapps”目录中,启动Tomcat

(3).在浏览器地址栏中输入如下的URLhttp://localhost:8080/axis2/”,应该可以进入axis2的欢迎界面,点击”Validate”如果没有报错,则说明axis2的环境已经配置好。

(4).点击”Administration”,初始用户名和密码分别是:adminaxis2,也可以到” %Tomcat%/webapps/axis2/WEB-INF/conf/axis2.xml”下修改用户名和密码。

3.POJO发布成AXIS2 WebService的简单例子:

(1).编程普通的java类,将编译过后的”.class”文件拷贝到axis2POJO发布目录”%Tomcat%/webapps/axis2/WEB-INF/POJO”中。
注意:若目录下没有POJO目录,则手动新建该目录即可。

(2).启动TomcatPOJO即被发布成了WebServicejava类中中所有的public方法即被发布成WebServiceOperation

注意:POJO类不能使用package关键字声明包,AXIS2默认是热部署的。

4.POJO无配置发布高级:

由于AXIS2POJO发布方式不支持Package,若类中需要调用其他类的方法时,需要将所引用的类编译后的”.class”文件拷贝到”%Tomcat%/webapps/axis2/WEB-INF/classes”目录下。

5.AXIS2客户端的简单实现:

(1).使用RPC方式调用WebService

 

(2).指定调用WebServiceURL

 

(3).指定调用服务方法的参数值:

 

(4).指定方法返回值的数据类型,如:

 

(5).指定要调用方法名和WSDL文件的命名空间:

 

(6).调用服务的方法:

 

注意:当方法没有参数时,第二个参数值不能为null,而要使用new Object[]{}

当被调用的方法没有返回值时,应该使用RPCServiceClient类的invokeRobust方法。

6.使用wsdl2java简化客户端的编写:

Windows控制台输出如下的命令行来生成调用WebService的代码:

%AXIS2_HOME%/bin/wsdl2java –uri wsdlurl –p 生成java类的包名 -s –o 客户端存根目录名

其中-url参数指定wsdl文件的路径,可以说本地路径,也可以是网络路径。-p参数指定了生成的java类的包名,-o参数指定了生成的一系列文件保存的根目录。执行完命令后就会在当前目录下生成一个存根目录名的目录,里面就是wsdl2java生成的客户端代码。

7.AXIS2复合类型数据的传递:

AXIS2中可以直接使用将WebService方法的参数或返回值类型声明为数组或者类(接口)

注意:

(1).在定义数组类型时只能使用一维数组,如果想传递多维数组,可以使用分隔符进行分割,如:

 

可以将该数组看作是一个3×3的二维数组。

(2).当传递自定义的类或接口时,请务必实现序列化。

8.使用services.xml方式发布WebService

(1).编写服务端的类。

(2).在工程META-INF目录下建services.xml文件,添加如下内容:

 

注意:有返回值的处理器为:org.apache.axis2.rpc.receivers.RPCMessageReceiver

无返回值的处理器为:org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver

(3).META-INF(包含services.xml)和编译好的类使用jar工具命令打包成”.aar”后缀名的文件。

(4).将打包的”.aar”文件发布到axis2的发布目录:%Tomcat%/webapps/axis2/WEB-INF/Services

注意:此种方法允许使用package

9.AXIS2使用DataHandler传输二进制文件:

(1).服务端:

 

(2).客户端:

 

10.AXIS2的状态管理:

(1).AXIS2管理WebService的状态基本上对开发人员是透明的,在WebService类需要使用org.apache,axis2.context.MessageContextorg.apache.axis2.context.ServiceContext类来保存与获取保存在服务端的状态信息,类似于使用HTTPSession接口的getAttributesetAttribute方法获得与设置Session属性。

(2).此外,还需修改services.xml文件的内容,为元素添加一个scope属性,该属性有4个可取的值:applicationsoapsessiontransportsessionrequest(全部小写,request是默认值)

其中transportsessionapplication分别实现同一个WebService类和跨WebService类的会话管理。

(3).在客户端需要使用setManageSession(true);打开session管理功能。

11.实现同一个WebServiceSession管理步骤:

(1).使用MessageContextServiceContext获取与设置Sessionkey-value对。

(2).为要进行session管理的WebService类在services.xml中所对应的元素添加一个scope属性,并将该属性值设置为”transportsession”

(3).在客户端使用setManageSession(true);打开Session管理功能。

12.WebServicesession管理操作代码:

(1).设置key-value

 

(2).获取value

 

(3).若用stub生成方式(进入axis2安装目录使用wsdl2java –uri wsdlURL –p 包名 –s –o 存放目录名),则需要添加”-serviceClient.getOptions().setManageSession(true);”

13.WebService跨服务会话管理:

在多个WebService服务之间共享会话状态,也称为跨服务会话管理,实现跨服务会话管理与实现同一个服务的会话管理类似,步骤如下:

(1).使用MessageContextServiceContext获取与设置Sessionkey-value对。

(2).为要进行session管理的WebService类在services.xml中所对应的元素添加一个scope属性,并将该属性值设置为”application”

(3).在客户端使用setManageSession(true);打开Session管理功能。

注意:java中只有将scope设置为application才支持跨服务的会话管理功能。

14.AXIS2Spring的集成:

(1).为工程添加Spring支持,在web.xml文件中添加spring的相关配置。

(2).spring配置文件中配置AXIS2ServiceBean如下:

 

(3).WEB-INF/services/下新建一个目录,这个名字可以起的随便,我们就起做test吧,然后在WEB-INF/services/test/下建立META-INF这个目录,最后在WEB-INF/services/test/META-INF/下建立service.xml,文件内容是:

 

15.AXIS2的异步调用WebService

同步调用时,如果被调用的WebService方法长时间没有返回,则客户端将一直处于阻塞状态,知道方法返回为止。为了避免长时间被阻塞,需要使用异步调用。AXIS2异步调用有两种方法:

(1).多线程。

(2).RPCServiceClient类提供了一个invokeNoBlocking()方法实现对WebService的异步调用。具体用法如下:

serviceClient.invokeNoBlocking(qname, parameters, 一个实现了AXISCallback接口的实例对象);

实现AXISCallback接口需要实现其如下4个方法,实现异步回调:

onComplete()onError(Exception e)onFault(MessageContext mc)onMessage(MessageContext mc)

注意:当使用wsdl2java生成客户端调用stub时,会自动生成服务名+CallbackHandler”的抽象回调类。

16.AXIS2的模块:

AXIS2可以通过模块(Module)进行扩展,AXIS2模块至少需要有两个类,这两个类分别实现了ModuleHandler接口,开发和使用一个AXIS2的模板步骤如下:

(1).编写实现了Module接口的类,AXIS2模块在进行初始化、销毁等动作时会自动调用该类中的相应方法。

(2).编写实现了Handler接口的类,该类是AXIS2模块的业务处理类。

(3).编写module.xml文件,该文件放在工程的META-INF目录下,用于配置模块。

(4).axis2.xml文件中配置AXIS2模块。

(5).services.xml文件中配置AXIS2模块的引用,模块名”/>

(6).发布AXIS2模块,需要使用jar命令将AXIS2模块打包成”.mar”后缀名包,然后将该后缀名为”.mar”的文件拷贝到AXIS2模块发布目录 %Tomcat%/webapps/axis2/WEB-INF/modules

注意:模块和Service要分开打包,将module类,handler类和module.xml打成”.mar”文件,将service类和services.xml文件打成”.aar”文件,分别发布。

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1668
14
PHP Tutorial
1273
29
C# Tutorial
1255
24
How to delete Xiaohongshu notes How to delete Xiaohongshu notes Mar 21, 2024 pm 08:12 PM

How to delete Xiaohongshu notes? Notes can be edited in the Xiaohongshu APP. Most users don’t know how to delete Xiaohongshu notes. Next, the editor brings users pictures and texts on how to delete Xiaohongshu notes. Tutorial, interested users come and take a look! Xiaohongshu usage tutorial How to delete Xiaohongshu notes 1. First open the Xiaohongshu APP and enter the main page, select [Me] in the lower right corner to enter the special area; 2. Then in the My area, click on the note page shown in the picture below , select the note you want to delete; 3. Enter the note page, click [three dots] in the upper right corner; 4. Finally, the function bar will expand at the bottom, click [Delete] to complete.

Learn to completely uninstall pip and use Python more efficiently Learn to completely uninstall pip and use Python more efficiently Jan 16, 2024 am 09:01 AM

No more need for pip? Come and learn how to uninstall pip effectively! Introduction: pip is one of Python's package management tools, which can easily install, upgrade and uninstall Python packages. However, sometimes we may need to uninstall pip, perhaps because we wish to use another package management tool, or because we need to completely clear the Python environment. This article will explain how to uninstall pip efficiently and provide specific code examples. 1. How to uninstall pip The following will introduce two common methods of uninstalling pip.

What should I do if the notes I posted on Xiaohongshu are missing? What's the reason why the notes it just sent can't be found? What should I do if the notes I posted on Xiaohongshu are missing? What's the reason why the notes it just sent can't be found? Mar 21, 2024 pm 09:30 PM

As a Xiaohongshu user, we have all encountered the situation where published notes suddenly disappeared, which is undoubtedly confusing and worrying. In this case, what should we do? This article will focus on the topic of "What to do if the notes published by Xiaohongshu are missing" and give you a detailed answer. 1. What should I do if the notes published by Xiaohongshu are missing? First, don't panic. If you find that your notes are missing, staying calm is key and don't panic. This may be caused by platform system failure or operational errors. Checking release records is easy. Just open the Xiaohongshu App and click "Me" → "Publish" → "All Publications" to view your own publishing records. Here you can easily find previously published notes. 3.Repost. If found

A deep dive into matplotlib's colormap A deep dive into matplotlib's colormap Jan 09, 2024 pm 03:51 PM

To learn more about the matplotlib color table, you need specific code examples 1. Introduction matplotlib is a powerful Python drawing library. It provides a rich set of drawing functions and tools that can be used to create various types of charts. The colormap (colormap) is an important concept in matplotlib, which determines the color scheme of the chart. In-depth study of the matplotlib color table will help us better master the drawing functions of matplotlib and make drawings more convenient.

How to add product links in notes in Xiaohongshu Tutorial on adding product links in notes in Xiaohongshu How to add product links in notes in Xiaohongshu Tutorial on adding product links in notes in Xiaohongshu Mar 12, 2024 am 10:40 AM

How to add product links in notes in Xiaohongshu? In the Xiaohongshu app, users can not only browse various contents but also shop, so there is a lot of content about shopping recommendations and good product sharing in this app. If If you are an expert on this app, you can also share some shopping experiences, find merchants for cooperation, add links in notes, etc. Many people are willing to use this app for shopping, because it is not only convenient, but also has many Experts will make some recommendations. You can browse interesting content and see if there are any clothing products that suit you. Let’s take a look at how to add product links to notes! How to add product links to Xiaohongshu Notes Open the app on the desktop of your mobile phone. Click on the app homepage

Getting Started with Pygame: Comprehensive Installation and Configuration Tutorial Getting Started with Pygame: Comprehensive Installation and Configuration Tutorial Feb 19, 2024 pm 10:10 PM

Learn Pygame from scratch: complete installation and configuration tutorial, specific code examples required Introduction: Pygame is an open source game development library developed using the Python programming language. It provides a wealth of functions and tools, allowing developers to easily create a variety of type of game. This article will help you learn Pygame from scratch, and provide a complete installation and configuration tutorial, as well as specific code examples to get you started quickly. Part One: Installing Python and Pygame First, make sure you have

Revealing the appeal of C language: Uncovering the potential of programmers Revealing the appeal of C language: Uncovering the potential of programmers Feb 24, 2024 pm 11:21 PM

The Charm of Learning C Language: Unlocking the Potential of Programmers With the continuous development of technology, computer programming has become a field that has attracted much attention. Among many programming languages, C language has always been loved by programmers. Its simplicity, efficiency and wide application make learning C language the first step for many people to enter the field of programming. This article will discuss the charm of learning C language and how to unlock the potential of programmers by learning C language. First of all, the charm of learning C language lies in its simplicity. Compared with other programming languages, C language

Let's learn how to input the root number in Word together Let's learn how to input the root number in Word together Mar 19, 2024 pm 08:52 PM

When editing text content in Word, you sometimes need to enter formula symbols. Some guys don’t know how to input the root number in Word, so Xiaomian asked me to share with my friends a tutorial on how to input the root number in Word. Hope it helps my friends. First, open the Word software on your computer, then open the file you want to edit, and move the cursor to the location where you need to insert the root sign, refer to the picture example below. 2. Select [Insert], and then select [Formula] in the symbol. As shown in the red circle in the picture below: 3. Then select [Insert New Formula] below. As shown in the red circle in the picture below: 4. Select [Radical Formula], and then select the appropriate root sign. As shown in the red circle in the picture below:

See all articles