Unified Information Access Blog

Welcome to Attivio's Unified Information Access Blog. Join us for discussions on topics ranging from enterprise search solutions, information access insights, Agile software development methodology to programming with Java. We hope you'll find the articles informative and participate in the discussions by leaving a comment.

Share

Attivio uses Jetty and Apache CXF to provide Web Services. We use javax XML bind annotations on our Java objects for automatic generation of WSDL types and serialization of our objects. Overall this configuration has worked very well for us. But recently we encountered a few problems when using interfaces for the first time, implemented in multiple modules. Initially we got the exceptions IllegalAnnotationsException and MarshalException, while attempting this, but finally found the right combination of annotations to solve our problem.

Even though all the pieces to this puzzle can already be found on the web, this article provides a summary and attached example of everything working together in a single JUnit test.

Classes and Web Service API Declarations

The MyObject and MyApi interfaces below are simple representations of the POJO and web service API calls that our code uses.
  @XmlRootElement
public interface MyObject {
public String getName();
public void setName(String name);
}

@WebService
public interface MyApi {
public List<MyObject> getMyObjects();
public void setMyObjects(List<MyObject> objects);
}

Our objective was to have multiple modules, each with its own implementations of MyObject and MyApi, for separate Web Service calls.

IllegalAnnotationsException Encountered

The first attempt at starting web services with these objects and API calls resulted in an IllegalAnnotationsException error during startup.
Caused by: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 
2 counts of IllegalAnnotationExceptions com.attivio.webservice.WebServiceTest$MyObject
is an interface, and JAXB can't handle interfaces.
Adding the "@XmlJavaTypeAdapter(AnyTypeAdapter.class)" annotation to the object interface solved this problem by telling JAXB to use a special type adapter needed for interfaces.
@XmlJavaTypeAdapter(AnyTypeAdapter.class) 
@XmlRootElement
public interface MyObject {
...
}

Note: The AnyTypeAdapter.java source code is very simple and can be readily found online.

Marshalling Error Encountered

It turns out that adding the AnyTypeAdapter annotation was only part of the solution. Although this annotation lets the web service start successfully, the first call to getMyObjects() resulted in a marshalling error.

INFO: Interceptor has thrown exception, unwinding now
org.apache.cxf.interceptor.Fault: Marshalling Error:
class com.attivio.webservice.WebServiceTest$MyObjectImpl1
nor any of its super class is known to this context.   
Caused by: javax.xml.bind.MarshalException - with linked exception:
[javax.xml.bind.JAXBException:
class com.attivio.webservice.WebServiceTest$MyObjectImpl1 nor any of its
super class is known to this context.]

The final piece of the puzzle was solved by creating a per-module interface, MyApi1, which extends MyApi and has the “@XmlSeeAlso({MyObjectImpl1.class, MyObjectImpl2.class})” annotation.  By extending this interface and registering it as the endpoint class, JAXB was able to find the appropriate implementations of the MyObject interface for serialization.  By putting the annotation on a module specific interface, instead of the base interface, different modules can have different objects and APIs without the need to know details of the other modules.

  @WebService
@XmlSeeAlso({MyObjectImpl1.class, MyObjectImpl2.class})
public interface MyApi1 extends MyApi {
}

public static class MyApiImpl implements MyApi1 {
...
}

Attachments

Sample Code: WebServiceTest.java
Maven Dependencies: pom.xml

References

Thanks to these two posts for their helpful information:

http://jaxb.dev.java.net/guide/Mapping_interfaces.html
http://littlesquare.com/2009/11/25/using-interfaces-with-jaxb/

And thanks to SoapUI for putting out a great free tool for testing web services.

 

Trackback(0)
Comments (2)add comment

Frank said:

...
thanks for the suggestion. it worked for my project.I can now deploy but the service still has a problem.
November 25, 2011

Ramiro said:

...
Excelent! Thanks!
December 21, 2011

Write comment
smaller | bigger

security image
Write the displayed characters


busy

Attivio on LinkedIn

 

blue-rss-icon.png

Enter your email address:

 

Articles by Date

Recent Posts

Thinking Like a Tester

As a member of what was back then, just a three-person QA team, my heart sank when I read the title of one of our early...
Read More...

What AIE and unified information access mean for developers

There has been a lot of press recently on unified information access and how it enables business users and IT staff to reduce the time it takes to provide...
Read More...

The (Real) Semantic Web Requires Machine Learning

The (Real) Semantic Web Requires Machine Learning
We think about the semantic web in two complementary (and equivalent) ways. It can be viewed as: • A large set of subject-verb-object triples, where...
Read More...

More on Triples and Graphs

More on Triples and Graphs
One of the follow-up questions I've received regarding the post on Triples...
Read More...
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8