- 浏览: 260754 次
- 性别:
- 来自: 济南
文章分类
- 全部博客 (303)
- c (31)
- c++ (16)
- java (18)
- c# (1)
- python (3)
- java web (6)
- oracle (7)
- sqlserver (2)
- mysql (2)
- android (24)
- android系统 (15)
- android多媒体部分 (15)
- android游戏 (12)
- linux (26)
- javaScript (1)
- ajax (1)
- node JS (2)
- html (5)
- apache (3)
- jboss (1)
- weblogic (0)
- 通信协议 (10)
- 云计算 (1)
- 分布式 (5)
- ejb (1)
- webservice (5)
- 设计模式 (16)
- JNI (6)
- swing (13)
- 版本控制 (1)
- UML (1)
- xml (4)
- spring (5)
- hibernate (5)
- struts1 (3)
- struts2 (4)
- ibatis (0)
- tomcat (2)
- 心得体会 (1)
- css (1)
- 嵌入式 (41)
- arm体系结构 (10)
soap协议
1 客户端与服务端使用soap进行通信
示例代码:
service端:
/**
* 使用soap协议进行通信
*
* @time 下午7:40:34
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class SoapService {
public static OMElement requestSoap = null;
public OMElement request(OMElement soapBody) {
requestSoap = soapBody;
Iterator it = requestSoap.getChildElements();
OMElement issuerElement = (OMElement) it.next();
OMElement serialElement = (OMElement) it.next();
OMElement revocationDateElement = (OMElement) it.next();
String issuer = issuerElement.getText();
String serial = serialElement.getText();
String revocationDate = revocationDateElement.getText();
System.out.println("issuer=" + issuer + "\n serial=" + serial +
"revocationDate=" + revocationDate);
OMFactory factory = OMAbstractFactory.getOMFactory();
OMNamespace namespace = factory.createOMNamespace
("http://soapstyle.axis2service.yue.cn", "");
OMElement soapResponse = factory.createOMElement("SoapResponse",
namespace);
OMElement soaplssuer = factory.createOMElement("Issuer", namespace);
soaplssuer.setText("issuer:" + issuer);
soapResponse.addChild(soaplssuer);
OMElement soapSerial = factory.createOMElement("Serial", namespace);
soapSerial.setText("serial:" + serial);
soapResponse.addChild(soapSerial);
OMElement soapRevokeDate = factory.createOMElement("RevocationDate",
namespace);
soapRevokeDate.setText("RevocationDate" + revocationDate);
soapResponse.addChild(soapRevokeDate);
soapResponse.build();
return soapResponse;
}
}
<service name="SoapService" >
<description>
revoking certificate.
</description>
<parameter name="ServiceClass"
locked="false">cn.yue.axis2service.soapstyle.SoapService</parameter>
<operation name="request">
<messageReceiver
class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
<actionMapping>urn:request</actionMapping>
</operation>
</service>
客户端
/**
* soap协议通信客户端
*
* 非阻塞(异步)全双工
*
* @time 下午4:04:55
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class SoapServiceDualNonBlock {
private static EndpointReference endpointReference = new EndpointReference
("http://localhost:8089/services/SoapService");
private static boolean finish = false;
public void request() {
OMFactory factory = OMAbstractFactory.getOMFactory();
OMNamespace namespace = factory.createOMNamespace
("http://soapstyle.axis2service.yue.cn", "");
OMElement issuer = factory.createOMElement("Issuer", namespace);
OMElement serial = factory.createOMElement("Serial", namespace);
OMElement revocationDate = factory.createOMElement("RevocationDate",
namespace);
issuer.setText("yuezhenhua");
serial.setText("1234567");
revocationDate.setText("2012-11-21");
OMElement requestSoapMessage = factory.createOMElement("request",
namespace);
requestSoapMessage.addChild(issuer);
requestSoapMessage.addChild(serial);
requestSoapMessage.addChild(revocationDate);
requestSoapMessage.build();
Options options = new Options();
options.setTo(endpointReference);
ServiceClient sender = null;
try {
AxisCallback callback = new AxisCallback() {
@Override
public void onMessage(MessageContext msgContext) {
OMElement result = msgContext.getEnvelope
().getBody().getFirstElement();
System.out.println("result:" + result);
finish = true;
}
@Override
public void onFault(MessageContext msgContext) {
System.out.println("onFault=" +
msgContext.getEnvelope().getBody().getFault().toString());
}
@Override
public void onError(Exception e) {
e.printStackTrace();
}
@Override
public void onComplete() {
System.out.println("onComplete");
}
};
sender = new ServiceClient();
sender.setOptions(options);
System.out.println("=======invoke the service===");
sender.sendReceiveNonBlocking(requestSoapMessage, callback);
synchronized (callback) {
if (!finish) {
try {
callback.wait(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
if (!finish) {
throw new AxisFault("Server was shutdown as
the async response take too long to complete");
}
}
} catch (AxisFault e1) {
e1.printStackTrace();
} finally {
if (null != sender) {
try {
sender.cleanup();
} catch (AxisFault e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
SoapServiceDualNonBlock client = new SoapServiceDualNonBlock();
client.request();
}
}
2 服务端将exception以soapFault的形式抛给客户端
/**
* 将exception以soapfault形式抛给客户端
*
* @time 下午9:00:52
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class SoapFaultService {
private int i = 0;
public OMElement getPrice(OMElement request) throws AxisFault {
if (null == request) {
SOAPFault fault = getSoapFault();
return fault;
}
OMFactory factory = OMAbstractFactory.getOMFactory();
OMNamespace namespace = factory.createOMNamespace
("http://soapstyle.axis2service.yue.cn", "");
OMElement response = factory.createOMElement("Price", namespace);
response.setText(String.valueOf(i++));
return response;
}
private SOAPFault getSoapFault() {
MessageContext context = MessageContext.getCurrentMessageContext();
SOAPFactory factory = null;
if (context.isSOAP11()) {
factory = OMAbstractFactory.getSOAP11Factory();
} else {
factory = OMAbstractFactory.getSOAP12Factory();
}
SOAPFault fault = factory.createSOAPFault();
SOAPFaultCode faultCode = factory.createSOAPFaultCode(fault);
faultCode.setText("13");
factory.createSOAPFaultValue(faultCode);
SOAPFaultReason faultReason = factory.createSOAPFaultReason(fault);
faultReason.setText("request can not be null!");
factory.createSOAPFaultText(faultReason);
factory.createSOAPFaultDetail(fault);
return fault;
}
}
<service name="SoapFaultService" >
<description>
revoking certificate.
</description>
<parameter name="ServiceClass"
locked="false">cn.yue.axis2service.soapstyle.SoapFaultService</parameter>
<operation name="getPrice">
<messageReceiver
class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
<actionMapping>urn:getPrice</actionMapping>
</operation>
</service>
客户端
/**
* 客户端接收从服务器端传来的以soapfault形式的exception
*
* 非阻塞(异步)全双工
*
* @time 下午4:04:55
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class SoapFaultServiceDualNonBlock {
private static EndpointReference endpointReference = new EndpointReference
("http://localhost:8089/services/SoapFaultService");
private static boolean finish = false;
ServiceClient sender = null;
public void getPrice() {
try {
OMFactory factory = OMAbstractFactory.getOMFactory();
OMNamespace namespace = factory.createOMNamespace
("http://soapstyle.axis2service.yue.cn", "");
// OMElement request = factory.createOMElement("Price",
namespace);
Options options = new Options();
options.setTo(endpointReference);
options.setAction("urn:getPrice");
options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
options.setUseSeparateListener(true);
AxisCallback callback = new AxisCallback() {
@Override
public void onMessage(MessageContext msgContext) {
OMElement result = msgContext.getEnvelope
().getBody().getFirstElement();
System.out.println("price:" + result);
finish = true;
}
// 用于接收服务端抛过来的exception
@Override
public void onFault(MessageContext msgContext) {
QName errorCode = new QName("faultcode");
QName reason = new QName("faultString");
OMElement faultElement =
msgContext.getEnvelope().getBody().getFault();
System.out.println("ErrorCode[" +
faultElement.getFirstChildWithName(errorCode).getText() + "] caused by:" +
faultElement.getFirstChildWithName(reason).getText());
}
@Override
public void onError(Exception e) {
e.printStackTrace();
}
@Override
public void onComplete() {
System.out.println("onComplete");
}
};
sender = new ServiceClient();
sender.setOptions(options);
sender.engageModule("addressing");
try {
sender.sendReceiveNonBlocking(null, callback);
} catch (Exception e2) {
e2.printStackTrace();
System.out.println(e2.getMessage());
}
synchronized (callback) {
if (!finish) {
try {
callback.wait(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
} catch (AxisFault e) {
e.printStackTrace();
} finally {
try {
sender.cleanup();
} catch (AxisFault e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
SoapFaultServiceDualNonBlock client = new
SoapFaultServiceDualNonBlock();
client.getPrice();
}
}
3 使用swa(soap with attachment)来进行附件的传送
axis2有两种附件传输形式
mtom
swa soap with attachment
客户端把要上传的文件,转成二进制码由soap的request一起发送到服务器端(attchmentID)
/**
* 接收客户端上传文件
*
* @time 下午7:47:34
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class FileUploadService {
public String uploadFile(String name, String attchmentID) {
FileOutputStream fileOutputStream = null;
StringBuffer uploadFilePath = new StringBuffer();
String fileNamePrefix = "";
String fileName = "";
try {
MessageContext context =
MessageContext.getCurrentMessageContext();
Attachments attachments = context.getAttachmentMap();
DataHandler dataHandler = attachments.getDataHandler
(attchmentID);
fileNamePrefix = name.substring(name.indexOf("."),
name.length());
fileName = "11";
System.out.println("fileName:" + fileName);
System.out.println("fileNamePrefix:" + fileNamePrefix);
uploadFilePath.append("d:/");
uploadFilePath.append(fileName);
uploadFilePath.append(fileNamePrefix);
System.out.println("uploadFilePath:" + uploadFilePath);
File file = new File(uploadFilePath.toString());
fileOutputStream = new FileOutputStream(file);
dataHandler.writeTo(fileOutputStream);
fileOutputStream.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != fileOutputStream) {
fileOutputStream.close();
}
fileOutputStream = null;
} catch (IOException e) {
e.printStackTrace();
}
}
return "success";
}
}
<service name="FileUploadService" >
<parameter name="ServiceClass"
locked="false">cn.yue.axis2service.fileupload.FileUploadService</parameter>
<operation name="uploadFile">
<messageReceiver
class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
<actionMapping>urn:uploadFile</actionMapping>
</operation>
</service>
客户端
/**
* 上传文件
*
* @time 下午8:07:05
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class FileUploadClient {
private static EndpointReference reference = new EndpointReference
("http://localhost:8089/services/FileUploadService");
public void uploadFile() {
try {
String filePath = "d:/AppCan/xizan.jpg";
String destFile = "xizan.jpg";
Options options = new Options();
options.setTo(reference);
options.setProperty(Constants.Configuration.ENABLE_SWA,
Constants.VALUE_TRUE);
options.setSoapVersionURI
(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
options.setTimeOutInMilliSeconds(10000);
options.setAction("urn:uploadFile");
ConfigurationContext context =
ConfigurationContextFactory.createConfigurationContextFromFileSystem
("D:/workspace4/axis2webservice/WebContent/WEB-INF/modules", null);
ServiceClient sender = new ServiceClient(context, null);
OperationClient mepClient = sender.createClient
(ServiceClient.ANON_OUT_IN_OP);
MessageContext messageContext = new MessageContext();
FileDataSource fileDataSource = new FileDataSource(new File
(filePath));
DataHandler handler = new DataHandler(fileDataSource);
String attachmentID = messageContext.addAttachment(handler);
SOAPFactory factory = OMAbstractFactory.getSOAP11Factory();
SOAPEnvelope envelope = factory.getDefaultEnvelope();
OMNamespace namespace = factory.createOMNamespace
("http://fileupload.axis2service.yue.cn", "");
OMElement uploadFile = factory.createOMElement("uploadFile",
namespace);
OMElement nameEle = factory.createOMElement("name",
namespace);
nameEle.setText(destFile);
OMElement idEle = factory.createOMElement("attchmentID",
namespace);
idEle.setText(attachmentID);
uploadFile.addChild(nameEle);
uploadFile.addChild(idEle);
envelope.getBody().addChild(uploadFile);
System.out.println("message==" + envelope);
messageContext.setEnvelope(envelope);
mepClient.addMessageContext(messageContext);
mepClient.execute(true);
MessageContext response = mepClient.getMessageContext
(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
SOAPBody body = response.getEnvelope().getBody();
OMElement element = body.getFirstChildWithName(new QName
("http://fileupload.axis2service.yue.cn", "return"));
System.out.println(element.getText());
} catch (AxisFault e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
FileUploadClient client = new FileUploadClient();
client.uploadFile();
}
}
发表评论
-
axis2 webservice学习笔记二
2012-11-19 20:27 965使用简单java类型构建webservice 示例代码如下: ... -
axis2 webservice学习笔记一
2012-11-18 16:34 2657axis2 webservice axis 包括以下几部分 ... -
webService学习笔记
2012-09-05 11:22 784面向服务的架构 异构系统间的远程调用 远程调用 面向过程 ... -
ssh整合webservice cxf采用注解的方式+andriod客户端
2012-10-17 22:16 1347定义接口和实现类 /** * 自定义service接口 ...
相关推荐
在本学习笔记中,主要介绍了使用Apache Axis2框架来开发和测试Web Service的过程。Apache Axis2是Apache SOAP栈的一个实现,提供了简单且高效的Web Service开发工具。 首先,开发者需要在Eclipse集成开发环境中搭建...
在本篇WebService学习笔记中,我们将探讨几个关键的概念和技术,包括SOAP协议、JAX-WS、WSDL文档以及一些常用的Web服务框架。 首先,SOAP(Simple Object Access Protocol)是一种基于XML的协议,用于在分布式环境...
当我们谈论“Axis2,CXF版本学习笔记”时,这通常涉及到两个主要的Java Web服务框架:Apache Axis2和Apache CXF。这两个框架都用于构建和部署SOAP(简单对象访问协议)和RESTful(Representational State Transfer)...
【WebService学习笔记0001】 在IT领域,WebService是一种基于开放标准(如XML、SOAP、WSDL和UDDI)的互操作性技术,它允许不同系统间的应用程序通过网络进行通信。本学习笔记将围绕WebService的核心概念、工作原理...
axis1.4 webservice个人学习笔记
**Axis学习笔记(网页转贴)** Axis是一个开源的Java库,主要用于创建和使用Web服务。它是Apache软件基金会的一部分,广泛应用于开发基于SOAP(简单对象访问协议)的Web服务。本学习笔记将深入探讨Axis在Web服务开发...
标题“Axis2学习笔记”揭示了本篇内容主要围绕Apache Axis2框架展开,这是一个用于构建Web服务和客户端的开源工具。Axis2是基于Axis1的升级版,它提供了更高效、模块化和可扩展的架构,使得开发和部署Web服务变得...
3. **WebService 基础**:深入探讨了 WebService 的概念和技术栈。 4. **WebService 在 EBS 中的应用**:具体介绍了如何在 Oracle EBS 不同版本中部署和使用 WebService。 5. **其他注意事项**:列举了一些额外的...
6. **工具与框架**:在学习过程中,可能会用到各种工具和框架,例如Apache Axis、CXF、Gson、JAXB等,它们可以帮助简化Web服务的开发和测试。 7. **安全性**:Web服务的安全性非常重要,包括身份验证、授权、数据...
axis学习资料汇总: Axis实例与分析详解; Axis学习笔记.pdf; Tomcat5.0.28下AXIS完全安装手册.doc; Axis1.4 开发指南_V1.0.pdf; AXIS入门及应用.rar; Axis开发Web+Services.doc 等等
项目收集的axis的相关资料~~ Axis.pdf ...AXIS学习笔记.txt WebService之axis的复杂对象传输方案.txt 使用Apache Axis部署 Web服务时的常见问题及其解决方法.txt 使用Axis开发Web Service程序.txt
"新建 文本文档.txt"可能是一份简单的文本文件,可能包含笔记、命令行指令或者一些关键概念的解释,对于学习过程来说也是重要的参考资料。 "axis_百度百科.files"和"JAVA WebService实例 - 我的文章 - 阳光博客(IT...
AXIS2是一个SOAP引擎,用于处理Web服务,其lib目录下的jar包是开发WebService业务的关键依赖。 配置环节,需要将AXIS2的bin目录添加到系统Path环境变量中,这样可以在命令行中直接调用AXIS2的相关工具。将axis2.war...
文件名“63535299webservice-example.rar”可能包含了一个Web服务的示例代码,这可能是用Java或C#实现的一个基本服务,可以用来学习如何定义服务操作、处理请求和返回响应。 “105230312stock.rar”可能是一个关于...