按照下面,分别建2个工程,一个client(客户端),一个server(服务端)

先实现服务端:

1、编写services.xml文件,该文件是放在aar文件里的\META-INF目录下的:

<?xml version="1.0" encoding="UTF-8"?>
<!-- This file was auto-generated from WSDL -->
<!-- by the Apache Axis2 version: 1.4.1 Built on : Aug 19, 2008 (10:13:39 LKT)
<parameter name="useOriginalwsdl">false</parameter>
<parameter name="modifyUserWSDLPortAddress">true</parameter>
-->
<serviceGroup>
<service name="axis2Service">
<description>
This is a sample Web Service.
</description>
<!-- // ServiceClass指定Java Class的位置,即实现服务的类。 -->
<parameter name="ServiceClass" locked="false">com.study.axis2.service.impl.Axis2ServiceImpl</parameter>
<!-- // operation 与Java Class中方法名对应。 -->
<operation name="user">
<!-- // messageReceiver看下文注解。 -->
<messageReceiver class="com.study.axis2.receive.Axis2MessageReceiverInOut"/>
</operation>
</service>
</serviceGroup>

2、编写Axis2ServiceImpl类:

package com.study.axis2.service.impl;

import com.study.axis2.domain.User;
import com.study.axis2.domain.UserResponse;
import com.study.axis2.service.Axis2Service; public class Axis2ServiceImpl implements Axis2Service { public UserResponse user(User user) {
// 将in转换为String。
int userId = user.getUserId();
String userName = user.getUserName(); System.out.println("USER ID : " + userId + "; USER NAME : " + userName); UserResponse response = new UserResponse();
response.setRspCode("0000");
response.setRspDesc("SUCCESS"); return response;
} }

3、编写User和UserResponse类:

package com.study.axis2.domain;

public class User {

    private int userId;
private String userName; public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
package com.study.axis2.domain;

public class UserResponse {

    private String rspCode;
private String rspDesc; public String getRspCode() {
return rspCode;
}
public void setRspCode(String rspCode) {
this.rspCode = rspCode;
}
public String getRspDesc() {
return rspDesc;
}
public void setRspDesc(String rspDesc) {
this.rspDesc = rspDesc;
}
}

4、编写Axis2MessageReceiverInOut类,用来接收报文的,实际上用Axis2ServiceImpl类来处理业务逻辑,但为了省事,略,直接在InOut类里编写了响应报文:

package com.study.axis2.receive;

import java.io.ByteArrayInputStream;
import java.util.Iterator; 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.axiom.om.impl.builder.StAXOMBuilder;
import org.apache.axiom.soap.SOAPBody;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axiom.soap.SOAPFactory;
import org.apache.axis2.AxisFault;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.receivers.AbstractInOutMessageReceiver; public class Axis2MessageReceiverInOut extends AbstractInOutMessageReceiver{
private static String ENCODEING = "UTF-8";
private static String NAMING_SPACE = "http://impl.service.axis2.study.com"; @Override
public void invokeBusinessLogic(MessageContext envMsg, MessageContext newEnvMsg)
throws AxisFault {
System.out.println("------------------------------------");
String body = envMsg.getEnvelope().getBody().toString();
System.out.println("request body1 : " + body); //Axis2Service axis2 = new Axis2ServiceImpl(); StringBuffer soapRequestData = new StringBuffer();
soapRequestData.append("<userResponse>");
soapRequestData.append("<rspCode>0000</rspCode>");
soapRequestData.append("<rspDesc>SUCCESS</rspDesc>");
soapRequestData.append("</userResponse>"); SOAPEnvelope env = toEnvelope(soapRequestData.toString()); newEnvMsg.setEnvelope(env);
} public static SOAPEnvelope toEnvelope(String sourceXml)
{
String xmlBody = sourceXml; OMFactory of = OMAbstractFactory.getOMFactory();
OMNamespace bname = of.createOMNamespace(NAMING_SPACE, ""); SOAPFactory s12f = OMAbstractFactory.getSOAP12Factory();
SOAPEnvelope s12e = s12f.getDefaultEnvelope(); try
{
byte[] bytes = xmlBody.getBytes(ENCODEING);
ByteArrayInputStream is = new ByteArrayInputStream(bytes); StAXOMBuilder builder = new StAXOMBuilder(is); OMElement elementBody = builder.getDocumentElement();
//elementBody.setNamespace(bname);
//addNameSpacePrefix(elementBody, bname); SOAPBody s12b = s12e.getBody();
s12b.addChild(elementBody);
}
catch (Exception e)
{
e.printStackTrace();
}
return s12e;
} public static OMElement addNameSpacePrefix(OMElement element,
OMNamespace prefix)
{
if (element.getChildElements() != null)
{
Iterator<OMElement> it = element.getChildElements();
element.setNamespace(prefix);
while (it.hasNext())
{
OMElement childelement = (OMElement) it.next();
childelement.setNamespace(prefix); if ((childelement.getChildElements() == null)
|| (!childelement.getChildElements().hasNext()))
continue;
addChildNameSpacePrefix(childelement.getChildElements(), prefix);
}
} return element;
} public static void addChildNameSpacePrefix(Iterator element,
OMNamespace prefix)
{
if (element != null)
{
while (element.hasNext())
{
OMElement childelement = (OMElement) element.next(); childelement.setNamespace(prefix);
if ((childelement.getChildElements() == null)
|| (!childelement.getChildElements().hasNext()))
continue;
addChildNameSpacePrefix(childelement.getChildElements(), prefix);
}
}
} }

5、将该类和services.xml文件都放在axis2service.aar下 ,并在WEB-INF/services/services.list文件下增加axis2service.aar:

axis2service.aar
version-1.6..aar

服务端编写好,发布,http://localhost:8082/axis2_server_150701/services/axis2Service?wsdl

