users@jaxb.java.net

Re: JAXB with interfaces - not working

From: lightbulb432 <veerukrishnan_at_hotmail.com>
Date: Wed, 14 Nov 2007 21:30:41 -0800 (PST)

Putting annotations on the Child interface doesn't seem to work either. I'm
using JAXB RI 2.1.4.

I got the solution I tried in my original post from the following link on
jaxb.dev.java.net (https://jaxb.dev.java.net/guide/Mapping_interfaces.html).
It also says "Every reference to interface needs to have the @XmlElementRef
annotation" - what does this mean? I don't see example usages in that
document.

I can't tell whether I need to use XmlJavaTypeAdapter, to which the above
link also refers. The document specifies that you use this only when there's
a 1-to-1 association between interface and implementation, which isn't true
in my case (i.e. a Child can have multiple implementations - ChildImpl1,
ChildImpl2, etc.)

Am I on the right track? Thanks.



lightbulb432 wrote:
>
> The following is a sample of trying to use interfaces rather than refer to
> classes directly, and it isn't working. In the debugger, the Parent object
> isn't populated with a List of Child implementations - however, if Parent
> refers to ChildImpl class rather than the Child interface, it works. What
> am I doing wrong?
>
> @XmlRootElement(name="parent")
> public class Parent {
>
> private List<Child> children = new ArrayList<Child>();
>
> @XmlElementWrapper
> @XmlAnyElement
> public List<Child> getChildren() {
> return children;
> }
>
> public void setChildren(List<Child> children) {
> this.children = children;
> }
> }
>
> public interface Child {
>
> public abstract String getId();
>
> public abstract void setId(String id);
>
> }
>
> @XmlRootElement(name="child")
> public class ChildImpl implements Child {
>
> private String id;
>
> @XmlAttribute
> public String getId() {
> return id;
> }
>
> public void setId(String id) {
> this.id = id;
> }
>
> }
>
> jaxb.index contains the following two lines, and is in the same package as
> both the Parent and ChildImpl classes:
> Parent
> ChildImpl
>
> The xml file being unmarshalled follows:
>
> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
> <parent>
> <children>
> <child id="one" />
> <child id="two" />
> </children>
> </parent>
>
> The classes are all in one package, the interfaces are all in another -
> but this doesn't seem to be the cause of the problem. Could the cause be
> that I need to put annotations on the Child interface? Either on the class
> itself, or on the abstract getter method?
>
> Any thoughts on how to make this work? Thanks.
>

-- 
View this message in context: http://www.nabble.com/JAXB-with-interfaces---not-working-tf4795511.html#a13761135
Sent from the java.net - jaxb users mailing list archive at Nabble.com.