添加SOAP消息体的名称空间

时间:2014-07-31 14:03:39

标签: java xml soap xml-namespaces soapheader

我想为SOAPbody中的所有项添加名称空间,与XML操作相关的配置在下面我添加了标题的名称空间但它没有来到SOAP-body

Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

        if (outboundProperty.booleanValue()) { 
            SOAPMessage message = smc.getMessage(); 
            try {

                SOAPEnvelope envelope = smc.getMessage().getSOAPPart().getEnvelope();
                System.out.println("Envelop : "+envelope);

                envelope.addNamespaceDeclaration("sch", "http://api.jasperwireless.com/ws/schema");

                SOAPHeader header = envelope.addHeader();

                SOAPElement security =
                        header.addChildElement("Security", "wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");

                SOAPElement usernameToken =
                        security.addChildElement("UsernameToken", "wsse");
                usernameToken.addAttribute(new QName("xmlns:wsu"), "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");

                SOAPElement username =
                        usernameToken.addChildElement("Username", "wsse");
                username.addTextNode("XXXXXXXXX");

                SOAPElement password =
                        usernameToken.addChildElement("Password", "wsse");
                password.setAttribute("Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");
                password.addTextNode("XXXXXXXXXXXXXX");

                //Print out the outbound SOAP message to System.out
                message.writeTo(System.out);
                System.out.println("");

            } catch (Exception e) {
                e.printStackTrace();
            }

及以下是从上面的代码

创建的XML
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sch="http://api.jasperwireless.com/ws/schema">
        <S:Header>
            <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
                <wsse:UsernameToken xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
                    <wsse:Username>XXXXXX</wsse:Username>
                    <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">XXXXXXX</wsse:Password>
                </wsse:UsernameToken>
            </wsse:Security>
        </S:Header>
        <S:Body>
            <GetTerminalsByMsisdnRequest xmlns="http://api.jasperwireless.com/ws/schema">
                <messageId>my message</messageId>
                <version>1.3</version>
                <licenseKey>537bba2a-9406-4703-a900-a8fee7a3f310</licenseKey>
                <msisdns>
                    <msisdn>65144099016324</msisdn>
                </msisdns>
            </GetTerminalsByMsisdnRequest>
        </S:Body>
    </S:Envelope> 

但我希望像下面的示例一样生成它。我是java和SOAP的新手,请帮帮我

<S:Body>
            <sch:GetTerminalsByMsisdnRequest xmlns="http://api.jasperwireless.com/ws/schema">
                <sch:messageId>my message</messageId>
                <sch:version>1.3</version>
                <sch:licenseKey>537bba2a-9406-4703-a900-a8fee7a3f310</licenseKey>
                <sch:msisdns>
                    <sch:msisdn>65144099016324</msisdn>
                </sch:msisdns>
            </sch:GetTerminalsByMsisdnRequest>
        </S:Body>

0 个答案:

没有答案