minOccurs,nillable和限制的目的是什么?

fal*_*lla 15 java xml xsd wsdl jaxb

所需文件说:

如果required()true,则将Javabean属性映射到XML模式元素声明minOccurs="1".maxOccurs"1"单个值属性和"unbounded"对多值的属性.

如果required()false,那么Javabean属性将映射到XML Schema元素声明minOccurs="0".maxOccurs"1"单个值属性和"unbounded"对多值的属性.

nillable的文档说:

如果nillable()true,则将JavaBean属性映射到XML Schema nillable元素声明.


代码xs:complexType:

public class WSData {
    //...

    @XmlElement(required = true, nillable = false)
    public void setMonth(XmlMonthType month) {
        this.month = month;
    }

    public void setUserLogin(String userLogin) {
        this.userLogin = userLogin;
    }
}
Run Code Online (Sandbox Code Playgroud)

代码xs:simpleType:

@XmlType
@XmlEnum(Integer.class)
public enum XmlMonthType {
    @XmlEnumValue("1")
    JANUARY,
    @XmlEnumValue("2")
    FEBRUARY,
    @XmlEnumValue("3")
    MARCH,
    /* ... months 4 ~9 ... */
    @XmlEnumValue("10")
    OCTOBER,
    @XmlEnumValue("11")
    NOVEMBER,
    @XmlEnumValue("12")
    DECEMBER;
}
Run Code Online (Sandbox Code Playgroud)

生成的XML架构:

<xs:complexType name="wsData">
  <xs:sequence>
    <xs:element name="month" type="xs:string"/>
    <xs:element minOccurs="0" name="userLogin" type="xs:string"/>
  </xs:sequence>
</xs:complexType>

<xs:simpleType name="xmlMonthType">
  <xs:restriction base="xs:int">
    <xs:enumeration value="1"/>
    <xs:enumeration value="2"/>
    <xs:enumeration value="3"/>
    <!-- ... months 4 ~9 ... -->
    <xs:enumeration value="10"/>
    <xs:enumeration value="11"/>
    <xs:enumeration value="12"/>
  </xs:restriction>
</xs:simpleType>
Run Code Online (Sandbox Code Playgroud)


事实:

  1. minOccurs的默认值为1.因此,月份是必需的(必须存在);
  2. 这个月有限制.因此,month只能有一个由12个定义的枚举中的一个定义的值;
  3. nillable的默认值为false.所以,月份不能有空值;
  4. XML Schema正确生成.

问题:

  1. 这是接受月的贬值(必须不存在);
  2. 它接受月份的任何值,如13(除非不能解析为Integer);
  3. 它接受空值;

我没想到会遇到这些问题,我错过了什么吗?
如果这种行为是正确的,那么required,nillablexs:restriction的目的是什么?

bco*_*ody 10

Nillable允许空值.例如,如果您有一个Integer或Date,如果它是可为空的,则XML标记可能为空.如果它不是可存档但不是必需的,那么XML元素要么必须存在有效​​内容,要么根本不存在; 空标签无效.


bdo*_*han 5

使minOccurs 1,需要月份;

默认值为minOccurs1,因此该month元素是必需的.注意必须如何minOccurs="0"添加userLogin以使其可选.

<xs:complexType name="wsData">
  <xs:sequence>
    <xs:element name="month" type="xs:string"/>
    <xs:element minOccurs="0" name="userLogin" type="xs:string"/>
  </xs:sequence>
</xs:complexType>
Run Code Online (Sandbox Code Playgroud)

使用生成的限制验证月份(不使用XmlAdapter).

您可以Schema在Unmarshaller上设置实例以验证输入:

演示

以下代码可用于生成XML架构:

package forum9111936;

import java.io.IOException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.SchemaOutputResolver;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(WSData.class);
        SchemaOutputResolver sor = new SchemaOutputResolver() {

            @Override
            public Result createOutput(String namespaceUri,
                    String suggestedFileName) throws IOException {
                StreamResult result = new StreamResult(System.out);
                result.setSystemId(suggestedFileName);
                return result;
            }

        };
        jc.generateSchema(sor);
        System.out.println();
    }

}
Run Code Online (Sandbox Code Playgroud)

UPDATE

JAXB RI通常会ValidationEvent针对转换问题抛出严重性1.默认值ValidationEventHandler忽略严重性小于2的所有问题.这通常会导致将值设置为null.您可以覆盖ValidationEventHandler如下:

    unmarshaller.setEventHandler(new ValidationEventHandler() {
        @Override
        public boolean handleEvent(ValidationEvent event) {
            System.out.println(event);
            return event.getSeverity() < ValidationEvent.ERROR;
        }
    });
Run Code Online (Sandbox Code Playgroud)

但是,JAXB RI似乎不会抛出与转换枚举值相关的事件(可能的错误).如果您恰好使用EclipseLink JAXB(MOXy)作为JAXB提供程序,那么您将获得如下异常:

Exception in thread "main" Local Exception Stack: 
Exception [EclipseLink-116] (Eclipse Persistence Services - 2.4.0.qualifier): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: No conversion value provided for the value [13] in field [month/text()].
Mapping: org.eclipse.persistence.oxm.mappings.XMLDirectMapping[month-->month/text()]
Descriptor: XMLDescriptor(forum9111936.WSData --> [])
    at org.eclipse.persistence.exceptions.DescriptorException.noFieldValueConversionToAttributeValueProvided(DescriptorException.java:1052)
    at org.eclipse.persistence.mappings.converters.ObjectTypeConverter.convertDataValueToObjectValue(ObjectTypeConverter.java:140)
    at org.eclipse.persistence.oxm.mappings.XMLDirectMapping.getAttributeValue(XMLDirectMapping.java:287)
    at org.eclipse.persistence.internal.oxm.XMLDirectMappingNodeValue.endElement(XMLDirectMappingNodeValue.java:190)
    at org.eclipse.persistence.oxm.record.UnmarshalRecord.endElement(UnmarshalRecord.java:910)
    at org.eclipse.persistence.internal.oxm.record.XMLStreamReaderReader.parseEvent(XMLStreamReaderReader.java:133)
    at org.eclipse.persistence.internal.oxm.record.XMLStreamReaderReader.parse(XMLStreamReaderReader.java:83)
    at org.eclipse.persistence.internal.oxm.record.XMLStreamReaderReader.parse(XMLStreamReaderReader.java:72)
    at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:838)
    at org.eclipse.persistence.oxm.XMLUnmarshaller.unmarshal(XMLUnmarshaller.java:626)
    at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:472)
    at forum9111936.Demo2.main(Demo2.java:30)
Run Code Online (Sandbox Code Playgroud)

欲获得更多信息