JAXB and composite pattern

I am working with JAXB right now, and I am struggling to annotate my composite idea of ​​a preliminary request so that JAXB is happy.

The prerequisite request may be:

  • simple: only node text containing the request
  • compound
    • OR: preconditions of 1 request for a precondition or other matches
    • And: preconditions of 1 request for a precondition, and the rest match

Of course, complex queries can be made from compound queries, as in the following example:

    <precondition>
        <or>
            <and>
                <query>foo</query>
                <query>bar</query>
            </and>
            <query>baz</query>
        </or>
    </precondition>

In my Java model, I have one interface annotated with PreconditionQuery (actually an abstract class, because JAXB seems like displeased interfaces) with 3 implementations of SimplePreconditionQuery, CompoundOrPreconditionQuery and CompoundAndPreconditionQuery.

@XmlSeeAlso(PreconditionQuery.class)
@XmlRootElement(name = "query")
public class SimplePreconditionQuery extends PreconditionQuery {

    private String query;

    @XmlValue
    public String getQuery() {
        return query;
    }

    public void setQuery(String query) {
        this.query = query;
    }
}

(CompoundOrPreconditionQuery ):

@XmlSeeAlso(PreconditionQuery.class)
@XmlRootElement(name = "and")
public class CompoundAndPreconditionQuery extends PreconditionQuery {

    private Collection<PreconditionQuery> preconditionQueries = newArrayList();

    @XmlElementRef(name = "query")
    public Collection<PreconditionQuery> getPreconditionQueries() {
        return preconditionQueries;
    }

    public void setPreconditionQueries(Collection<PreconditionQuery> preconditionQueries) {
        this.preconditionQueries = preconditionQueries;
    }
}

, bean :

public class Precondition {

    private PreconditionQuery query;

    @XmlElementRef(required = true)
    public PreconditionQuery getQuery() {
        return query;
    }

    public void setQuery(PreconditionQuery query) {
        this.query = query;
    }
}

, JAXB , :

Caused by: com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
Invalid @XmlElementRef : Type "class xxx.PreconditionQuery" or any of its subclasses are not known to this context.
    this problem is related to the following location:
        at public xxx.PreconditionQuery xxx.Precondition.getQuery()
        at xxx.Precondition

@XmlElementRef , ?

+1
1

@XmlSeeAlso({PreconditionQuery.class})

CompoundAndPreconditionQuery.

/ PreconditionQuery @XmlRootElement.

, @XmlAccessorType - , . .

, ObjectFactory @XmlRegistry @XmlElementDecl, , .

, @XmlRootElement/@XmlSeeAlso.

@XmlElementRef, , , .:)

.

+1

All Articles