nillable与minOccurs的区别

本文解析了Java WebService中的@XmlElement注解属性required和nillable的作用,required=false表示XML文档中可以没有某个元素但不能没有值;nillable=true表示元素值可为空但元素必须出现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在java的WebService中,有个注解@XmlElement,其中有两个属性

一个required,一个nillable,分别代表wsdl中的minOccurs和nillable属性

 

这两个属性是XSD中的常见属性,但他们的作用经常容易被混淆起来,这里做个区分说明,以作备忘:

minOccurs=0,即required=false。表示XML文档中可以没有某个元素,但不能没有值

nillable=true。表示XML中该元素的取值是可以为空,但不能没有这个元素出现

 

一般来说对于一个接口需要新维护一个非必填属性的话,那么可以考虑添加required=false注解属性,以帮助兼容旧版接口定义。

<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns="urn:rmyy:service" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" targetNamespace="urn:rmyy:service"> <wsdl:documentation>rmyy</wsdl:documentation> <wsdl:types> <xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="urn:rmyy:service"> <xs:element name="rmyyService"> <xs:complexType> <xs:sequence> <xs:element minOccurs="0" name="userName" nillable="true" type="xs:string"/> <xs:element minOccurs="0" name="password" nillable="true" type="xs:string"/> <xs:element minOccurs="0" name="svcCode" nillable="true" type="xs:string"/> <xs:element minOccurs="0" name="inputXml" nillable="true" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="rmyyServiceResponse"> <xs:complexType> <xs:sequence> <xs:element minOccurs="0" name="return" nillable="true" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> </wsdl:types> <wsdl:message name="rmyyServiceRequest"> <wsdl:part name="parameters" element="ns:rmyyService"/> </wsdl:message> <wsdl:message name="rmyyServiceResponse"> <wsdl:part name="parameters" element="ns:rmyyServiceResponse"/> </wsdl:message> <wsdl:portType name="rmyyPortType"> <wsdl:operation name="rmyyService"> <wsdl:input message="ns:rmyyServiceRequest" wsaw:Action="urn:rmyyService"/> <wsdl:output message="ns:rmyyServiceResponse" wsaw:Action="urn:rmyyServiceResponse"/> </wsdl:operation> </wsdl:portType> <wsdl:binding name="rmyySoap11Binding" type="ns:rmyyPortType"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/> <wsdl:operation name="rmyyService"> <soap:operation soapAction="urn:rmyyService" style="document"/> <wsdl:input> <soap:body use="literal"/> </wsdl:input> <wsdl:output> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:binding name="rmyySoap12Binding" type="ns:rmyyPortType"> <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/> <wsdl:operation name="rmyyService"> <soap12:operation soapAction="urn:rmyyService" style="document"/> <wsdl:input> <soap12:body use="literal"/> </wsdl:input> <wsdl:output> <soap12:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:binding name="rmyyHttpBinding" type="ns:rmyyPortType"> <http:binding verb="POST"/> <wsdl:operation name="rmyyService"> <http:operation location="rmyyService"/> <wsdl:input> <mime:content type="application/xml" part="parameters"/> </wsdl:input> <wsdl:output> <mime:content type="application/xml" part="parameters"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="rmyy"> <wsdl:port name="rmyyHttpSoap11Endpoint" binding="ns:rmyySoap11Binding"> <soap:address location="http://172.17.0.87:8090/ws/services/rmyy.rmyyHttpSoap11Endpoint/"/> </wsdl:port> <wsdl:port name="rmyyHttpSoap12Endpoint" binding="ns:rmyySoap12Binding"> <soap12:address location="http://172.17.0.87:8090/ws/services/rmyy.rmyyHttpSoap12Endpoint/"/> </wsdl:port> <wsdl:port name="rmyyHttpEndpoint" binding="ns:rmyyHttpBinding"> <http:address location="http://172.17.0.87:8090/ws/services/rmyy.rmyyHttpEndpoint/"/> </wsdl:port> </wsdl:service> </wsdl:definitions>怎么在postman请求这个接口
最新发布
06-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值