Code, JavaEE

Making An Interface Work With JAXB

I have a Customer class that contains a List of type Product where Product is an interface. I annotated the Customer class with the @XmlRootElement(name=”customer”) annotation and attempted to do a maven clean and maven install. Upon doing the install (which does a compile also) I received an error that JAXB doesn’t work with interfaces. After googling for a solution, I found this stackoverflow post – http://stackoverflow.com/questions/13068146/how-to-overcome-package-com-sun-xml-internal-xxx-does-not-exist-when-compiling . After updating my compiler plugin with the compilerArgument tag it worked, it is as follows:


<build>
 <plugins>
 <plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-compiler-plugin</artifactId>
 <version>${maven.compiler.plugin}</version>
 <configuration>
 <source>${java.version}</source>
 <target>${java.version}</target>
 <compilerArgument>
 -XDignore.symbol.file
 </compilerArgument>
 </configuration>
 </plugin>
 </plugins>
 </build>

The java interface for Product now includes annotations as follows:


@XmlJavaTypeAdapter(AnyTypeAdapter.class)
@XmlRootElement
public interface Product {
 public void setId(String id);
 public String getId();

public void setName(String name);
 public String getName();

public void setCost(BigDecimal cost);
 public BigDecimal getCost();
}

 

And that’s it… Oh, I also had to add a default no-arg constructor to my Customer class.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s