我正在使用axis2来提供一个基本的Web服务,它将获取文件名作为参数,并生成一个响应SOAP数据包,该数据包将随SOAP一起附加文件.
这是我创建服务代码的方式(它的简单和灵感来自Axis2示例代码)
public String getFile(String name) throws IOException
{
MessageContext msgCtx = MessageContext.getCurrentMessageContext();
File file = new File (name);
System.out.println("File = " + name);
System.out.println("File exists = " + file.exists());
FileDataSource fileDataSource = new FileDataSource(file);
System.out.println("fileDataSource = " + fileDataSource);
DataHandler dataHandler = new DataHandler(fileDataSource);
System.out.println("DataHandler = " + dataHandler);
String attachmentID = msgCtx.addAttachment(dataHandler);
System.out.println("attachment ID = " + attachmentID);
return attachmentID;
}
现在客户端代码 –
MessageContext response = mepClient
.getMessageContext(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
SOAPBody body = response.getEnvelope().getBody();
OMElement element = body.getFirstElement().getFirstChildWithName(
new QName("http://service.soapwithattachments.sample","return"));
String attachementId = element.getText();
System.out.println("attachment id is " + attachementId);
Attachments attachment = response.getAttachmentMap();
DataHandler dataHandler = attachment.getDataHandler(attachementId);
问题是dataHandler始终为null.虽然我认为在服务器端,文件已被读取并与SOAP数据包一起附加.难道我做错了什么 ?
编辑:
我把< parameter name =“enableSwA”locked =“false”> true< / parameter>在axis2.xml文件中.
最佳答案 我找到了这个问题的解决方案.
问题是,在服务器端,使用MessageContext msgCtx = MessageContext.getCurrentMessageContext(); call,我们得到传入消息上下文的句柄.我在传入的消息上下文中添加了附件,而附件需要添加到传出消息上下文中.
要获取传出消息上下文的句柄,需要执行以下步骤 –
//this is the incoming message context
MessageContext inMessageContext = MessageContext.getCurrentMessageContext();
OperationContext operationContext = inMessageContext.getOperationContext();
//this is the outgoing message context
MessageContext outMessageContext = operationContext.getMessageContext(WSDLConstants.MESSAGE_LABEL_OUT_VALUE);
收到传出消息上下文后,在此处添加附件 –
String attachmentID = outMessageContext.addAttachment(dataHandler);
其余代码保持不变.
更多相关信息可以在here找到.