In my previous post I showed how to create RESTful services using Spring Framework. For representation of resources in XML I used JAXB and I followed the bottom-up approach (I wrote the Java classes and I let to generate XML/XSD from Java classes). In this post I will demonstrate how you can generate the same Java classes (User and UserList) from XML Schema (XSD) during Maven build, therefore using a top-down approach. For generation of Java classes from XML Schema during Maven build I will use Java.net Maven 2 JAXB 2 Plugin.
Along with the default generation of Java classes from XML Schema using xjc tool, I will add the following customizations:
- Making all the generated classes to implement Serializable (useful when you want to use classes with some remote services: RMI, EJB, etc).
- Use java.util.Calendar instead of javax.xml.datatype.XMLGregorianCalendar for fields generated from elements of type xs:dateTime.
- Generated classes should also have a generated toString() method.
- Classes have to be annotated with XmlRootElement (required by Spring Framework when classes are used to represent state in RESTful services).
- You can further enhance and customize the generation of Java classes using various plugins.
Project sources are available for download. So let’s start, step by step:
Read More »