使用javax.xml添加新POJO时出现ClassCastException

时间:2016-10-05 09:30:29

标签: java xml maven xsd

我创建了一个新的POJO类,它使用javax.xml.bind.annotation.*将其架构生成为XSD文件。

这是班级:

package <<my_package>>;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import <<MyOtherClass>>;

@XmlRootElement(name = "MyPojo", namespace="<<my_namespace>>")
@XmlAccessorType(XmlAccessType.FIELD)
public class MyPojo {
    @XmlElement(name = "MyOtherClass")
    private MyOtherClass myOtherClass;

    @XmlElement(name = "Message", required = true)
    private String message;

    public MyOtherClass getMyOtherClass() {
        return myOtherClass;
    }

    public void setMyOtherClass(MyOtherClass myOtherClass) {
        this.myOtherClass = myOtherClass;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String essage) {
        this.message = message;
    }
}

但是,当尝试使用maven编译它时,我收到以下错误:

java.lang.ClassCastException: com.sun.tools.apt.mirror.type.ClassTypeImpl cannot be cast to com.sun.mirror.type.AnnotationType
    at com.sun.tools.apt.mirror.declaration.AnnotationMirrorImpl.getAnnotationType(AnnotationMirrorImpl.java:100)
    at com.sun.tools.jxc.apt.InlineAnnotationReaderImpl.getAllAnnotations(InlineAnnotationReaderImpl.java:113)
    at com.sun.tools.jxc.apt.InlineAnnotationReaderImpl.getAllMethodAnnotations(InlineAnnotationReaderImpl.java:102)
    at com.sun.tools.jxc.apt.InlineAnnotationReaderImpl.getAllMethodAnnotations(InlineAnnotationReaderImpl.java:64)
    at com.sun.xml.bind.v2.model.impl.ClassInfoImpl.ensureNoAnnotation(ClassInfoImpl.java:1126)
    at com.sun.xml.bind.v2.model.impl.ClassInfoImpl.collectGetterSetters(ClassInfoImpl.java:1039)
    at com.sun.xml.bind.v2.model.impl.ClassInfoImpl.findGetterSetterProperties(ClassInfoImpl.java:939)
    at com.sun.xml.bind.v2.model.impl.ClassInfoImpl.getProperties(ClassInfoImpl.java:310)
    at com.sun.xml.bind.v2.model.impl.ModelBuilder.getClassInfo(ModelBuilder.java:243)
    at com.sun.xml.bind.v2.model.impl.ModelBuilder.getClassInfo(ModelBuilder.java:209)
    at com.sun.xml.bind.v2.model.impl.ModelBuilder.getTypeInfo(ModelBuilder.java:315)
    at com.sun.xml.bind.v2.model.impl.TypeRefImpl.calcRef(TypeRefImpl.java:92)
    at com.sun.xml.bind.v2.model.impl.TypeRefImpl.getTarget(TypeRefImpl.java:69)
    at com.sun.xml.bind.v2.model.impl.ElementPropertyInfoImpl$1.get(ElementPropertyInfoImpl.java:74)
    at com.sun.xml.bind.v2.model.impl.ElementPropertyInfoImpl$1.get(ElementPropertyInfoImpl.java:77)
    at java.util.AbstractList$Itr.next(AbstractList.java:358)
    at com.sun.xml.bind.v2.model.impl.ModelBuilder.getClassInfo(ModelBuilder.java:255)
    at com.sun.xml.bind.v2.model.impl.ModelBuilder.getClassInfo(ModelBuilder.java:209)
    at com.sun.xml.bind.v2.model.impl.ModelBuilder.getTypeInfo(ModelBuilder.java:315)
    at com.sun.xml.bind.v2.model.impl.TypeRefImpl.calcRef(TypeRefImpl.java:92)
    at com.sun.xml.bind.v2.model.impl.TypeRefImpl.getTarget(TypeRefImpl.java:69)
    at com.sun.xml.bind.v2.model.impl.ElementPropertyInfoImpl$1.get(ElementPropertyInfoImpl.java:74)
    at com.sun.xml.bind.v2.model.impl.ElementPropertyInfoImpl$1.get(ElementPropertyInfoImpl.java:77)
    at java.util.AbstractList$Itr.next(AbstractList.java:358)
    at com.sun.xml.bind.v2.model.impl.ModelBuilder.getClassInfo(ModelBuilder.java:255)
    at com.sun.xml.bind.v2.model.impl.ModelBuilder.getClassInfo(ModelBuilder.java:209)
    at com.sun.xml.bind.v2.model.impl.ModelBuilder.getTypeInfo(ModelBuilder.java:315)
    at com.sun.xml.bind.v2.model.impl.ModelBuilder.getTypeInfo(ModelBuilder.java:330)
    at com.sun.tools.xjc.api.impl.j2s.JavaCompilerImpl.bind(JavaCompilerImpl.java:90)
    at com.sun.tools.jxc.apt.SchemaGenerator$1.process(SchemaGenerator.java:115)
    at com.sun.mirror.apt.AnnotationProcessors$CompositeAnnotationProcessor.process(AnnotationProcessors.java:84)
    at com.sun.tools.apt.comp.Apt.main(Apt.java:480)
    at com.sun.tools.apt.main.AptJavaCompiler.compile(AptJavaCompiler.java:270)
    at com.sun.tools.apt.main.Main.compile(Main.java:1127)
    at com.sun.tools.apt.main.Main.compile(Main.java:989)
    at com.sun.tools.apt.Main.processing(Main.java:113)
    at com.sun.tools.apt.Main.process(Main.java:103)
    at com.sun.tools.apt.Main.process(Main.java:85)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.sun.tools.jxc.AptBasedTask$InternalAptAdapter.execute(AptBasedTask.java:97)
    at com.sun.tools.jxc.AptBasedTask.compile(AptBasedTask.java:144)
    at com.sun.tools.jxc.maven2.SchemaGenAdapter.execute(SchemaGenAdapter.java:111)
    at com.sun.tools.jxc.maven2.SchemaGenMojo.execute(SchemaGenMojo.java:161)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
    at org.codehaus.classworlds.Launcher.main(Launcher.java:47)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

为什么我会收到此错误以及如何解决?

1 个答案:

答案 0 :(得分:1)

过了一会儿,我想出来了。在我的POJO课程中,我引用了MyOtherClass,其中包含以下代码:

package <<my_package>>;

import java.math.BigDecimal;

public class MyOtherClass {

    private int id;
    private BigDecimal quantity;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public BigDecimal getQuantity() {
        return quantity;
    }

    public void setQuantity(BigDecimal quantity) {
        this.quantity = quantity;
    }
}

此文件没有XML注释,因此Java不知道如何在XSD中生成其架构。然而这是必需的,因为我的POJO需要在XSD中引用它。

修复程序添加了为所有引用对象生成XSD架构所需的所有相关XML注释。在我的情况下,MyOtherClass成为了这个:

package <<my_package>>;

import javax.xml.bind.annotation.*;
import java.math.BigDecimal;

@XmlType(name = "MyOtherClass", namespace = "<<my_namespace>>")
@XmlAccessorType(XmlAccessType.FIELD)
public class MyOtherClass {

    @XmlAttribute(name = "id")
    private int id;

    @XmlAttribute(name = "quantity")
    @XmlSchemaType(name = "decimal")
    private BigDecimal quantity;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public BigDecimal getQuantity() {
        return quantity;
    }

    public void setQuantity(BigDecimal quantity) {
        this.quantity = quantity;
    }
}