  <?xml version="1.0" encoding="UTF-8" ?>
- <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:ns="http://impl.service.axis2.study.com" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" targetNamespace="http://impl.service.axis2.study.com">
<wsdl:documentation>axis2Service</wsdl:documentation>
- <wsdl:types>
- <xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://impl.service.axis2.study.com">
- <xs:element name="user">
- <xs:complexType>
- <xs:sequence>
<xs:element minOccurs="0" name="user" nillable="true" type="xs:anyType" />
</xs:sequence>
</xs:complexType>
</xs:element>
- <xs:element name="userResponse">
- <xs:complexType>
- <xs:sequence>
<xs:element minOccurs="0" name="return" nillable="true" type="xs:anyType" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
</wsdl:types>
- <wsdl:message name="userRequest">
<wsdl:part name="parameters" element="ns:user" />
</wsdl:message>
- <wsdl:message name="userResponse">
<wsdl:part name="parameters" element="ns:userResponse" />
</wsdl:message>
- <wsdl:portType name="axis2ServicePortType">
- <wsdl:operation name="user">
<wsdl:input message="ns:userRequest" wsaw:Action="urn:user" />
<wsdl:output message="ns:userResponse" wsaw:Action="urn:userResponse" />
</wsdl:operation>
</wsdl:portType>
- <wsdl:binding name="axis2ServiceSoap11Binding" type="ns:axis2ServicePortType">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
- <wsdl:operation name="user">
<soap:operation soapAction="urn:user" style="document" />
- <wsdl:input>
<soap:body use="literal" />
</wsdl:input>
- <wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
- <wsdl:binding name="axis2ServiceSoap12Binding" type="ns:axis2ServicePortType">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
- <wsdl:operation name="user">
<soap12:operation soapAction="urn:user" style="document" />
- <wsdl:input>
<soap12:body use="literal" />
</wsdl:input>
- <wsdl:output>
<soap12:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
- <wsdl:binding name="axis2ServiceHttpBinding" type="ns:axis2ServicePortType">
<http:binding verb="POST" />
- <wsdl:operation name="user">
<http:operation location="user" />
- <wsdl:input>
<mime:content type="application/xml" part="parameters" />
</wsdl:input>
- <wsdl:output>
<mime:content type="application/xml" part="parameters" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
- <wsdl:service name="axis2Service">
- <wsdl:port name="axis2ServiceHttpSoap11Endpoint" binding="ns:axis2ServiceSoap11Binding">
<soap:address location="http://localhost:8082/axis2_server_150701/services/axis2Service.axis2ServiceHttpSoap11Endpoint/" />
</wsdl:port>
- <wsdl:port name="axis2ServiceHttpSoap12Endpoint" binding="ns:axis2ServiceSoap12Binding">
<soap12:address location="http://localhost:8082/axis2_server_150701/services/axis2Service.axis2ServiceHttpSoap12Endpoint/" />
</wsdl:port>
- <wsdl:port name="axis2ServiceHttpEndpoint" binding="ns:axis2ServiceHttpBinding">
<http:address location="http://localhost:8082/axis2_server_150701/services/axis2Service.axis2ServiceHttpEndpoint/" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

axis2可以访问http://localhost:8082/axis2_server_150701/services/listServices来查看发布了哪些接口:

以上服务端已完全搭好,现编写客户端client,服务端增加个User类:

package com.study.axis2.client;

import java.io.ByteArrayInputStream;
import java.io.InputStream; import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity; import com.study.axis2.domain.User; public class Axis2Client {
private static String url = "http://localhost:8082/axis2_server_150701/services/axis2Service"; public static void main(String[] args) { User user = new User();
user.setUserId(11111);
user.setUserName("66666"); HttpClient client = new HttpClient();
PostMethod method = new PostMethod(url);
StringBuffer soapRequestData = new StringBuffer();
soapRequestData.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
soapRequestData
.append("<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
+ " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\""
+ " xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">");
soapRequestData.append("<soap12:Body>");
soapRequestData.append("<user xmlns=\"http://impl.service.axis2.study.com\">");
soapRequestData.append("<userId>" + user.getUserId() + "</userId>");
soapRequestData.append("<userName>" + user.getUserName() + "</userName>");
soapRequestData.append("</user>");
soapRequestData.append("</soap12:Body>");
soapRequestData.append("</soap12:Envelope>"); String xml = soapRequestData.toString(); byte[] b; try
{
b = xml.getBytes("utf-8"); System.out.println("request : " + xml);
InputStream is = new ByteArrayInputStream(b, 0, b.length);
RequestEntity re = new InputStreamRequestEntity(is, b.length, "application/soap+xml; charset=utf-8");
method.setRequestEntity(re); client.executeMethod(method);
String response = method.getResponseBodyAsString();
System.out.println("response : " + response);
}
catch (Exception e)
{
e.printStackTrace();
}
} }

执行客户端代码,输出(输出结果内容为xml,稍微format下):

request :
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<user xmlns="http://impl.service.axis2.study.com">
<userId>11111</userId>
<userName>yff</userName>
</user>
</soap12:Body>
</soap12:Envelope>
response :
<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<soapenv:Body>
<userResponse>
<rspCode>0000</rspCode>
<rspDesc>SUCCESS</rspDesc>
</userResponse>
</soapenv:Body>
</soapenv:Envelope>

Axis2学习的第一天的更多相关文章

  1. RabbitMQ学习总结 第一篇:理论篇

    目录 RabbitMQ学习总结 第一篇:理论篇 RabbitMQ学习总结 第二篇:快速入门HelloWorld RabbitMQ学习总结 第三篇:工作队列Work Queue RabbitMQ学习总结 ...

  2. 学习KnockOut第一篇之Hello World

    学习KnockOut第一篇之Hello World 笔者刚开始学习KnockOut.写的内容就相当于一个学习笔记.且在此处向官网致敬,比较喜欢他们家的Live Example版块,里面有jsFiddl ...

  3. ActionBarSherlock学习笔记 第一篇——部署

    ActionBarSherlock学习笔记 第一篇--部署          ActionBarSherlock是JakeWharton编写的一个开源框架,使用这个框架,可以实现在所有的Android ...

  4. Java学习记录第一章

    学习Java第一章的记录,这一章主要记录的是Java的最基础部分的了解知识,了解Java的特性和开发环境还有Java语言的优缺点. 计算机语言的发展大概过程:机器语言--->汇编语言---> ...

  5. oracle学习笔记第一天

    oracle学习笔记第一天 --oracle学习的第一天 --一.几个基础的关键字   1.select select (挑选) 挑选出显示的--列--(可以多列,用“,”隔开,*表示所有列),为一条 ...

  6. javascript的ES6学习总结(第一部分)

    ES6(ESNext学习总结——第一部分) ES6, 全称 ECMAScript 6.0 ,是 JavaScript 的下一个版本标准,2015.06 发版. ECMA每年6月份,发布一个版本 201 ...

  7. Web基础学习---HTML 第一天

    Web基础学习---HTML 第一天 1 HTML标签 2.CSS Web开发基础HTML好吧离开Python几天...如何学好前端?? 多去看别人的网站.多看.多写.多练,(知乎.36Kr.)多练就 ...

  8. QT学习之第一个程序

    QT学习之第一个程序 目录 手动创建主窗口 居中显示 添加窗口图标 显示提示文本 Message Box的应用 手动连接信号与槽 手动创建主窗口 窗口类型 QMainWindow: 可以包含菜单栏.工 ...

  9. 创芯Xilinx Microblaze 学习系列第一集

    创芯Xilinx Microblaze 学习系列第一集 Xilinx ISE Design Suite 13.2 The MicroBlaze™ embedded processor soft cor ...

随机推荐

  1. Linux系统木马后门查杀方法详解

    木马和后门的查杀是系统管理员一项长期需要坚持的工作,切不可掉以轻心.以下从几个方面在说明Linux系统环境安排配置防范和木马后门查杀的方法: 一.Web Server(以Nginx为例) 1.为防止跨 ...

  2. java数据结构和算法------快速排序

    package iYou.neugle.sort; public class Quick_sort { public static void QuickSort(double[] array, int ...

  3. 谈谈 Repository、IUnitOfWork 和 IDbContext 的实践

    谈谈 Repository.IUnitOfWork 和 IDbContext 的实践 上一篇:<DDD 领域驱动设计-谈谈 Repository.IUnitOfWork 和 IDbContext ...

  4. PBOC电子钱包与电子现金及QPBOC

    电子钱包:EP 电子现金:EC,在PBOC规范中的13部分定义了<基于借贷记应用的小额支付规范中> QPBOC:在PBOC规范的12部分中定义了<费接触式IC卡支付规范> PB ...

  5. Notes of the scrum meeting(12.5)

    meeting time:18:00~18:30p.m.,December 5th,2013 meeting place:3号公寓一层 attendees: 顾育豪                   ...

  6. 课题练习——找从1到N出现的1的个数

    #include<iostream.h>#include<conio.h>int Sum1(int n){ int count = 0; //记录1的个数 int factor ...

  7. 【转】oracle number与java中long、int的对应

    Oracle数据库中number类型在hibernate的引用 1)如果不指定number的长度,或指定长度n>18 id number not null,转换为pojo类时,为java.mat ...

  8. 【Binary Tree Post order Traversal】cpp

    题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given bina ...

  9. TCL随记(2)

    file函数: file dirname name 返回文件所在目录 file exists name 测试文件是否存在,存在返回1,否则返回0 file extension name 返回文件扩展名 ...

  10. codeforces 161D Distance in Tree 树形dp

    题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...