1.安装axis2.下载axis2发布包axis2-1.4.1-war.zip,解压后激昂axis2.war复制到tomcat下的webapps目录下。下载地址:http://ws.apache.org/axis2/。
2.验证是否发布成功。启动tomcat,打开“http://localhost:8080/axis2”,可以看到axis2的欢迎界面。点击“Services”可以查看当前已经发布的服务。
3.创建要发布的服务类SampleService.java。
- package axis2;
- 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;
- }
- private void ping() {
- }
- }
4.创建服务描述文件services.xml。服务描述文件定义了可用的消息接收类。
- <service name="UserGuideSampleService">
- <description>
- This is a sample service created in the Axis2 User's Guide
- </description>
- <parameter name="ServiceClass">
- axis2.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>
5.生成aar文件。在根目录下新建一个名为META-INF的文件夹,此文件夹与class下的axis2目录位于同一位置。将services.xml文件复制到META-INFO目录下。此时的文件路径如下:
根目录
|- META-INF
| |-services.xml
|
|- axis2
| |-SampleService.class
从命令行进入根目录,执行命令:
- jar cvf SampleService.aar ./*
生成SampleService.aar文件。
这一步也可以直接从eclipse中导出jar文件,然后将扩展名修改为.aar。
6.发布服务。将 SampleService.aar复制到%TOMCAT_HOME%/webapps/axis2/WEB-INF/services目录下。打开该目录下的services.list文件,在末尾增加一行,“SampleService.aar”。
7.查看服务。打开“http://localhost:8080/axis2”,点击“Services”可以看到刚才发布的服务了。
8.创建客户端调用程序。执行前确保已经将axis2.war/lib目录下的jar包引入工程。
- package axis2;
- 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.Constants;
- import org.apache.axis2.addressing.EndpointReference;
- import org.apache.axis2.client.Options;
- 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”,调用成功。
本文介绍如何使用Apache Axis2发布Web服务,包括安装配置、创建服务类、生成aar文件及发布服务的过程,并提供了客户端调用服务的示例。
481

被折叠的 条评论
为什么被折叠?



