Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate against javax bind annotations rather than schema

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

like image 431
Matthew Campbell Avatar asked Nov 05 '22 09:11

Matthew Campbell


1 Answers

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:

    • https://jaxb.dev.java.net/issues/
  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.

like image 106
bdoughan Avatar answered Nov 10 '22 13:11

bdoughan