以前我使用axis为我的Web服务实现了一个处理程序,它将读取xml文件并将其作为响应返回。我的代码如下:
try {
Message msg = msgContext.getResponseMessage();
MessageContext context = MessageContext.getCurrentContext();
InputStream in = this.getClass().getClassLoader().getResourceAsStream("login_rsp_xml");
if(in==null){
System.out.println("in is empty!!!");
}
DocumentBuilderFactory dbFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(in);
msg.getSOAPPart().setContent(new DOMSource(doc));
System.out.println("return file>>"+fileName);
} catch (Exception e) {
System.setProperty("req_no", null);
e.printStackTrace();
}
现在,我需要使用axis2做同样的事情。有谁知道如何更换axis2中的传出响应?我确实尝试将文件转换为字符串并调用envelope.setText(),但失败并获得异常“org.apache.axiom.soap.SOAPProcessingException: Can not detach SOAP Body, SOAP Envelope must have a Body !!
”。
提前感谢您的帮助/建议。
我在axis2中的代码:
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.impl.builder.StAXOMBuilder;
import org.apache.axiom.om.util.AXIOMUtil;
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.context.ServiceContext;
import org.apache.axis2.description.AxisMessage;
import org.apache.axis2.description.AxisService;
import org.apache.axis2.engine.Handler;
import org.apache.axis2.handlers.AbstractHandler;
import org.apache.axis2.jaxws.message.Message;
import org.apache.axis2.transport.http.HTTPConstants;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class SamRspHandler extends AbstractHandler implements Handler {
public String getFileContent(String path) throws Exception{
InputStream in = this.getClass().getClassLoader()
//.getResourceAsStream("/xmlFiles/loginRspHeader.xml");
.getResourceAsStream(path);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc2 = dBuilder.parse(in);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
"yes");
StringWriter writer = new StringWriter();
transformer
.transform(new DOMSource(doc2), new StreamResult(writer));
String output2 = writer.getBuffer().toString();
return output2;
}
public InvocationResponse invoke(MessageContext msgContext)
throws AxisFault {
System.out.println("In Response");
String content = msgContext.getEnvelope().toString();
try {
System.out.println("zzzz>>"+System.getProperty("abc"));
msgContext.getEnvelope().setText("abc");
} catch (Exception e) {
e.printStackTrace();
}
return InvocationResponse.CONTINUE;
}
public void revoke(MessageContext msgContext) {
}
}
答案 0 :(得分:0)
如果要在SOAP响应中设置XML文件,则必须将其封装在一个
<![CDATA[]>
标记。这是为了防止XML文件作为SOAP响应的一部分进行处理。请注意,SOAP还使用XML来处理请求和响应。
<![CDATA[
<?xml version="1.0"?>
<!-- Your XML -->
]>
如上所述创建XML文件并将其设置为envelope.setText()