Friday, September 28, 2007

Annotated JAXB Classes

Over the last week or so, I've started to use JAXB along with the Restlet framework. We are actively developing RESTful web services here at So being new to the Restlet framework, I was eager to get started. One type of representation supported by Restlet is of course XML. To generate XML representations we are using JAXB 2. Being an advocate of annotations, I thought I'd start with annotated POJO's and let the JAXB provider do the rest (I assumed this would be a lot like JPA). I ran into a problem however, trying to create a JAXBContext for my package, I got this error:
WARNING: Problem creating Marshaller
javax.xml.bind.JAXBException: "com.overstock" doesnt contain ObjectFactory.class or jaxb.index
It took me a while to figure out what went wrong. So now that I've got things working correctly, I thought I'd post this example and solution to hopefully save you some time. Given this class:
 1 package com.overstock;
 3 import javax.xml.bind.annotation.XmlAccessType;
 4 import javax.xml.bind.annotation.XmlAccessorType;
 5 import javax.xml.bind.annotation.XmlElement;
 6 import javax.xml.bind.annotation.XmlRootElement;
 8 @XmlRootElement(name="example", namespace="")
 9 @XmlAccessorType(XmlAccessType.FIELD)
10 public class ExampleJaxbClass {
12   @XmlElement(required=true)
13   private String elementOne;
14   private String elementTwo;
16   protected ExampleJaxbClass() {
17     super();
18   }
20   public String getElementOne() {
21     return elementOne;
22   }
23   public void setElementOne(String elementOne) {
24     this.elementOne = elementOne;
25   }
26   public String getElementTwo() {
27     return elementTwo;
28   }
29   public void setElementTwo(String elementTwo) {
30     this.elementTwo = elementTwo;
31   }
32 }
You can easily convert it to XML via the javax.xml.bind.Marshaller class, like this:
 1 public class ExampleTest {
 3   @Test
 4   public void generateXml() throws JAXBException {
 5     ExampleJaxbClass ex = new ExampleJaxbClass();
 6     ex.setElementOne("first Element Value");
 7     ex.setElementTwo("second Element Value");
 9     // Get a JAXB Context for the object we created above
10     JAXBContext context = JAXBContext.newInstance(ex.getClass());
12     // To convert ex to XML, I need a JAXB Marshaller
13     Marshaller marshaller = context.createMarshaller();
15     // Make the output pretty
16     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
17     StringWriter sw = new StringWriter();
19     // marshall the object to XML
20     marshaller.marshal(ex, sw);
22     // print it out for this example
23     System.out.println(sw.toString());
24   }
25 }
Here is the XML generated by the annotations above:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:example xmlns:ns2="">
<elementOne>first Element Value</elementOne>
<elementTwo>second Element Value</elementTwo>
Now for the problem. To create a marshaller, you first need to create a JAXBContext via its newInstance() factory method. You can create a context for a specific JAXB class, as in the example above, or you can create a context for a list of packages (check out the javadoc page for more). When using the Restlet class JaxbRepresentation (only available in Restlet 1.1m1), it uses the package version of newInstance(), that's when I got my error above. I didn't want to create an ObjectFactory (apparently this is another way to get around the above error), at least not yet if I could help it, so I wanted to get some more info on the jaxb.index file. I couldn't find out much, I even looked at the JSR-222 spec. Well, it turns out that all you need to do is add class names to the file and place the file in the package (directory) where your JAXB annotated classes reside (it's similar in one way to a jpa persistence.xml file but without the xml). Here is the content of my jaxb.index file for the example class above:
As you can see, its just the class name, not the fully qualified name (the package name is determined by the directory you placed the file in) or the .class name. If you want to test this out, we need to slightly change the test above. Modify line 10 in the unit test above to look like this:
JAXBContext context = JAXBContext.newInstance(ex.getClass().getPackage().getName());
If the package com.overstock does not have jaxb.index file, this change will cause the test to throw the JAXBException. Add the file and everything works great. If you know where there is good documentation on this let me know I couldn't find any :-)
Code formatting courtesy of Code2HTML.

Friday, September 14, 2007

Goodbye consulting, hello

It would have been much better if I'd posted this month's ago but I've been so busy with work and the UJUG (Utah Java User Group) that I haven't had much time to blog. I'm hopping this will change from today onward. So my first bit of news is that after being a consultant for most of the last eight years or so I've finally decided to take a full-time job with I actually started back in April of this year, so I've been there for a while now. I must say that I'm having more fun at than I've had in years. That is part of the reason I haven't been blogging, my day job is so satisfying that when I get home I don't need to challenge myself with something interesting, I get to do interesting stuff all day long. I'm hoping to start blogging a little more often (like more than every two months) and talk a little bit about what we are doing, from a technology standpoint that is. On another note, I just finished an article on JPA for I think it will be posted on Tuesday of next week (Sept. 18th). If you are interested in looking at the source, you can check out the project page here.