Reputation: 193
I like Jersey, I really do, but I would rather use interfaces as the return types for my resources instead of concrete classes. I followed the instructions here:
http://jaxb.java.net/guide/Mapping_interfaces.html
It showed how you can annotate the interface with an adapter for classes implementing the interfaces so that JAXB will be able to bind to them.
From the page:
@XmlJavaTypeAdapter(AbstractFooImpl.Adapter.class)
interface IFoo {
...
}
abstract class AbstractFooImpl implements IFoo {
...
static class Adapter extends XmlAdapter<AbstractFooImpl,IFoo> {
IFoo unmarshal(AbstractFooImpl v) { return v; }
AbstractFooImpl marshal(IFoo v) { return (AbstractFooImpl)v; }
}
}
I'm not too crazy about a interface knowing what implements it, that seems kinda ugly. Our code is using a DAO pattern with a factory to abstract out the database we're using so we can swap it later if we need to. That kind of cyclical dependency just seems like it's going to cause problems later.
I'm also not happy with not being able to use these interfaces with my GWT client that's going to be using these web services. Since the annotation comes from javax.xml.bind, it's not compatible with the GWT compiler. My current workaround is a special service side interface with annotations.
So is there a better way to do this? Maybe using a Provider to instantiate the classes? Or some type of Filter that can map the interfaces to some type of factory that can provide the implementations?
Any help would be appreciated.
Thanks.
Upvotes: 4
Views: 1624
Reputation: 148977
EclipseLink JAXB (MOXy) offers support for mapping interfaces (I'm the tech lead):
MOXy can be easily used with Jersey:
Upvotes: 1