JAXB IllegalAnnotationsException

时间:2019-02-21 16:22:58

标签: java xml jaxb

此语句引发IllegalAnnotationException。我想知道为什么。我自己很难找到问题,因为我不熟悉JAXB,并且错误中除了名称外没有任何信息。

JAXBContext context = JAXBContext.newInstance(Article.class);

这是Article类:

@XmlRootElement(name = "article")
@XmlAccessorType(XmlAccessType.FIELD)
public class Article {

    @XmlElement(name = "meta")
    private Metadata metadata;

    @XmlElement
    private Body body;

    @XmlElement(name = "tags")
    private Tags tags;

    public Metadata getMetadata() {
        return metadata;
    }

    public Body getBody() {
        return body;
    }

    public Tags getTags() {
        return tags;
    }

    public void setTags(Tags tags) {
        this.tags = tags;
    }

    public List<Paragraph> getParagraphs() {
        return body.getParagraphs();
    }
}

Tag类:

@XmlAccessorType(XmlAccessType.FIELD)
public class Tags {

    @XmlElement(name = "headword")
    private List<Headword> headword;

    @XmlElement(name = "formParenthesis")
    private List<FormParenthesis> formParenthesis;

    @XmlElement(name = "etymology")
    private List<Etymology> etymology;

    @XmlElement(name = "overview")
    private List<Overview> overview;

    @XmlElement(name = "connections")
    private List<Connections> connections;

    @XmlElement(name = "compounds")
    private List<Compounds> compounds;

    @XmlElement(name = "derivations")
    private List<Derivations> derivations;

    @XmlElement(name = "reference")
    private List<Reference> reference;

    @XmlElementWrapper(name = "sources")
    @XmlElement(name = "source")
    private List<Source> sources;

    @XmlElementWrapper(name = "definitions")
    @XmlElement(name = "definition")
    private List<Definition> definitions;

    @XmlElementWrapper(name = "professions")
    @XmlElement(name = "profession")
    private List<Profession> professions;

    @XmlElementWrapper(name = "usabilities")
    @XmlElement(name = "usability")
    private List<Usability> usabilities;

    @XmlElementWrapper(name = "remarks")
    @XmlElement(name = "remark")
    private List<Commentary> commentaries;

    @XmlElementWrapper(name = "languageTests")
    @XmlElement(name = "languageTest")
    private List<LanguageTest> languageTests;

    public List<Headword> getHeadword() {
        return headword;
    }

    public List<FormParenthesis> getFormParenthesis() {
        return formParenthesis;
    }

    public List<Etymology> getEtymology() {
        return etymology;
    }

    public List<Overview> getOverview() {
        return overview;
    }

    public List<Connections> getConnections() {
        return connections;
    }

    public List<Compounds> getCompounds() {
        return compounds;
    }

    public List<Derivations> getDerivations() {
        return derivations;
    }

    public List<Reference> getReference() {
        return reference;
    }

    public List<Source> getSources() {
        return sources;
    }

    public void setSources(List<Source> sources) {
        this.sources = sources;
    }

    public boolean hasSources() {
        return sources != null;
    }

    public List<Definition> getDefinitions() {
        return definitions;
    }

    public List<Definition> setDefinitions(List<Definition> definitions) {
        this.definitions = definitions;
        return this.definitions;
    }

    public boolean hasDefinitions() {
        return definitions != null;
    }

    public List<Profession> getProfessions() {
        return professions;
    }

    public List<Profession> setProfessions(List<Profession> professions) {
        this.professions = professions;
        return this.professions;
    }

    public boolean hasProfessions() {
        return professions != null;
    }

    public List<Usability> getUsabilities() {
        return usabilities;
    }

    public List<Usability> setUsabilities(List<Usability> usabilities) {
        this.usabilities = usabilities;
        return this.usabilities;
    }

    public boolean hasUsabilities() {
        return usabilities != null;
    }

    public List<Commentary> getCommentaries() {
        return commentaries;
    }

    public List<Commentary> setCommentaries(List<Commentary> commentaries) {
        this.commentaries = commentaries;
        return this.commentaries;
    }

    public boolean hasCommentaries() {
        return commentaries != null;
    }

    public List<LanguageTest> getLanguageTests() {
        return languageTests;
    }

    public List<LanguageTest> setLanguageTests(List<LanguageTest> languageTests) {
        this.languageTests = languageTests;
        return this.languageTests;
    }

    public boolean hasLanguageTests() {
        return languageTests != null;
    }

}

这些注释是非法的吗?

请让我知道您是否想查看代码的其他部分。

更新: 我确定了导致错误的三个标签。我跑了,只有其中一个没有被注释掉:

Tags.java:

@XmlElement(name = "derivations")
private List<Derivations> derivations;

这是堆栈跟踪:

com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
    at com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:91)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:445)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:277)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:124)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1123)
    at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:147)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:247)
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:234)
    at javax.xml.bind.ContextFinder.find(ContextFinder.java:462)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:641)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:584)
    at com.mycom.ap.processing.io.utils.ArticleSerializationHelper.fromFile(ArticleSerializationHelper.java:33)
    at com.mycom.ap.processing.ArticleProcessingWrapper.run(ArticleProcessingWrapper.java:34)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

Derivations.java:

public class Derivations extends PositionalIds {

    @XmlElement(name = "derivation")
    List<Derivation> derivations;

    public List<Derivation> getDerivations() {
        return derivations;
    }
}

PositionalIds.java

@XmlTransient
public abstract class PositionalIds {

    @XmlAttribute(name = "start")
    private String startId;

    @XmlAttribute(name = "end")
    private String endId;

    public String getStartId() {
        return startId;
    }

    public String getEndId() {
        return endId;
    }
}

UDPATE:

Derivations.java 更改为:

@XmlAccessorType(XmlAccessType.FIELD)
public class Derivations extends PositionalIds {

    @XmlElement(name = "derivation")
    List<Derivation> derivations;

    public List<Derivation> getDerivations() {
        return derivations;
    }
}

不能解决问题。仍然存在相同的错误。

1 个答案:

答案 0 :(得分:0)

确保您的Derivations(和其他可能的类似课程)也具有:

@XmlAccessorType(XmlAccessType.FIELD)

否则,您将两次显示JAXB相同的字段,因为您也有getter。