davy_wei
davy_wei

Reputation: 159

JAXB generate classes from xsds always have @XmlElement(required=true)

I have my xml schema defined as below

<?xml version="1.0" encoding="UTF-8"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://poc/"
elementFormDefault="qualified"
targetNamespace="http://poc/"
attributeFormDefault="unqualified"
xmlns:tns="http://poc/">

<xs:simpleType name="custType">
    <xs:restriction base="xs:string">
        <xs:enumeration value="Primary"/>
        <xs:enumeration value="Coapplicant"/>
    </xs:restriction>
</xs:simpleType>
<xs:complexType name="addressType">
    <xs:sequence>
        <xs:element name="address" type="xs:string"/>
        <xs:element name="city" type="xs:string"/>
        <xs:element name="state" type="xs:string" minOccurs="0"/>
        <xs:element name="zip" type="xs:string" minOccurs="0"/>
    </xs:sequence>
</xs:complexType>

<xs:element name="request1">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="fname" type="xs:string" minOccurs="0"/>
            <xs:element name="lname" type="xs:string" minOccurs="0"/>
            <xs:element maxOccurs="1" name="categoryCode" minOccurs="0">
                <xs:simpleType>
                   <xs:restriction base="xs:integer">
                    <xs:maxLength value="4"/>
                   </xs:restriction>
               </xs:simpleType>
            </xs:element>
            <xs:element name="retiredInd" type="xs:boolean" minOccurs="0"/>
            <xs:element name="custType" type="tns:custType" minOccurs="0"/> 
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:element name="response1">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="addr" type="tns:addressType" minOccurs="0"/>
            <xs:element name="nation" type="xs:token" minOccurs="0"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>
</xs:schema>

I uses maven-jaxb2-plugin for generating Java classes against this xsd.

<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <version>0.8.3</version>
    <configuration>
        <schemaDirectory>${basedir}/cfg</schemaDirectory>
        <schemaIncludes>
            <include>xsd/*.xsd</include>
        </schemaIncludes>
        <generateDirectory>${basedir}/src/main/java</generateDirectory>
        <strict>false</strict>
        <extension>true</extension>
        <removeOldOutput>false</removeOldOutput>
        <clearOutputDir>false</clearOutputDir>
        <forceRegenerate>true</forceRegenerate><plugins>
            <plugin>
                <groupId>org.jvnet.jaxb2_commons</groupId>
                <artifactId>jaxb2-basics</artifactId>
                <version>0.6.4</version>
            </plugin>
            <plugin>
                <groupId>org.jvnet.jaxb2_commons</groupId>
                <artifactId>jaxb2-basics-annotate</artifactId>
                <version>0.6.4</version>
            </plugin>
        </plugins>
    </configuration>
    <executions>
        <execution>
        <id>tsys-sources</id>
        <goals>
            <goal>generate</goal>
        </goals>
        </execution>
    </executions>
</plugin>

After generating the code, I found that all the elements are having the annotation of @XmlElement(required = true). Why? I have many minOccurs="0" elements. Why the required=true is always added on the elements.

Upvotes: 2

Views: 1501

Answers (2)

davy_wei
davy_wei

Reputation: 159

my local config issue in my binding.xml. I have it fixed after clearing some of the useless config.

Upvotes: 0

Ilya
Ilya

Reputation: 29663

I generate sources from you example, using plugin from your example
and I have @XmlElement(required = true) only on

AddressType#address  
AddressType#city

fields.

public class AddressType {

    @XmlElement(required = true)
    protected String address;
    @XmlElement(required = true)
    protected String city;
    protected String state;
    protected String zip;   
    // ...  
}

So, add minOcurrs to this fields also.

Upvotes: 2

Related Questions