1.到Apache官网下载Axis2
给你地址:http://axis.apache.org/axis2/java/core/download.cgi
2.设置环境
把下载好的Axis2放到一个你喜欢的位置,我放在了
F:\Program Files\axis2-1.5.5
然后设置环境变量:
AXIS2_HOME=F:\Program Files\axis2-1.5.5 在PATH 最后添加 ;%AXIS2_HOME%\bin
3.简单测试
%AXIS2_HOME%\bin\axis2server.bat (Windows)
执行这个脚本,会看到:
Using JAVA_HOME C:\Program Files\Java\jdk1.7.0 Using AXIS2_HOME F:\Program Files\axis2-1.5.5 [INFO] [SimpleAxisServer] Starting [INFO] [SimpleAxisServer] Using the Axis2 RepositoryF:\Program Files\axis2-1.5.5\repository [SimpleAxisServer] Using the Axis2 RepositoryF:\Program Files\axis2-1.5.5\repository [SimpleAxisServer] Using the Axis2 Configuration FileF:\Program Files\axis2-1.5.5\conf\axis2.xml [INFO] Clustering has been disabled [INFO] Deploying module: addressing-1.5.5 - file:/F:/Program Files/axis2-1.5.5/repository/modules/ad dressing-1.5.5.mar [INFO] Deploying module: metadataExchange-1.5.5 - file:/F:/Program Files/axis2-1.5.5/repository/modu les/mex-1.5.5.mar [INFO] Deploying module: mtompolicy-1.5.5 - file:/F:/Program Files/axis2-1.5.5/repository/modules/mt ompolicy-1.5.5.mar [INFO] Deploying module: ping-1.5.5 - file:/F:/Program Files/axis2-1.5.5/repository/modules/ping-1.5 .5.mar [INFO] Deploying module: script-1.5.5 - file:/F:/Program Files/axis2-1.5.5/repository/modules/script ing-1.5.5.mar [INFO] Deploying module: soapmonitor-1.5.5 - file:/F:/Program Files/axis2-1.5.5/repository/modules/s oapmonitor-1.5.5.mar [INFO] Deploying Web service: version.aar - file:/F:/Program Files/axis2-1.5.5/repository/services/v ersion.aar [INFO] [SimpleAxisServer] Started [SimpleAxisServer] Started [INFO] Listening on port 8080
在浏览器里面输入:http://localhost:8080/axis2/services/
如果看到:
Deployed servicesVersion
Available operations
- getVersion
这说明Axis2没问题
4.编译Axis2项目
首先,你应该下载了 Ant ,并且版本要大于 1.6.5 ,设置好Ant环境变量 ,然后再进行下面的操作
下面进入到 $AXIS_HOME/webapp 下
F:\Program Files\axis2-1.5.5\webapp>ant -version Apache Ant(TM) version 1.8.2 compiled on December 20 2010 F:\Program Files\axis2-1.5.5\webapp>ant create.war Buildfile: F:\Program Files\axis2-1.5.5\webapp\build.xml init: [mkdir] Created dir: F:\Program Files\axis2-1.5.5\dist [mkdir] Created dir: F:\Program Files\axis2-1.5.5\dist\temp [copy] Copying 59 files to F:\Program Files\axis2-1.5.5\dist\temp prepare.repo: [copy] Copying 9 files to F:\Program Files\axis2-1.5.5\dist\temp\WEB-INF [mkdir] Created dir: F:\Program Files\axis2-1.5.5\dist\temp\WEB-INF\conf [copy] Copying 1 file to F:\Program Files\axis2-1.5.5\dist\temp\WEB-INF\conf create.war: [war] Building war: F:\Program Files\axis2-1.5.5\dist\axis2.war [delete] Deleting directory F:\Program Files\axis2-1.5.5\dist\temp BUILD SUCCESSFUL Total time: 4 seconds F:\Program Files\axis2-1.5.5\webapp>
这样,便编译好了Axis2的项目。
5.测试Axis2项目
准备使用 Tomcat容器,所以,没有的先下载。
刚刚build好的Axis2项目应该在 AXIS2_HOME/dist 下面
把这个 axis2.war 剪切(或者复制)到tomcat下的webapp目录下,启动Tomcat(启动前把刚刚打开的Axis服务关掉,因为都用了8080端口)。
再输入下面的地址:
http://localhost:8080/axis2/axis2-web/index.jsp
如果看到这样的结果:
Welcome!
Welcome to the new generation of Axis. If you can see this page you have successfully deployed the Axis2 Web Application. However, to ensure that Axis2 is properly working, we encourage you to click on the validate link.
- Services
View the list of all the available services deployed in this server. - Validate
Check the system to see whether all the required libraries are in place and view the system information. - Administration
Console for administering this Axis2 installation.
说明成功了。
点击 Services 可查看当前存在的服务。
6.编写测试服务
使用Eclipse新建一个Java项目
导入所需jar包:当然就是 %AXIS2_HOME%\lib 下面的所有jar包(为了方便)。
新建包:
mm.test.webservices.axis2userguide
在这个包下面
创建 SampleService.java
内容如下:
package mm.test.webservices.axis2userguide;
import javax.xml.stream.XMLStreamException;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
public class SampleService {
public OMElement sayHello(OMElement element) throws XMLStreamException {
element.build();
element.detach();
String rootName = element.getLocalName();
System.out.println("Reading " + rootName + " element");
OMElement childElement = element.getFirstElement();
String personToGreet = childElement.getText();
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace omNs = fac.createOMNamespace(
"http://example1.org/example1", "example1");
OMElement method = fac.createOMElement("sayHelloResponse", omNs);
OMElement value = fac.createOMElement("greeting", omNs);
value.addChild(fac.createOMText(value, "Hello, " + personToGreet));
method.addChild(value);
return method;
}
@SuppressWarnings("unused")
private void ping() {
}
}
然后再到另一个地方(随意,当然,当前项目下也可以)创建一个文件夹:
J:\SampleService
这是我的。
然后,把刚才编译好的 mm.test.webservices.axis2userguide.SampleService.class
连带文件夹拷到这个目录下
再在当前目录下新建:
META-INF
在META-INF目录下新建文件:services.xml,内容如下:
<service name="UserGuideSampleService"> <description> This is a sample service created in the Axis2 User's Guide </description> <parameter name="ServiceClass"> mm.test.webservices.axis2userguide.SampleService </parameter> <operation name="sayHello"> <messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/> </operation> <operation name="ping"> <messageReceiver class="org.apache.axis2.receivers.RawXMLINOnlyMessageReceiver"/> </operation> </service>
还是在J:\SampleService文件夹下,打开cmd窗口,输入:
J:\SampleService>jar cvf SampleService.aar ./* 标明清单(manifest) 忽略项 META-INF/ 增加:META-INF/services.xml(读入= 549) (写出= 243)(压缩了 55%) 增加:mm/(读入= 0) (写出= 0)(存储了 0%) 增加:mm/test/(读入= 0) (写出= 0)(存储了 0%) 增加:mm/test/webservices/(读入= 0) (写出= 0)(存储了 0%) 增加:mm/test/webservices/axis2userguide/(读入= 0) (写出= 0)(存储了 0%) 增加:mm/test/webservices/axis2userguide/SampleService.class(读入= 2232) (写出= 1074)(压缩了 51%) J:\SampleService>
这样,就会在 J:\SampleService 目录下生成一个 SampleService.aar 包。
7.部署测试服务
第1种:使用管理控制台部署
打开管理控制台:http://localhost:8080/axis2/axis2-admin/
输入用户名密码:admin/axis2
选择第一项 : Upload Service
找到刚才生成的 SampleService.aar 直接部署吧(点击 浏览 --> upload)
第2种:手工拷贝到对应地址
这个没得说,就把 SampleService.aar 直接拷贝到
%TOMCAT_HOME%\webapps\axis2\WEB-INF\services
这个目录下面就可以了。
8.查看是否部署成功
http://localhost:8080/axis2/services/listServices
打开后,可以看到:
UserGuideSampleService
多出了这么一个服务,说明部署成功。
9.编写客户端进行测试
在刚刚新建的java项目下:
在 mm.test.webservices.axis2userguide 包下
新建 : SampleClient.java 内容如下:
package mm.test.webservices.axis2userguide;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.Constants;
import org.apache.axis2.client.ServiceClient;
public class SampleClient {
private static EndpointReference targetEPR = new EndpointReference(
"http://localhost:8080/axis2/services/UserGuideSampleService");
public static OMElement greetUserPayload(String personToGreet) {
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace omNs = fac.createOMNamespace(
"http://example1.org/example1", "example1");
OMElement method = fac.createOMElement("sayHello", omNs);
OMElement value = fac.createOMElement("personToGreet", omNs);
value.addChild(fac.createOMText(value, personToGreet));
method.addChild(value);
return method;
}
public static void main(String[] args) {
try {
OMElement payload = SampleClient.greetUserPayload("John");
Options options = new Options();
options.setTo(targetEPR);
options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
ServiceClient sender = new ServiceClient();
sender.setOptions(options);
OMElement result = sender.sendReceive(payload);
String response = result.getFirstElement().getText();
System.out.println(response);
} catch (Exception e) { // (XMLStreamException e) {
System.out.println(e.toString());
}
}
}
右键运行下,如果发现控制台输出:
Hello, John
说明:调用服务成功。