首页 > 解决方案 > 带有 Java 实现的 SOAP WS (JAX-WS):由于命名空间限定,我收到空参数

问题描述

我有一个带有 JAX-WS 的 SOAP 项目,在我的 WSDL 文件中,属性elementFormDefault设置为qualified. 因此,生成的 xml 请求在所有元素前面都有命名空间 (proj:):

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:proj="http://my-project.com/Project">
   <soapenv:Header/>
   <soapenv:Body>
      <proj:Consultation>
         <proj:Header>
            <proj:WSVersion>value</proj:WSVersion>
            <proj:TimeRequete>value</proj:TimeRequete>
            <proj:IDRequest>value</proj:IDRequest>
         </proj:Header>
         <proj:Element1>value</proj:Element1>
         <proj:Element2>value</proj:Element2>
         <proj:limit>value</proj:limit>
      </proj:Consultation>
   </soapenv:Body>
</soapenv:Envelope>

问题是在服务器端,我检索到的所有元素都是空的。但是当我从元素中删除命名空间标签时(比如当属性elementFormDefault设置为时unqualified),我可以访问我的所有元素并且它工作得很好。像这样 :

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:proj="http://my-project.com/Project">
   <soapenv:Header/>
   <soapenv:Body>
      <proj:Consultation>
         <Header>
            <WSVersion>value</proj:WSVersion>
            <TimeRequete>value</proj:TimeRequete>
            <IDRequest>value</proj:IDRequest>
         </Header>
         <Element1>value</proj:Element1>
         <Element2>value</proj:Element2>
         <limit>value</proj:limit>
      </proj:Consultation>
   </soapenv:Body>
</soapenv:Envelope>

不幸的是,我无法将其更改elementFormDefaultunqualified:我无法修改 WSDL 文件......并且所有生成的请求都在 xml 请求中的元素之前获得了名称空间标记“proj:”。

我搜索了一下,我有点确定这是一个命名空间问题,在服务器端有一些注释,如@WebResult、@WebMethod 等,但我尝试修改所有这些、设置命名空间、删除它们......似乎不起作用。

这是我的 WSDL 文件:

<wsdl:definitions
        xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
        xmlns:tns="http://my-project.com/Project"
        xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
        xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="MyProject"
        targetNamespace="http://my-project.com/Project"
>
    <wsdl:types>
        <xsd:schema elementFormDefault="qualified" targetNamespace="http://my-project.com/Project"  xmlns:tns="http://my-project.com/Project">

            <xsd:element name="DefaultFault">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element minOccurs="1" name="CodeError" type="xsd:string"/>
                        <xsd:element minOccurs="1" name="LibelleError" type="xsd:string"/>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>

            <xsd:simpleType name="Element1Type">
                <xsd:restriction base="xsd:string">
                    <xsd:minLength value="1"/>
                    <xsd:maxLength value="1"/>
                </xsd:restriction>
            </xsd:simpleType>
            <xsd:simpleType name="Element2Type">
                <xsd:restriction base="xsd:string">
                    <xsd:minLength value="0"/>
                    <xsd:maxLength value="30"/>
                </xsd:restriction>
            </xsd:simpleType>
            <xsd:simpleType name="Element3Type">
                <xsd:restriction base="xsd:string">
                    <xsd:minLength value="0"/>
                    <xsd:maxLength value="30"/>
                </xsd:restriction>
            </xsd:simpleType>
            <xsd:simpleType name="Element4Type">
                <xsd:restriction base="xsd:string">
                    <xsd:minLength value="0"/>
                    <xsd:maxLength value="20"/>
                </xsd:restriction>
            </xsd:simpleType>
            <xsd:simpleType name="Element5Type">
                <xsd:restriction base="xsd:string">
                    <xsd:minLength value="0"/>
                    <xsd:maxLength value="10"/>
                </xsd:restriction>
            </xsd:simpleType>
            <xsd:simpleType name="Element6Type">
                <xsd:restriction base="xsd:string">
                    <xsd:minLength value="0"/>
                    <xsd:maxLength value="50"/>
                </xsd:restriction>
            </xsd:simpleType>

            <xsd:element name="Consultation">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="Header" type="tns:HeaderRequeteType"/>
                        <xsd:element minOccurs="0" name="Element1" type="tns:Element1Type"/>
                        <xsd:element minOccurs="0" name="Element2" type="tns:Element2Type"/>
                        <xsd:element minOccurs="0" name="limit" type="xsd:int"/>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
            <xsd:element name="ConsultationResponse">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="Header" type="tns:HeaderReponseType"/>
                        <xsd:element name="Element3" type="tns:Element3Type"/>
                        <xsd:element name="offset" type="xsd:int"/>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>

            <xsd:element name="Update">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="Header" type="tns:HeaderRequeteType"/>
                        <xsd:element minOccurs="0" name="Element4" type="tns:Element4Type"/>
                        <xsd:element minOccurs="0" name="Element5" type="tns:Element5Type"/>
                        <xsd:element name="data" type="xsd:string"/>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
            <xsd:element name="UpdateResponse">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="Header" type="tns:HeaderReponseType"/>
                        <xsd:element name="Element6" type="tns:Element6Type"/>
                        <xsd:element name="element" type="xsd:string"/>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>

            <xsd:complexType name="HeaderRequeteType">
                <xsd:sequence>
                    <xsd:element name="WSVersion" type="xsd:string"/>
                    <xsd:element name="TimeRequete" type="xsd:dateTime"/>
                    <xsd:element name="IDRequest" type="xsd:string"/>
                </xsd:sequence>
            </xsd:complexType>
            <xsd:complexType name="HeaderReponseType">
                <xsd:sequence>
                    <xsd:element name="WSVersion"  type="xsd:string"/>
                    <xsd:element name="TimeReponse" type="xsd:dateTime"/>
                    <xsd:element name="TimeRequete" type="xsd:dateTime"/>
                    <xsd:element name="IDResponse" type="xsd:string"/>
                </xsd:sequence>
            </xsd:complexType>

        </xsd:schema>
    </wsdl:types>

    <wsdl:message name="MyProject_DefaultFaultMessage">
        <wsdl:part name="fault" element="tns:DefaultFault" />
    </wsdl:message>

    <wsdl:message name="MyProject_Consultation_InputMessage">
        <wsdl:part name="request" element="tns:Consultation"/>
    </wsdl:message>
    <wsdl:message name="MyProject_Consultation_OutputMessage">
        <wsdl:part name="response" element="tns:ConsultationResponse"/>
    </wsdl:message>

    <wsdl:message name="MyProject_Update_InputMessage">
        <wsdl:part name="request" element="tns:Update"/>
    </wsdl:message>
    <wsdl:message name="MyProject_Update_OutputMessage">
        <wsdl:part name="response" element="tns:UpdateResponse"/>
    </wsdl:message>

    <wsdl:portType name="MyProject">
        <wsdl:operation name="Consultation">
            <wsdl:input message="tns:MyProject_Consultation_InputMessage"/>
            <wsdl:output message="tns:MyProject_Consultation_OutputMessage"/>
            <wsdl:fault name="defaultFault" message="tns:MyProject_DefaultFaultMessage"/>
        </wsdl:operation>
        <wsdl:operation name="Update">
            <wsdl:input message="tns:MyProject_Update_InputMessage"/>
            <wsdl:output message="tns:MyProject_Update_OutputMessage"/>
            <wsdl:fault name="defaultFault" message="tns:MyProject_DefaultFaultMessage"/>
        </wsdl:operation>
    </wsdl:portType>
    <wsdl:binding name="BasicHttpBinding_MyProject" type="tns:MyProject">
        <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
        <wsdl:operation name="Consultation">
            <soap:operation soapAction="http://my-project.com/Project/Consultation" style="document"/>
            <wsdl:input>
                <soap:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal"/>
            </wsdl:output>
            <wsdl:fault name="defaultFault">
                <soap:fault name="defaultFault" use="literal"/>
            </wsdl:fault>
        </wsdl:operation>
        <wsdl:operation name="Update">
            <soap:operation soapAction="http://my-project.com/Project/Update" style="document"/>
            <wsdl:input>
                <soap:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal"/>
            </wsdl:output>
            <wsdl:fault name="defaultFault">
                <soap:fault name="defaultFault" use="literal"/>
            </wsdl:fault>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:service name="MyProject">
        <wsdl:port name="BasicHttpBinding_MyProject" binding="tns:BasicHttpBinding_MyProject">
            <soap:address location="http://my-project.com/Project"/>
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>

