本文介绍了JAXB和复合模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在使用JAXB,我正在努力注释我的复合前提条件查询概念,以便JAXB会很高兴。

I'm working with JAXB right now and I'm struggling to annotate my composite notion of precondition query so that JAXB would be happy.

前置条件查询可以be:

A precondition query can be:


  • simple:只包含查询的文本节点

  • 复合

    • OR:1前置条件查询或其他匹配的前提条件

    • AND:1前置条件查询和其他匹配的前提条件

    当然,复合查询可以由复合查询组成,就像下面的例子一样:

    Of course, compound queries can be made of compound queries, just like in the following example:

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

    在我的Java模型中,我有一个带注释的单一接口PreconditionQuery(实际上是一个自JAXB以来的抽象类)似乎不满意接口)有3个实现SimplePreconditionQuery,CompoundOrPreconditionQuery和CompoundAndPreconditionQuery。

    In my Java model, I've got a single interface annotated PreconditionQuery (actually an abstract class since JAXB seems unhappy with interfaces) with 3 implementations 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基本相同) :

    For the compound ones (CompoundOrPreconditionQuery is basically the same):

    @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级别,我映射为这个:

    Finally, at the enclosing bean level, I mapped like this:

    public class Precondition {
    
        private PreconditionQuery query;
    
        @XmlElementRef(required = true)
        public PreconditionQuery getQuery() {
            return query;
        }
    
        public void setQuery(PreconditionQuery query) {
            this.query = query;
        }
    }
    

    最后,JAXB抱怨无法解决这个问题:

    In the end, JAXB complains about not being able to resolve this:

    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 不能完成这项工作,那会是什么?

    If @XmlElementRef doesn't do the job, what will?

    推荐答案

    请尝试添加

    @XmlSeeAlso({PreconditionQuery.class})
    

    CompoundAndPreconditionQuery 类。

    然后,尝试使用 @XmlRootElement注释所有 PreconditionQuery 类/子类

    Then, try annotating ALL of your PreconditionQuery classes/subclasses with @XmlRootElement.

    另外,对于 @XmlAccessorType 要小心一点 - 有时候你有,有时不。用它来更好地注释包。

    Also, be a bit more careful with @XmlAccessorType - sometimes you have it, sometimes not. Better annotate the package with it.

    最后,你可能需要 ObjectFactory @ XmlRegistry @XmlElementDecl ,但我认为这绝对不是必需的。

    Finally, you may need an ObjectFactory with @XmlRegistry and @XmlElementDecl, but I do not think it is absolutely required.

    我认为问题是缺少 @XmlRootElement / @XmlSeeAlso 注释。

    I think the problem is the missing @XmlRootElement/@XmlSeeAlso annotations.

    @XmlElementRef 已知很棘手,但确实有效。 :)

    @XmlElementRef is known to be tricky, but it does work for sure. :)

    祝你好运和祝福。

    这篇关于JAXB和复合模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 10:54