1

以前は、xml ファイルを読み取って応答として返す軸を使用して、Web サービス用のハンドラーを 1 つ実装していました。私のコードは以下の通りです:

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) {

    }


}
4

1 に答える 1