JAX-WS中如何去掉空的xmlns元素

3

我正尝试使用 JAX-WS 客户端生成如下请求:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:alin="http://www.alinma.com" xmlns="http://www.alinma.com"> 
<soapenv:Header/>
<soapenv:Body>
    <alin:CustDtlsInqRq>
        <alin:MsgRqHdr>
            <RqUID>BPM11957T201508101626333904816</RqUID>
            <SCId>BPMP</SCId>
            <FuncId>24000000</FuncId>
            <RqMode>0</RqMode>
            <CustLangPref>En</CustLangPref>
            <CustId>
                <CIF>6060</CIF>
            </CustId>
            <AgentId>OprtSupportReviewer</AgentId>
        </alin:MsgRqHdr>
        <Body>
            <CIF>6060</CIF>
        </Body>
    </alin:CustDtlsInqRq>
</soapenv:Body>

但是生成的请求是这样的:
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://www.alinma.com">
<S:Header/>
<S:Body>
    <ns2:CustDtlsInqRq xmlns:ns2="http://www.alinma.com">
        <MsgRqHdr xmlns="">
            <RqUID>OLPM201508261333149660000020</RqUID>
            <SCId>BPMP</SCId>
            <FuncId>24000000</FuncId>
            <CustLangPref>En</CustLangPref>
            <CustId>
                <CIF>6060</CIF>
            </CustId>
        </MsgRqHdr>
        <Body xmlns="">
            <CIF>6060</CIF>
        </Body>
    </ns2:CustDtlsInqRq>
</S:Body>

新增的标签 xmlns="" 会在服务器端造成问题。 以下是将请求用 SOAP Envelope 包装的处理程序代码:

if (outboundProperty.booleanValue()) {

        try {
            SOAPMessage soapMessage = context.getMessage();

            SOAPEnvelope soapEnvelope = soapMessage.getSOAPPart().getEnvelope();
            soapEnvelope.addNamespaceDeclaration("", "http://www.alinma.com");
            // Grab the header of the SOAP envelop
            SOAPHeader soapHeader = soapEnvelope.getHeader();

            // Attach a new header if there is none...
            if (soapHeader == null) {
                soapHeader = soapEnvelope.addHeader();
            }

            soapMessage.saveChanges();
            soapMessage.writeTo(System.out); //TODO: remove this line (just for debugging)

        } catch (Exception e) {
            logger.error("Error Creating SOAP message", e);
        }  

    }

如何调整空的 xmlns 标签?

你是否正在使用复杂的XSD元素?如果是,那么你可能需要正确定义内部和外部命名空间。 - user4948585
我修改了对象(MsgRqHdrType)的注释,如下:@XmlType(name = "MsgRqHdr_Type", propOrder = { "rqUID", "scId", "funcId", "rqMode", "custLangPref", "terminalId", "terminalDesc", "bankId", "branchId", "networkRefId", "custId", "usrId", "sec", "override", "agentId", "clientDt", "echoData", "version" },namespace="http://www.alinma.com") 但这并没有解决问题。 - Khaled adel
你需要设置targetNamespace元素。 - user4948585
请参阅http://docs.oracle.com/cd/E17802_01/webservices/webservices/docs/2.0/jaxws/annotations.html。 - user4948585
1个回答

1

我已经解决了这个问题。 由于我的错误,我删除了生成的文件package-info.java,其中包括所有包类的头命名空间定义。

@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.alinma.com", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package com.ejada.sadadolp.bkf.mwservices.core;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接