I am facing an issue with JAXB and xjc while generating Java code from an XML schema (XSD) that includes an xs:decimal element with a minExclusive constraint. Despite specifying the type as xs:decimal in my XSD, xjc is generating code that attempts to parse the values as integers, leading to a NumberFormatException.
Here's a simplified version of my XSD:
<xs:element name="test">
<xs:simpleType>
<xs:restriction base="xs:decimal">
<xs:minExclusive value="1.5"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Code that is being generating
@XmlElement(required = true)
@NotNull
@DecimalMin <- error occurs on this line
protected BigDecimal test
ERROR
Task :command-xsd:src_frm_xsd
[ant:xjc] java.lang.NumberFormatException: For input string: "1.5"
[ant:xjc] at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) [ant:xjc] at java.lang.Integer.parseInt(Integer.java:580)
[ant:xjc] at java.math.BigInteger.<init>(BigInteger.java:472) [ant:xjc] at java.math.BigInteger.<init>(BigInteger.java:608) [ant:xjc] at com.sun.tools.xjc.addon.krasa.JaxbValidationsPlugins.processType(JaxbValidationsPlugins.java:324) [ant:xjc] at com.sun.tools.xjc.addon.krasa.JaxbValidationsPlugins.processElement(JaxbValidationsPlugins.java:219) [ant:xjc] at com.sun.tools.xjc.addon.krasa.JaxbValidationsPlugins.processElement(JaxbValidationsPlugins.java:203) [ant:xjc] at com.sun.tools.xjc.addon.krasa.JaxbValidationsPlugins.run(JaxbValidationsPlugins.java:152) [ant:xjc] at com.sun.tools.xjc.model.Model.generateCode(Model.java:292) [ant:xjc] at com.sun.tools.xjc.XJC2Task._doXJC(XJC2Task.java:524) [ant:xjc] at com.sun.tools.xjc.XJC2Task.doXJC(XJC2Task.java:457) [ant:xjc] at com.sun.tools.xjc.XJC2Task.execute(XJC2Task.java:380) [ant:xjc] at com.sun.istack.tools.ProtectedTask.execute(ProtectedTask.java:103) [ant:xjc] at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292) [ant:xjc] at sun.reflect.GeneratedMethodAccessor88.invoke(Unknown Source) [ant:xjc] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [ant:xjc] at java.lang.reflect.Method.invoke(Method.java:498) [ant:xjc] at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:99) [ant:xjc] at groovy.util.AntBuilder.performTask(AntBuilder.java:333) [ant:xjc] at groovy.util.AntBuilder.nodeCompleted(AntBuilder.java:278) [ant:xjc] at org.gradle.api.internal.project.ant.BasicAntBuilder.nodeCompleted(BasicAntBuilder.java:80) [ant:xjc] at groovy.util.BuilderSupport.doInvokeMethod(BuilderSupport.java:151) [ant:xjc] at groovy.util.AntBuilder.doInvokeMethod(AntBuilder.java:213) [ant:xjc] at org.gradle.api.internal.project.ant.BasicAntBuilder.doInvokeMethod(BasicAntBuilder.java:107) [ant:xjc] at groovy.util.BuilderSupport.invokeMethod(BuilderSupport.java:64) [ant:xjc] at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:43) [ant:xjc] at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:135)
error: annotation @DecimalMin
is missing a default value for the element 'value'
@DecimalMin
^
And the generated code is trying to parse the values as integers, resulting in a NumberFormatException.
I've tried adding custom JAXB bindings, but the issue persists. How can I ensure that xjc correctly generates code that handles xs:decimal with minExclusive as a BigDecimal and not as an integer?
Any insights or suggestions would be greatly appreciated. Thank you!
While building the UI project, we generated Java classes from the XML schema, expecting it to map the XML type to the corresponding Java type correctly. However, in this case, it seems to generate incorrect code that tries to parse the XML value as an integer rather than a decimal, resulting in an error and failing to generate the correct annotations.To Fix the Issue
The XJsr303Annotations
option allows for generating Java classes with annotations.
to add the correct annotation @DecimalMax
And @DecimalMin
for maxExculsive
and minExculsive
we have to add the -XJsr303Annotations:JSR_349=true
arg(value: "-XJsr303Annotations:JSR_349=true")