具有 Java 实现的 SOAP WS (JAX-WS) :由于命名空间合格,我收到空参数

SOAP WS (JAX-WS) with Java implementation : I receive null parameters because of the namespace qualified

我有一个带有 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>

不幸的是,我无法将 elementFormDefault 更改为 unqualified:我无法修改 WSDL 文件...并且生成的所有请求在xml 请求中的元素。

我搜索了一下,我确定这是服务器端@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 中添加命名空间,但它似乎没有用

注:

如果有人能帮助我,我将不胜感激,这个问题实际上让我发疯。 谢谢!

我终于解决了这个问题。

我加了一个

package-info.java

class 在我的包中,包含我的所有类型的网络服务。

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

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

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