1

以前,我确实使用轴为我的 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中的传出响应?我确实尝试将文件转换为字符串并调用信封.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) {

    }


}
4

1 回答 1

0

如果您在 SOAP 响应中设置 XML 文件,则它必须包含在 <![CDATA[]>标记内。这是为了防止将 XML 文件作为 SOAP 响应的一部分进行处理。请注意,SOAP 还使用 XML 来处理请求和响应。

<![CDATA[
    <?xml version="1.0"?>
    <!-- Your XML -->
]>

如上所述创建您的 XML 文件并设置它envelope.setText()

于 2013-10-09T07:35:21.163 回答