使用JAX-WS时SOAP请求中缺少前缀

5
在一个Java项目中,我使用jax-ws和wsdl创建了一个webservice客户端。请求是有效的xml,但缺少一个在此特定soap调用中所需的前缀。目前,我通过在package-info.java类中手动添加xmlns={@javax.xml.bind.annotation.XmlNs(prefix="gen", namespaceURI="http://schemas...")})来解决此问题,但我认为这不是最好的解决方案,因为package-info是自动生成的,如果源代码再次生成,则我的解决方案将被覆盖。
我怀疑我的wsdl中缺少某些内容。
以下是wsdl内容:
<?xml version="1.0" encoding="UTF-8"?>
    <wsdl:definitions xmlns:tns="http://xmlns.example.com/1308658932768" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns0="http://schemas.triennium.com/Servicepunt/gen" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" name="Untitled" targetNamespace="http://xmlns.example.com/1308658932768">
        <wsdl:types>
        <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
     xmlns="http://schemas.triennium.com/Servicepunt/gen"
     targetNamespace="http://schemas.triennium.com/Servicepunt/gen"
     elementFormDefault="qualified"
     attributeFormDefault="unqualified">
            <xs:element name="CallInfo">
                <xs:complexType>
                    ...
                </xs:complexType>
            </xs:element>
            <xs:element name="ResponseStatus">
                <xs:complexType>
                    ...
                </xs:complexType>
            </xs:element>
            <xs:element name="Credentials">
                <xs:complexType>
                    ...
                </xs:complexType>
            </xs:element>
            <xs:element name="AuthenticateAannemerRequest">
                <xs:complexType>
                    ...
                </xs:complexType>
            </xs:element>
            <xs:element name="AuthenticateAannemerResponse">
                <xs:complexType>
                    ...
                </xs:complexType>
            </xs:element>
        </xs:schema>
    </wsdl:types>
    <wsdl:service name="WSDL_Generator.2.0.2">
        <wsdl:port name="AuthenticateAannemer.2.0.2" binding="tns:AuthenticateAannemer.2.0.2Binding">
            <soap:address location="http://10.11.2.12:2592"/>
        </wsdl:port>
    </wsdl:service>
    <wsdl:portType name="AuthenticateAannemer">
        <wsdl:operation name="AuthenticateAannemer">
            <wsdl:input message="tns:AuthenticateAannemerRequest"/>
            <wsdl:output message="tns:AuthenticateAannemerResponse"/>
        </wsdl:operation>
    </wsdl:portType>
    <wsdl:binding name="AuthenticateAannemer.2.0.2Binding" type="tns:AuthenticateAannemer">
        <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
        <wsdl:operation name="AuthenticateAannemer">
            <soap:operation style="document" soapAction="/AuthenticateAannemer.2.0.2/AuthenticateAannemer"/>
            <wsdl:input>
                <soap:body use="literal" parts="input"/>
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal" parts="output"/>
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:message name="AuthenticateAannemerRequest">
        <wsdl:part name="input" element="ns0:AuthenticateAannemerRequest"/>
    </wsdl:message>
    <wsdl:message name="AuthenticateAannemerResponse">
        <wsdl:part name="output" element="ns0:AuthenticateAannemerResponse"/>
    </wsdl:message>
</wsdl:definitions>

以下是没有前缀的请求:

<?xml version='1.0' encoding='UTF-8'?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Body>
        <AuthenticateAannemerRequest   xmlns="http://schemas.triennium.com/Servicepunt/gen">
            <CallInfo>
                <Customer>...</Customer>
                <Module>...</Module>
                <Version>...</Version>
            </CallInfo>
            <Credentials>
                <Username>...</Username>
                <Password>...</Password>
            </Credentials>
        </AuthenticateAannemerRequest>
    </S:Body>
</S:Envelope>

以下是我想要的请求:

<?xml version='1.0' encoding='UTF-8'?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Body>
        <gen:AuthenticateAannemerRequest xmlns:gen="http://schemas.triennium.com/Servicepunt/gen">
            <gen:CallInfo>
                <gen:Customer>...</Customer>
                <gen:Module>...</Module>
                <gen:Version>...</Version>
            </gen:CallInfo>
            <gen:Credentials>
                <gen:Username>...</Username>
                <gen:Password>...</Password>
            </gen:Credentials>
        </gen:AuthenticateAannemerRequest>
    </S:Body>
</S:Envelope>

编辑:在第二个示例中添加了命名空间声明前缀,这是一开始就有的,但在复制和粘贴代码时不知何故丢失了。


5
对于 XML 解析器或 SOAP 实现,两个请求应该是相同的。在 XML 中,前缀本身没有意义,重要的是命名空间和名称的本地部分。在你的第二个例子中,前缀“gen”实际上没有绑定到任何命名空间,这应该是无效的。如果你想使用 xmlns:gen 而不是仅使用 xmlns,那么两个请求应该被处理为相同。 - Jörn Horstmann
3个回答

0
如果您从模式生成类,则可以添加JAXB绑定文件。 查看answer中的模板,该文件指定模式默认命名空间的前缀。 然后在使用生成工具时指定附加的绑定文件。 编辑 您将使用的一个示例文件是:
<?xml version="1.0"?>
<jxb:bindings version="1.0"
              xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xmlns:namespace="http://jaxb2-commons.dev.java.net/namespace-prefix"
              xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd
              http://jaxb2-commons.dev.java.net/namespace-prefix http://java.net/projects/jaxb2-commons/sources/svn/content/namespace-prefix/trunk/src/main/resources/prefix-namespace-schema.xsd">

    <jxb:bindings>
        <jxb:schemaBindings>
            <jxb:package name="your-package-name-here" />
        </jxb:schemaBindings>
        <jxb:bindings>
            <namespace:prefix name="http://schemas.triennium.com/Servicepunt/gen" />
        </jxb:bindings>
    </jxb:bindings>
</jxb:bindings>

在生成类时,请记得使用jaxb-commons插件。


0

您需要手动将package-info.java编译成class文件。


0
@XmlRootElement(namespace = "http://schemas.triennium.com/Servicepunt/gen")
public class AuthenticateAannemerRequest implements Serializable {

    private CallInfo callInfo;
    private Credentials credentials;

    public AuthenticateAannemerRequest() {
    }

    @XmlElement(namespace = "http://schemas.triennium.com/Servicepunt/gen")
    public CallInfo getCallInfo() {
        return callInfo;
    }

    public void setCallInfo(CallInfo callInfo) {
        this.callInfo = callInfo;
    }

    @XmlElement(namespace = "http://schemas.triennium.com/Servicepunt/gen")
    public Credentials getCredentials() {
        return credentials;
    }

    public void setCredentials(Credentials credentials) {
        this.credentials = credentials;
    }
}

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