web集成axis2_AXIS2 Web服务教程

web集成axis2

Welcome to Apache Axis2 Tutorial. Recently I was trying to upgrade my Apache Axis2 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教程。 最近,我试图将我的Apache Axis2技能从1.3升级到最新版本1.5.4,但我找不到任何不言自明的,涵盖最新版本的教程。 因此,它构成了我发布Axis2 Web Services Tutorial的基础。

Apache Axis2教程 (Apache Axis2 Tutorial)

谁应该使用本教程? (Who Should use this Tutorial?)

This tutorial is intended for Java programmers who are interested in developing and deploying Web Services using Apache Axis2.

本教程适用于对使用Apache Axis2开发和部署Web服务感兴趣的Java程序员。

先决条件 (Prerequisites)

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)有基本的了解。

使用的软件和工具 (Software and Tools Used)

  1. Java Development Kit (JDK) 1.6.0 (Tomcat 7 requires min JDK 1.6)

    Java开发工具包(JDK)1.6.0(Tomcat 7需要最低JDK 1.6)
  2. Apache Ant 1.7.0 (Axis2 requires minimum version 1.6.5)

    Apache Ant 1.7.0(Axis2需要最低版本1.6.5)
  3. Apache Axis2 1.5.4 (Binary Distribution)

    Apache Axis2 1.5.4(二进制发行版)
  4. Eclipse 3.6.1 IDE for Project Development (You can use other IDE also, for example NetBeans)

    用于项目开发的Eclipse 3.6.1 IDE(您也可以使用其他IDE,例如NetBeans)
  5. Apache Tomcat 7.0.8

    Apache Tomcat 7.0.8
  6. SoapUI for testing our web service.

    SoapUI用于测试我们的Web服务。
  7. Mac OS X 10.6.4 (I am working on Mac OS but the tutorial should work for other operating systems also, however some change might be needed in executing the commands)

    Mac OS X 10.6.4(我正在Mac OS上工作,但本教程也应适用于其他操作系统,但是在执行命令时可能需要进行一些更改)