Java实现:

package web.service.server.myproject;


@WebService(name = "myProject", targetNamespace = "http://my-project.com/Project")
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
public class MyProjectServiceImpl implements MyProjectService {

    @Override
    @WebMethod(operationName = "Consultation", action = "http://my-project.com/Project/Consultation")
    @WebResult(name = "ConsultationResponse", targetNamespace = "http://my-project.com/Project", partName = "response")
    public ConsultationResponse consultation(Consultation request)
        throws MyProjectDefaultMessage {

            // some code...

        }

    @Override
    @WebMethod(operationName = "Update", action = "http://my-project.com/Project/Update")
    @WebResult(name = "UpdateResponse", targetNamespace = "http://my-project.com/Project", partName = "response")
    public UpdateResponse update(Update request)
        throws MyProjectDefaultMessage {

            // some code ...

        }
}

还有一些元素:

package web.service.server.myproject.types;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "header",
    "element1",
    "element2",
    "limit"
})
@XmlRootElement(name = "Consultation")
public class Consultation {

    @XmlElement(name = "Header", required = true)
    protected HeaderRequeteType header;
    @XmlElement(name = "Element1")
    protected String element1;
    @XmlElement(name = "Element2")
    protected String element2;
    protected Integer limit;

    // getters and setters

}

package web.service.server.myproject.types;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "EnteteRequeteType", propOrder = {
    "VersionWS",
    "timeRequete",
    "idRequest",
})

public class HeaderRequeteType {

    @XmlElement(name = "WVersionS", required = true)
    protected String versionWS;
    @XmlElement(name = "TimeRequete", required = true)
    @XmlSchemaType(name = "dateTime")
    protected XMLGregorianCalendar timeRequete;
    @XmlElement(name = "IDRequest", required = true)
    protected String idRequest;

    // getters and setters

}

我也尝试在 @XmlType 中添加命名空间,但它似乎没有用

笔记 :

如果有人可以帮助我,我将非常感激,这个问题实际上让我发疯。谢谢 !

标签: javaweb-servicessoapnamespacesjax-ws

解决方案


我终于设法解决了这个问题。

我添加了一个

包信息.java

我的包中包含所有类型的 Web 服务的类。

@javax.xml.bind.annotation.XmlSchema(namespace = "http://my-project.com/Project", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package web.service.server.myproject.types;

如果我理解正确,这会放置一个包级别的注释,以便我的元素都链接到我的命名空间。

我希望这能帮助下一个有同样问题的人:)


推荐阅读