使用jax-ws在soap请求中缺少前缀

时间:2012-11-22 11:07:52

标签: java wsdl jax-ws

在Java项目中,我使用jax-ws和wsdl创建了一个Web服务客户端。该请求是有效的xml,但缺少此特定soap调用所需的前缀。目前我通过手动将xmlns = {@ javax.xml.bind.annotation.XmlNs(prefix =“gen”,namespaceURI =“http:// schemas ...”)})添加到package-info.java类来解决这个问题。但是觉得这不是最好的解决方案,因为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>

编辑:在第二个例子中为namespacedeclaration添加了前缀,这是从那里开始的,但是在c / p代码时以某种方式丢失了。

3 个答案:

答案 0 :(得分: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;
    }
}

答案 1 :(得分:0)

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

答案 2 :(得分: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插件。