系统设置 (System Setup)

  1. Download the latest version of Apache Tomcat latest version. As of now, the latest version is 7.0.8 and that is what I am using for this tutorial. It requires minimum Java Version 1.6, so make sure it’s installed in your system. If Java 6 is not installed in your system, you should download and install it first from Java SE Downloads. Download the Tomcat Core zip (apache-tomcat-7.0.8.zip) and unzip it to install it on your system. Set the JAVA_HOME environment variable to start and stop the server.

    下载最新版本的Apache Tomcat最新版本。 到目前为止,最新版本是7.0.8,这就是我在本教程中使用的版本。 它要求最低的Java版本1.6,因此请确保已将其安装在系统中。 如果您的系统中未安装Java 6,则应首先从Java SE Downloads下载并安装它。 下载Tomcat Core zip(apache-tomcat-7.0.8.zip)并解压缩以将其安装在系统上。 设置JAVA_HOME环境变量以启动和停止服务器。
  2. Download Apache Axis2 1.5.4 Binary Distribution zip from Apache Axis2 – Releases. This step is required to create axis2.war that will be deployed to tomcat and to get the axis2 libraries to be used in projects.

    Apache Axis2 – Releases下载Apache Axis2 1.5.4 Binary Distribution zip。 创建轴向2.war并将其部署到tomcat并获取将在项目中使用的axis2库是必需的。
  3. Unzip the Axis2 binary distribution zip into any convenient directory. Go to axis2-1.5.4/webapp directory and run the “ant create.war” command to create the axis2.war deployment in the axis2-1.5.4/dist directory. If you don’t have Apache Ant installed, you can download and install it from Apache Ant – Binary Distributions. Please note that I was facing some issue with axis2.war downloaded from War Distribution. Later on, I found out that few jars are missing in the axis2 War Distribution. War Distribution contains only 58 jars whereas Binary Distribution contains 63 jars. (I am feeling lazy to find out, which jars are missing.)
    $ ant create.war
    Buildfile: build.xml
    
    init:
        [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/temp
    
    prepare.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/conf
    
    create.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/temp
    
    BUILD SUCCESSFUL
    Total time: 2 seconds

    将Axis2二进制分发zip解压缩到任何方便的目录中。 转到axis2-1.5.4 / webapp目录,然后运行“ ant create.war”命令在axis2-1.5.4 / dist目录中创建axis2.war部署。 如果未安装Apache Ant,则可以从Apache Ant – Binary Distributions下载并安装它。 请注意,我从War Distribution下载的axis2.war遇到了一些问题。 后来,我发现axis2战争分配中缺少几个罐子。 战争分配只包含58个罐子,而二进制分配则包含63个罐子。 (我很懒惰地发现哪些瓶子不见了。)
  4. Deploy the axis2.war in the tomcat application server by copying it in tomcat webapps directory. You may need to restart the server if it’s not supporting hot deployment.

    通过将其复制到tomcat webapps目录中,将axis2.war部署在tomcat应用程序服务器中。 如果服务器不支持热部署,则可能需要重新启动服务器。
  5. Go to https://localhost:8080/axis2/ and click on Validate link. If the Happy Axis page is coming with GREEN color then it means that axis2 is successfully deployed. Our system setup is ready now and we can proceed for creating Axis2 web services.

    转到https:// localhost:8080 / axis2 /,然后单击“验证”链接。 如果“快乐轴”页面带有绿色,则表示axis2已成功部署。 我们的系统设置现已准备就绪,我们可以继续创建Axis2 Web服务。

创建Axis2 Web服务 (Creating Axis2 Web Service)

For creating Axis2 Web Service archive, we need following:

为了创建Axis2 Web服务存档,我们需要满足以下条件:

  1. A Java Project (Axis2WSImplementation) with a class that will be exposed as a web service. In my example, I am exposing two operations from MyService class. The first operation getData input is String and returns String whereas the second operation getObjectData input is MyBean java object and returns MyBean java object after some data manipulation. Note that MyBean class implements Serializable interface so that it can be transferred over the network.

    一个Java项目(Axis2WSImplementation),其类将作为Web服务公开。 在我的示例中,我将从MyService类公开两个操作。 第一个操作getData输入为String并返回String,而第二个操作getObjectData输入为MyBean java对象,并在进行一些数据操作后返回MyBean java对象。 请注意,MyBean类实现了Serializable接口,以便可以通过网络进行传输。
  2. Ant build.xml file that will be used to create aar, wsdl and client side stub and callback handler classes.

    将用于创建aar,wsdl和客户端存根和回调处理程序类的Ant build.xml文件。
  3. services.xml file that will be part of the axis2 archive. This file will be put in the META-INF folder of the axis2 archive.

    services.xml文件,它将是axis2存档的一部分。 该文件将放置在axis2归档文件的META-INF文件夹中。

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文件时创建。

Axis2 Web服务项目说明 (Axis2 Web Service Project Explanation)

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服务公开的类。 其他重要参数是targetNamespaceschemaNamespace

services.xml

services.xml

<service name="MyService" scope="application" targetNamespace="https://journaldev.com/">
 <description>
 MyService
 </description>
 <messageReceivers>
 <messageReceiver mep="https://www.w3.org/2004/08/wsdl/in-only" class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
 <messageReceiver mep="https://www.w3.org/2004/08/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
 </messageReceivers>
 <schema schemaNamespace="https://journaldev.com/xsd"/>
 <parameter name="ServiceClass">com.journaldev.ws.MyService</parameter>
</service>

build.xml: Ant build file for performing Axis2 tasks. There are three targets defined whose details are:

build.xml :用于执行Axis2任务的Ant构建文件。 定义了三个目标,其详细信息是:

  1. generate.wsdl: This target generates the MyService.wsdl file in the build folder. Make sure that targetNamespace and schemaTargetNamespace is same as in service.xml file.

    generate.wsdl :此目标在生成文件夹中生成MyService.wsdl文件。 确保targetNamespace和schemaTargetNamespace与service.xml文件中的相同。
  2. generate.service: This target generates axis2 archive in the build folder. It includes the services.xml file in the archive and archive name is MyService.aar

    generate.service :此目标在生成文件夹中生成axis2存档。 它在归档文件中包含services.xml文件,归档文件名为MyService.aar
  3. generate.client: This target generates the client side classes. Make sure you run this after executing generate.wsdl so the MyService.wsdl file is present in the build folder.

    generate.client :此目标生成客户端类。 确保在执行generate.wsdl之后运行此程序,以便MyService.wsdl文件位于构建文件夹中。

build.xml

build.xml

<project name="AxisWSImplementation" basedir="." default="generate.service">

 <property environment="env"/>
 <property name="build.dir" value="build"/>

 <path id="axis2.classpath">
 <fileset dir="${basedir}/lib">
 <include name="*.jar"/>
 </fileset>
 </path>

 <target name="compile.service">
 <mkdir dir="${build.dir}"/>
 <mkdir dir="${build.dir}/classes"/>
 <mkdir dir="${build.dir}/resources"/>
 <!--First let's compile the classes-->
 <javac debug="on"
 fork="true"
 destdir="${build.dir}/classes"
 srcdir="${basedir}/src"
 classpathref="axis2.classpath">
 </javac>
 </target>

 <target name="generate.wsdl" depends="compile.service">
 <taskdef name="java2wsdl"
 classname="org.apache.ws.java2wsdl.Java2WSDLTask"
 classpathref="axis2.classpath"/>
 <java2wsdl className="com.journaldev.ws.MyService"
 outputLocation="${build.dir}"
 targetNamespace="https://journaldev.com/"
 schemaTargetNamespace="https://journaldev.com/xsd">
 <classpath>
 <pathelement path="${axis2.classpath}"/>
 <pathelement location="${build.dir}/classes"/>
 </classpath>
 </java2wsdl>
 </target>

 <target name="generate.service" depends="compile.service">
 <copy toDir="${build.dir}/classes" failonerror="false">
 <fileset dir="${basedir}/resources">
 <include name="**/*.xml"/>
 </fileset>
 </copy>
 <jar destfile="${build.dir}/MyService.aar">
 <fileset excludes="**/Test.class" dir="${build.dir}/classes"/>
 </jar>
 </target>

 <target name="generate.client" depends="compile.service">
 <taskdef name="wsdl2java"
 classname="org.apache.axis2.tool.ant.AntCodegenTask"
 classpathref="axis2.classpath"/>
 <wsdl2java
 wsdlfilename="${build.dir}/MyService.wsdl"
 output="${build.dir}/resources" />
 </target>

 <target name="clean">
 <delete dir="${build.dir}"/>
 </target>
</project>

生成WSDL,Axis存档和存根文件 (Generating WSDL, Axis Archive and Stub Files)

  • Execute generate.wsdl ant target to generate MyService.wsdl file.

    执行generate.wsdl ant目标以生成MyService.wsdl文件。
  • Execute generate.service ant target to generate the MyService.aar file.

    执行generate.service ant目标以生成MyService.aar文件。
  • Execute generate.client ant target to generate the Stub classes.

    执行generate.client ant目标以生成Stub类。

Axis2 Web服务部署 (Axis2 Web Service Deployment)

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应该在此处列出两个操作。

Axis2 Web服务测试 (Axis2 Web Service Testing)

After deploying the service, first of all we need to test it. Here I am using SoapUI 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.

部署服务后,首先我们需要对其进行测试。 在这里,我使用的SoapUI是Web服务测试的最佳工具之一。 如果没有它,可以从他们的网站下载并轻松安装。

Steps for Testing using SoapUI

使用SoapUI进行测试的步骤

  1. Create a new SoapUI project with Project Name MyServiceTest (you can give any name you want) and Initial WSDL/WADL https://localhost:8080/axis2/services/MyService?wsdl (You can get this URL from Axis2 List Services page after clicking on the MyService link.). Leave other options with default value and click on OK button to create the SoapUI testing project.

    创建一个新的SoapUI项目,该项目名称为MyServiceTest(您可以输入任意名称),初始WSDL / WADL为https:// localhost:8080 / axis2 / services / MyService?wsdl(您可以从Axis2 List Services页面获取此URL,之后点击MyService链接。)。 将其他选项保留为默认值,然后单击“确定”按钮以创建SoapUI测试项目。
  2. Take any of the Soap Bindings and double click on the getData and getObjectData SOAP Requests.

    进行任何Soap Bindings并双击getData和getObjectData SOAP请求。
  3. Provide some values for input in the request and submit it to the web service end point URL. You should get output from the service similar to the below image. It confirms that our web service is up and running.

    提供一些用于请求中输入的值,并将其提交到Web服务端点URL。 您应该从服务获得类似于下图的输出。 它确认我们的Web服务已启动并正在运行。

Now we will proceed with the last task of invoking the web service using Axis2 stub classes.

现在,我们将继续执行使用Axis2存根类调用Web服务的最后一项任务。

使用存根文件调用Axis2 Web服务 (Axis2 Web Service invocation using Stub Files)

  1. Create a Java Project Axis2Client in Eclipse.

    在Eclipse中创建一个Java项目Axis2Client
  2. Create lib folder and copy all the Axis2 jars from downloaded binary distribution lib folder. Add these jars to the build path of the project.

    创建lib文件夹,然后从下载的二进制分发lib文件夹中复制所有Axis2 jar。 将这些jar添加到项目的构建路径。
  3. Copy earlier generated MyServiceStub.java and MyServiceCallbackHandler.java in the project src with correct package structure. In my case I copied them in com.journaldev package. If you have to provide these classes to somebody else, I would strongly suggest creating a jar from them and then distributing it with others to avoid any modifications.

    使用正确的包结构将早期生成的MyServiceStub.javaMyServiceCallbackHandler.java复制到项目src中。 就我而言,我将它们复制到com.journaldev包中。 如果您必须将这些类提供给其他人,我强烈建议您使用它们创建一个jar,然后将其分发给其他人以避免任何修改。
  4. Create the Axis2ClientUsingStubsFromAnt client class to invoke the web service operations. The project structure will look similar like below image.

    创建Axis2ClientUsingStubsFromAnt客户端类以调用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服务。 上面程序的输出是:

START
log4j:WARN No appenders could be found for logger (org.apache.axis2.description.AxisService).
log4j:WARN Please initialize the log4j system properly.
Output:HiPANKAJ
ID:101NAME:Output: KUMAR
DONE

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。 🙂

翻译自: https://www.journaldev.com/255/axis2-web-services-tutorial

web集成axis2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值