栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

SOAP WS-使@WebParam可选

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SOAP WS-使@WebParam可选

@WebParam将消息部分映射到参数,并且部分不是可选的。请参阅WSDL中的可选消息部分。因此,简短的答案就是您要问的事情根本无法完成。但是,如果可以重构此方法,则可以使用下面描述的方法之一。

通常,参数的可选性是通过schema设置的

minOccurs=0
。此外,除了使用多个参数,您还可以在架构中定义一个Request参数,并将其定义为的参数
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  transport="http://schemas.xmlsoap.org/soap/http" />        <wsdl:operation name=""sendSingle2"> <soap:operation soapAction="http://your.namespace.com/SendSingle2"  /> <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/literalwrapped
WS-I兼容。

因此,您的方法将改变公众

MyResponse sendSinge2(MyRequestrequest)
地方
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个参数封装起来,您首先要检查可选参数是否为null。

高温超导



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/455543.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号