views:

33

answers:

1

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: http://stackoverflow.com/questions/2603778/how-can-i-unmarshall-in-jaxb-and-enjoy-the-schema-validation-without-using-an-exp

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)?

A: 

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:

  1. If you are experiencing issues with the XJC tool, please consider logging a bug against it:

  2. 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.

  3. With JAXB (just like JPA) you can start with POJOs. JAXB annotations can be added to customize the XML representation.

Blaise Doughan
Valid type metadata can be validated against a Schema generated from the JAXB annotations but structural or grammar has to go against a schema. Read through the JAXB 2.2 spec and basically it is impossible to annotate up a choice complex type restricted to one occurrence. So my train of thought died quickly. Went to shipping schemas with the binary code and loading schemas via the SchemaFactory. Will deal with better "type" checking (more expressive restrictions than enums) with JSR 303.
Matthew Young