I'm currently generating my first JAXB data binding. I have a schema which contains within it a xs:simpleType:
<xs:simpleType name="NINumberType">
<xs:restriction base="xs:string">
<xs:pattern value="[A-Z]{2}\d{6}[A-D]{0,1}"/>
</xs:restriction>
</xs:simpleType>
In my binding.xjb I have this:
<jxb:bindings version="1.0"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
jxb:extensionBindingPrefixes="xjc">
<jxb:bindings schemaLocation="mySchema.xsd" node="/xs:schema">
<jxb:globalBindings mapSimpleTypeDef="true" />
<jxb:schemaBindings>
<jxb:package name="com.company.jaxb.mySchema"/>
</jxb:schemaBindings>
</jxb:bindings>
</jxb:bindings>
Then, through Eclipse (well RAD 7.5) I right click the schema, and choose Generate->Java. This produces data binding objects as expected, except that the NINumberType
has no built in restrictions:
/**
* <p>Java class for NINumberType simple type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <simpleType name="NINumberType">
* <restriction base="{http://www.w3.org/2001/XMLSchema}string">
* <pattern value="[A-Z]{2}\d{6}[A-Z]{0,1}"/>
* </restriction>
* </simpleType>
* </pre>
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "NINumberType", propOrder = {"value"})
public class NINumberType {
@XmlValue
protected String value;
public String getValue() {return value;}
public void setValue(String value) {this.value = value;}
}
No mention is made of the regular expression restriction specified in my schema, except in the class level javadoc. JAXB appears to have the information it needs to generate the restriction code, but is not using it. Can anyone help me ensure that the restriction code is generated such that attempting to bind a poorly formatted NI number will fail?
JAXB will not generate those restrictions into the Java model. You can specify an instance of javax.xml.validation.Schema
on the Marshaller
/Unmarshaller
if you want this constraint enforced during the conversion to/from XML.
For More Information
You may be able to find an XJC extension that leverages something like JSR-303 for validation constraints. The following link may help:
You could also look to xjc plugin https://github.com/krasa/krasa-jaxb-tools
By documentation it support XJsr303Annotations
and can generate:
@Valid
annotation for all complex types, can be further restricted to generate only for types from defined schema: -XJsr303Annotations:targetNamespace=http://www.foo.com/bar
@NotNull
annotation for objects that has a MinOccur
value >= 1 or for attributes with required use@Size
for lists that have minOccurs > 1
@Size
if there is a maxLength
or minLength
or length restriction@DecimalMax
for maxInclusive
restriction@DecimalMin
for minInclusive
restriction@DecimalMax
for maxExclusive
restriction, enable new parameter (inclusive=false)
with: -XJsr303Annotations:JSR_349=true
@DecimalMin
for minExclusive
restriction, enable new parameter (inclusive=false)
with: -XJsr303Annotations:JSR_349=true
@Digits
if there is a totalDigits
or fractionDigits
restriction.@Pattern
if there is a Pattern
restrictionIf you wonder how to use XJC plugins in your build environment (ant
, maven
, gradle
) I may recomment look at examples of another plugin: immutable-xjc
So I hope it helps.
Related to Generation of XSD restrictions in a schema generated from Java JAXB annotated classes
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With