@WebParam 将消息部分映射到参数,并且部分不能是可选的。见Optional Message Parts in WSDL。因此,简短的回答是,您所要求的正是无法完成的。但是,如果您可以重构此方法,则可以使用下面描述的方法之一。
通常通过模式minOccurs=0 设置参数的可选性。此外,您可以在架构中定义一个请求参数,而不是使用多个参数,您可以将其定义为WebMethod 的参数。可选性现在被封装在参数中,并且调用相同的方法来调用带有或不带有可选参数的调用。
我更喜欢先定义合同,而不是依赖自动生成的文件。一旦您弄清楚了 XSD、SOAP 和 WSDL 如何协同工作,您就再也不想使用基于注释/代码优先的定义了,因为反过来您会更加灵活。
代码示例:
<xs:schema
targetNamespace="http://your.namespace.com"
xmlns:tns="http://your.namespace.com"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFromDefault="qualified"
attributeFromDefault="qualified">
...
<xs:element name="MyRequest" type="tns:MyRequestType" />
<xs:element name="MyResponse" type="tns:MyResponseType" />
<xs:complexType name"MyRequestType">
<xs:sequence>
<xs:element name="username" type="xs:string" minOccurs="1" maxOccurs="1" />
<xs:element name="password" type="xs:string" minOccurs="1" maxOccurs="1" />
<xs:element name="newParam" type="xs:string" minOccurs="0" maxOccurs="1" />
</xs:sequence>
</xs:complexType>
...
</xs:schema>
在您的 WSDL 文件中,您可以这样定义消息:
<wsdl:definitions
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:msg="http://your.namespace.com"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
targetNamespace="http://your.namespace.com">
<wsdl:types>
<xs:schema>
<!-- either import the externalized schema -->
<xs:import namespace="http://your.namespace.com"
schemaLocation="someDir/yourMessageSchema.xsd" />
</xs:schema>
<!-- or define the schema within the WSDL - just copy the schema here -->
<xs:schema
targetNamespace="http://your.namespace.com"
xmlns:tns="http://your.namespace.com"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFromDefault="qualified"
attributeFromDefault="qualified">
...
</xs:schema>
</wsdl:types>
...
<wsdl:message name="sendSingle2Request">
<wsdl:part name="in" element="msg:MyRequest" />
</wsdl:message>
<wsdl:message name="sendSingle2Response">
<wsdl:part name="out" element="msg:MyResponse" />
</wsdl:message>
...
<wsdl:portType name="YourServiceEndpoint">
<wsdl:operation name="sendSingle2">
<wsdl:input message="tns:sendSingle2Request" />
<wsdl:output message="tns:sendSingle2Response" />
</wsdl:operation>
...
</wsdl:portType>
<wsdl:binding name="YourServiceBinding" type="YourServiceEndpoint">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name=""sendSingle2">
<soap:operation soapAction="http://your.namespace.com/SendSingle2" style="document" />
<wsdl:input>
<soap:body parts="in" use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body parts="out" use="literal" />
</wsdl:output>
</wsdl:operation>
...
</wsdl:binding>
<wsdl:service name="YourService">
<wsdl:port name="YourServicePort binding="tns:YourServiceBinding">
<soap:address location="http://your.server:port/path/to/the/service" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
这里的 WSDL 契约定义使用样式:document/literal,并且在模式的帮助下,实际的 SOAP 消息将是 document/literal wrapped,这进一步符合 WS-I。
因此,您的方法将更改为公共MyResponse sendSinge2(MyRequest request),其中request 现在封装username、passowrd 和newParam。如果 newParam 没有与 SOAP 请求一起发送,它只会返回 null,所以最好在使用之前先检查一下。
如果您坚持代码优先的方法,则需要首先定义您的 MyRequest 类,将其用作请求参数,而不是那些 2 或 3 个值。
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "MyRequest", namespace="http://your.namespace.com")
public class MyRequest implements Serializable
{
@XmlElement(name = "username", required = true)
protected String username;
@XmlElement(name = "password", required = true)
protected String password;
@XmlElement(name = "newParam", required = false)
protected String newParam;
...
}
MyResult 如果您还没有这样做,也应该这样做。 Web 方法现在可能看起来像这样:
@WebMethod(operationName = "sendSingle2")
@WebResult(name = "sendSingle2Response", targetNamespace = "http://your.namespace.com")
public MyResult sendSingle2(@WebParam(name = "sendSingle2Request") MyRequest request)
{
...
}
再次,request 封装了 3 个参数,您首先应该检查可选参数是否为空。
HTH