如何以属性名称/值格式发送和接收soap消息?

时间:2016-02-08 05:44:39

标签: web-services soap

需要在Web服务中设置什么才能以param属性格式发送和接收soap消息?例如,我需要使用此属性名称/值格式的参数发送soap请求:

<param name="controller_name">CPA Central</param>

并以类似的属性名称/值格式接收它们:

<attribute name="channel_number">1</attribute>

我用谷歌搜索了14个小时,但却找不到怎么做!如果有人能指出我正确的方向,我会非常感激。

1 个答案:

答案 0 :(得分:1)

以下是我最终如何做到这一点。似乎很荒谬,你必须拦截传出的肥皂信息并重新格式化,而不是以正确的格式创建它。但是,如果你像我一样被迫使用像JAX-WS这样的魔法黑盒子,你就会坚持使用它们提供的自动格式。

public class InneoquestLogicalHandler implements LogicalHandler<LogicalMessageContext> {
    private static Logger logger = Logger.getLogger(InneoquestSoapHandler.class);

    @Override
    public boolean handleMessage(LogicalMessageContext context) {
        boolean isResponse = (Boolean)context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
        if(!isResponse){
            logger.debug("InneoquestLogicalHandler.handleMessage(): this is a soap request");
        }
        else {
            logger.debug("InneoquestLogicalHandler.handleMessage(): this is a soap response");
            try {
                try {
                    transform(context);
                } catch (TransformerConfigurationException ex) {
                    java.util.logging.Logger.getLogger(InneoquestLogicalHandler.class.getName()).log(Level.SEVERE, null, ex);
                } catch (SOAPException ex) {
                    java.util.logging.Logger.getLogger(InneoquestLogicalHandler.class.getName()).log(Level.SEVERE, null, ex);
                }
            } catch (TransformerException ex) {
                java.util.logging.Logger.getLogger(InneoquestLogicalHandler.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    return true;
    }

    private void transform(LogicalMessageContext context) throws TransformerConfigurationException, TransformerException, SOAPException {
        LogicalMessage msg = context.getMessage();
        Source source = msg.getPayload();
        Transformer xFormer = TransformerFactory.newInstance().newTransformer();
        xFormer.setOutputProperty("omit-xml-declaration", "yes");
        DOMResult result = new DOMResult();
        xFormer.transform(source,result);
        Document doc = (Document) result.getNode();
        transformNodeList(doc,doc.getChildNodes());
        source = new DOMSource(doc);
        msg.setPayload(source);
    }

    private void transformNodeList(Document doc, NodeList nodeList) {
        for (int i=0; i< nodeList.getLength(); i++) {
            Node childNode = nodeList.item(i);
            if (childNode.getNodeName().equals("channel_number")) {
                Element elem = (Element)childNode;
                doc.renameNode(elem, elem.getNamespaceURI(), "attribute");
                elem.setAttribute("name", "channel_number");
            }
            else if (childNode.getNodeName().equals("count")) {
                Element elem = (Element)childNode;
                doc.renameNode(elem, elem.getNamespaceURI(), "response");
                elem.setAttribute("rows", elem.getTextContent());
                elem.setTextContent("");
                elem.setAttribute("type", "success");
            }

            NodeList children = childNode.getChildNodes();
            if (children != null) {
                transformNodeList(doc,children);
            }
        }
    }