Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is having an XSD considered good practice when passing XML marshalled objects across the wire?

Tags:

xml

jaxb

schema

xsd

I'm using a mix of JAXB, JPA, and a RESTful web service to pass objects across the wire. My domain objects contain a mix of JPA and JAXB annotations, and I'm able to successfully unmarshall my domain objects using Spring's RestTemplate with a minimum amount of code. I remember reading something not too long ago (may have been an answer on SO, may have been a blog) where the author argued that he would never rely on annotations in a production environment, but always marshall and unmarshall according to a schema. Is this still a necessary practice?

If I have a .jar with annotated beans that is a dependency in two projects (e.g. the producing RESTful web service and the consuming client), wouldn't introducing generated XSDs effectively add another set of data requiring maintenance? When using annotated JAXB POJOs, when are schemas necessary and what benefit do they provide?

like image 375
Fil Avatar asked Jan 24 '26 06:01

Fil


1 Answers

JAXB represents its metadata as annotations so there really is no marshal/unmarshal according to the XML schema.

I lead the JAXB component (MOXy) of EclipseLink, which is best know for its JPA implementation. My recommendation for developers is to use a combination of JAXB & JPA annotations on their model.

Uses of XML schema:

  • As mentioned by biziclop JAXB can leverage the XML Schema during marshal/unmarshal operations to perform validation.
  • The XML schema also provides a means for clients of your RESTful service to know what the expected XML message looks like. As mentioned by Fabian Steeg you can make the schema easier to maintain by having it created as part of your build.
like image 132
bdoughan Avatar answered Jan 25 '26 21:01

bdoughan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!