如何使用apache.cxf java从响应信封中提取soap标头

时间:2019-05-28 09:47:57

标签: java soap cxf

以下是来自服务器的响应:

enter image description here

我尝试使用以下代码,但对我而言不起作用

 public void handleMessage(SoapMessage message) throws Fault {
        SOAPMessage sm = message.getContent(SOAPMessage.class);
        try {
            System.out.println("In side read intercepter..");
            String soapheader=sm.getSOAPPart().getEnvelope().getHeader().getTextContent();
            System.out.println("SOAP Env Header:=>"+soapheader);
        } catch (SOAPException e) {
            throw new Fault(e);
        } 
    }

预期结果:

<e:Header>
        <Friends>
            <friend>
                <Name>Testabc</Name>
                <Age>12121</Age>
                <Phone>Testpqr</Phone>
            </friend>
        </Friends>
</e:Header>

2 个答案:

答案 0 :(得分:1)

我已经使用xpath并尝试了上述解决方案。

请检查是否可以帮到您。

注意:请以适当的方式处理异常:)

public static void main(String[] args)
            throws SAXException, IOException, ParserConfigurationException, XPathExpressionException, TransformerException {

        File fXmlFile = new File("C://Users//abhijitb//Desktop//Test.xml");
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        Document document = dbf.newDocumentBuilder().parse(fXmlFile);

        XPath xPath = XPathFactory.newInstance().newXPath();
        String xpathStr = "//Envelope//Header";
        Node result = (Node) xPath.evaluate(xpathStr, document, XPathConstants.NODE);

        // print the output to verify.
        System.out.println(nodeToString(result));
    }

    private static String nodeToString(Node node) throws TransformerException {
        StringWriter buf = new StringWriter();
        Transformer xform = TransformerFactory.newInstance().newTransformer();
        xform.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        xform.transform(new DOMSource(node), new StreamResult(buf));
        return (buf.toString());
    }

其输出如下:

<e:Header>
   <Friends>
       <friend>
           <Name>Testabc</Name>
           <Age>12121</Age>
           <Phone>Testpqr</Phone>
        </friend>
   </Friends>
</e:Header>

答案 1 :(得分:0)

在下面检查。 提取响应标题。

public class SOAPRSPInt extends AbstractSoapInterceptor {

    private static final Logger log = LoggerFactory.getLogger(SOAPRSPInt.class);

    public SOAPRSPInt() {
        super(Phase.POST_INVOKE);
    }

    @Override
    public void handleMessage(SoapMessage message) throws Fault {
        List<Header> list = message.getHeaders();

        log.debug("SOAPRSPInt message: '{}'", ToStringBuilder.reflectionToString(message),
                ToStringStyle.MULTI_LINE_STYLE);

        String hfrMSG = "SOAPRSPInt message ";
        String resultMSG = "";
        for (Header header : list) {

            hfrMSG += "  '" + header.getName() + "':" + 
                    ((ElementNSImpl) header.getObject()).getTextContent() + "\n";

            if (header.getName().toString().equalsIgnoreCase("{urn:sap-com:document:sap:rfc:functions}ResultStatus")) {
                resultMSG = ((ElementNSImpl) header.getObject()).getTextContent();
            }
        }

        log.debug("SOAPRSPInt headers: '{}'", hfrMSG);

        log.debug("SOAPRSPInt ResultMSG: '{}'", resultMSG);


    }
    
}