In our product we use apache CXF. Because of performance restriction, the schema validation has been set to false. Now, for an integer element if I am providing an invalid value, JAXB is unmarshaling it to some thing else. For example,
9999999999 is converted into 1410065407.
988888888888 is converted into 1046410808.
My question is what is the logic (formula) that is being followed here?
How to handle this (Considering the validation will be off)? I want such a value to be rejected.
Note: I’m the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.
SHORT ANSWER
This appears to be a bug in the JAXB reference implementation. I would recommend entering a bug for it at the following location:
This same use case works correctly in EclipseLink JAXB (MOXy).
LONG ANSWER
Below is a complete example that demonstrates the issue:
Root
Below is a domain class with
intandintegerfields.input.xml
Below is an XML document with the values from your question.
Demo
In the demo code below I’ve specified a
ValidationEventHandleron theUnmarshaller. This should catch aValidationEventfor any invalid values encountered during anunmarhsaloperation.Output – JAXB Reference Implementation
This output matches the behaviour you are seeing.
Output – EclipseLink JAXB (MOXy)
If you specify MOXy as your JAXB provider (see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html) this use case works as expected. You will get a
ValidationEventfor each invalid value, and if theValidationEventis handled the field/property won’t be set to a invalid value.POTENTIAL WORKAROUND
If your fields/properties are of type
Integerand you can switch from the JAXB reference implementation then you could create anXmlAdapterto do your ownIntegerto/fromStringconversions.IntegerAdapter
Below is an example of an
XmlAdapterdemonstrating how you could provide your own conversion logic.package-info
Using the
@XmlJavaTypeAdapterannotation at the package level means that theXmlAdapterwill apply to all fields/properties of typeIntegerfor classes in this package.Output
Below is the updated output when the RI is used. The
intvalues are still wrong, but not theIntegervalues arenullandValidationEventsare produced as expected.For More Information