本文共 15517 字,大约阅读时间需要 51 分钟。
web集成axis2
Welcome to Apache Axis2 Tutorial. Recently I was trying to upgrade my skills from 1.3 to latest version 1.5.4 and I was not able to find out any tutorial that is self-explanatory and covering the latest release. So it forms the basis of my post for Axis2 Web Services Tutorial.
欢迎使用Apache Axis2教程。 最近,我试图将我的技能从1.3升级到最新版本1.5.4,但我找不到任何不言自明的,涵盖最新版本的教程。 因此,它构成了我发布Axis2 Web Services Tutorial的基础。
This tutorial is intended for Java programmers who are interested in developing and deploying Web Services using Apache Axis2.
本教程适用于对使用Apache Axis2开发和部署Web服务感兴趣的Java程序员。
The scope of this tutorial is to use Axis2 for creating web services and invoking the web service using a Java client program and testing web service using Soap UI tool. Basic understanding of Java, Web Services, XML, Ant, and application server (Tomcat) is required to understand the tutorial with ease.
本教程的范围是使用Axis2创建Web服务,并使用Java客户端程序调用Web服务,并使用Soap UI工具测试Web服务。 为了轻松理解本教程,需要对Java,Web服务,XML,Ant和应用程序服务器(Tomcat)有基本的了解。
$ ant create.warBuildfile: build.xmlinit: [mkdir] Created dir: /Users/pankaj/Downloads/axis2-1.5.4/dist/temp [copy] Copying 59 files to /Users/pankaj/Downloads/axis2-1.5.4/dist/tempprepare.repo: [copy] Copying 9 files to /Users/pankaj/Downloads/axis2-1.5.4/dist/temp/WEB-INF [mkdir] Created dir: /Users/pankaj/Downloads/axis2-1.5.4/dist/temp/WEB-INF/conf [copy] Copying 1 file to /Users/pankaj/Downloads/axis2-1.5.4/dist/temp/WEB-INF/confcreate.war: [war] Building war: /Users/pankaj/Downloads/axis2-1.5.4/dist/axis2.war [delete] Deleting directory /Users/pankaj/Downloads/axis2-1.5.4/dist/tempBUILD SUCCESSFULTotal time: 2 seconds将Axis2二进制分发zip解压缩到任何方便的目录中。 转到axis2-1.5.4 / webapp目录,然后运行“ ant create.war”命令在axis2-1.5.4 / dist目录中创建axis2.war部署。 如果未安装Apache Ant,则可以从下载并安装它。 请注意,我从War Distribution下载的axis2.war遇到了一些问题。 后来,我发现axis2战争分配中缺少几个罐子。 战争分配只包含58个罐子,而二进制分配则包含63个罐子。 (我很懒惰地发现哪些瓶子不见了。)
For creating Axis2 Web Service archive, we need following:
为了创建Axis2 Web服务存档,我们需要满足以下条件:
The project structure will look like the below image.
项目结构如下图所示。
Don’t get confused with the content inside build folder. They will be created when we will execute the build.xml ant file.
不要与build文件夹中的内容混淆。 它们将在我们执行build.xml ant文件时创建。
MyService.java: Implementation class that will be exposed as Axis2 web service.
MyService.java :实现类,将作为Axis2 Web服务公开。
package com.journaldev.ws;import com.journaldev.bean.MyBean;public class MyService { public String getData(String input) { return "Hi" + input; } public MyBean getObjectData(MyBean myBean) { String name = myBean.getName(); int id = myBean.getId(); myBean.setId(id + 100); myBean.setName("Output: " + name); return myBean; }}
MyBean.java: Java Bean class that is input and output of getObjectData operation in web service.
MyBean.java:Web服务中getObjectData操作的输入和输出的Java Bean类。
package com.journaldev.bean;import java.io.Serializable;public class MyBean implements Serializable { private static final long serialVersionUID = -1129402159048345204L; private String name; private int id; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; }}
services.xml: Axis2 web service related parameters are part of this xml file. ServiceClass parameter specifies the class that will be exposed as web service. The other important parameters are targetNamespace and schemaNamespace.
services.xml :与Axis2 Web服务相关的参数是此xml文件的一部分。 ServiceClass参数指定将作为Web服务公开的类。 其他重要参数是targetNamespace和schemaNamespace 。
services.xml
services.xml
MyService com.journaldev.ws.MyService
build.xml: Ant build file for performing Axis2 tasks. There are three targets defined whose details are:
build.xml :用于执行Axis2任务的Ant构建文件。 定义了三个目标,其详细信息是:
build.xml
build.xml
Copy the MyService.aar in ~apache-tomcat-7.0.8/webapps/axis2/WEB-INF/services directory. Axis2 supports hot deployment of services, so you don’t need to restart the server. Check your service deployment on ListServices page (https://localhost:8080/axis2/services/listServices). MyService should be listed there with two operations.
将MyService.aar复制到〜apache-tomcat-7.0.8 / webapps / axis2 / WEB-INF / services目录中。 Axis2支持服务的热部署,因此您无需重新启动服务器。 在ListServices页面(https:// localhost:8080 / axis2 / services / listServices)上检查服务部署。 MyService应该在此处列出两个操作。
After deploying the service, first of all we need to test it. Here I am using that is one of the best tools for Web Service Testing. If you don’t have it, you can download it from their website and install it easily.
部署服务后,首先我们需要对其进行测试。 在这里,我使用的是Web服务测试的最佳工具之一。 如果没有它,可以从他们的网站下载并轻松安装。
Steps for Testing using SoapUI
使用SoapUI进行测试的步骤
Now we will proceed with the last task of invoking the web service using Axis2 stub classes.
现在,我们将继续执行使用Axis2存根类调用Web服务的最后一项任务。
Axis2ClientUsingStubsFromAnt Code
Axis2ClientUsingStubsFromAnt代码
package com.journaldev.ws.client;import java.rmi.RemoteException;import com.journaldev.MyServiceStub;import com.journaldev.MyServiceStub.GetData;import com.journaldev.MyServiceStub.GetDataResponse;import com.journaldev.MyServiceStub.GetObjectData;import com.journaldev.MyServiceStub.GetObjectDataResponse;import com.journaldev.MyServiceStub.MyBean;/** * * @author Pankaj - www.journaldev.com This class will invoke Axis2 web service * operations using Stub classes * */public class Axis2ClientUsingStubsFromAnt { /** * END_POINT is the web service endpoint */ private final static String END_POINT = "https://localhost:8080/axis2/services/MyService"; public static void main(String[] args) throws RemoteException { System.out.println("START"); // Create the Stub Object by passing the Web Service Endpoint URL MyServiceStub stub = new MyServiceStub(END_POINT); // Creating an input object for the getData operation GetData getDataInput = new GetData(); // Setting the input part in the getData input object getDataInput.setInput("PANKAJ"); // invoking the getData operation GetDataResponse getDataOutput = stub.getData(getDataInput); // get_return method returns the web service output object. Here its // String, so we can // directly print the returned value System.out.println("Output:" + getDataOutput.get_return()); // Creating input object for the getObjectData operation GetObjectData getObjectDataInput = new GetObjectData(); MyBean myBean = new MyBean(); myBean.setId(1); myBean.setName("KUMAR"); // Setting the input part in the getObjectData input object getObjectDataInput.setMyBean(myBean); // invoking the getObjectData operation GetObjectDataResponse getObjectDataOutput = stub .getObjectData(getObjectDataInput); // Get the MyBean object from the response object MyBean myBeanOutput = getObjectDataOutput.get_return(); // Print the myBeanOutput values to check that web service operations // are getting invoked System.out.println("ID:" + myBeanOutput.getId() + "NAME:" + myBeanOutput.getName()); System.out.println("DONE"); }}
Execute the Axis2ClientUsingStubsFromAnt class to invoke the web service. Output of the above program is:
执行Axis2ClientUsingStubsFromAnt类以调用Web服务。 上面程序的输出是:
STARTlog4j:WARN No appenders could be found for logger (org.apache.axis2.description.AxisService).log4j:WARN Please initialize the log4j system properly.Output:HiPANKAJID:101NAME:Output: KUMARDONE
If you find the tutorial helpful in understanding Axis2 and getting started with it, please share your thoughts in the comment section.
如果您发现本教程有助于理解Axis2并开始使用它,请在评论部分中分享您的想法。
And yeah, don’t forget to share it across with others.
是的,别忘了与他人分享。
Your two clicks and 5 seconds time can help someone else to learn Axis2 easily. 🙂
您的两次单击和5秒的时间可以帮助其他人轻松学习Axis2。 🙂
翻译自:
web集成axis2
转载地址:http://taozd.baihongyu.com/