java - 带有 Java 实现的 SOAP WS (JAX-WS) : I receive null parameters because of the namespace qualified

标签 java web-services soap namespaces 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>

问题是在服务器端,我检索到的所有元素都是空的。但是,当我从元素中删除 namespace 标记时(例如,当属性 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 请求中的元素之前都有命名空间标记“proj:”。

我进行了搜索,我有点确定这是服务器端 @WebResult、@WebMethod 等某些注释的 namespace 问题,但我尝试修改所有这些,设置 namespace ,删除它们......它似乎不起作用。

这是我的 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 中添加命名空间,但似乎不起作用

注意:

  • 我已经更改了 WSDL 和 java 代码中的名称等,以便更容易分析。我也减少了很多。如果有一些小的打字错误,这不是问题。
  • 在服务器端,它似乎将请求识别为“咨询”或“更新”对象,但内部的所有内容均为空(Header、Element1、Element2 ...)
  • 如果您需要更多信息,请随时询问

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

最佳答案

我终于成功解决了这个问题。

我添加了

package-info.java

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

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

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

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

关于java - 带有 Java 实现的 SOAP WS (JAX-WS) : I receive null parameters because of the namespace qualified,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59861290/

相关文章:

java - builder 模式和持久化

java - Error :(32, 27)错误:不兼容的类型:对象不能转换为long

c# - 将 JSON 集合发送到 ASMX Web 服务

java - 在 JPanel 数组中交换 JPanel

java - 使用 JAVA API 将 FHIR 资源序列化为 JSON

web-services - 为什么将 SOAP 用于 Web 服务?

Java、RPC/编码的 Axis 1.4 客户端无法发送空参数

Javascript、SOAP、执行、变量和函数调用

c# - 如何从我的 WCF 服务返回 HTTP 状态代码 420?

web-services - 基于订阅的 Web 服务业务/站点的最佳框架?