Been using latest JAXB Sun implementation but can rely on the XJC to generate correct annotations. Have several examples whereby the XJC doesn't attached XMLElement or XMLAttribute annotations for no logical reason. Plus have problems with the logic behind the plugins framework. Anyways I want to ditch the idea of writing schemas just to produce POJOs and then have to load schemas against only to validate.
Anybody have a way to validate directly against the Javax bind annotations? Saw a cool workaround at: how can i unmarshall in jaxb and enjoy the schema validation without using an explicit schema file
Where a schema was dynamically created just to do validation. Looking for an approach the goes directly against the annotations (like Hibernate Validator and JSR 303 but specifically for Javax bind annotations)?
There isn't any standard validation that can be done against JAXB annotations. In truth for the most part this is because the typed nature of the object model reduces the amount of invalid input that can appear (e.g. if my customer element has a child address element, then my Customer object has an Address property, and you can't set anything other than an Address object on that property).
Where you may want validation is on restricting a collection to a certain number elements (because you have maxOccurs="10"), or a string to a specific length (because you have a schema facet). JAXB 2.X (JSR 222) does not generate these onto your object model by default (although you can certainly add them yourself, like people do when using JPA), using JSR 303 and running a validator.
Other points related to your question:
If you are experiencing issues with the XJC tool, please consider logging a bug against it:
If no annotations are present then the default is @XmlElement, so some of these annotations may be missing for this reason. The annotation is normally only added to adjust the name or namespace information.
With JAXB (just like JPA) you can start with POJOs. JAXB annotations can be added to customize the XML representation.
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