错误使用xjc:superClass与超级类上的XmlTransient注释与EclipseLink Moxy(jaxb 2.1生成的代码)

时间:2012-05-03 18:56:39

标签: jaxb eclipselink jaxb2 moxy


我在超类上使用xjc:superClass和@javax.xml.bind.annotation.XmlTransient注释。

我正在尝试切换到Moxy,我收到此错误:

'Exception Description: Property [aspectStyleBlock] in class [com.aplia.q4.document.ValueStyle] references a class [com.aplia.q4.domain.AbstractBlock] that is marked transient, which is not allowed.
 - with linked exception:
[Exception [EclipseLink-50057] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.JAXBException
' . 

这一切在jaxb 2.1

下工作正常

如果我删除超类上的XmlTransient注释,我会收到此错误:

'ava.lang.RuntimeException: javax.xml.bind.JAXBException
 - with linked exception:
[java.lang.NullPointerException]
....

'Caused by: javax.xml.bind.JAXBException
 - with linked exception:
[java.lang.NullPointerException]
    at org.eclipse.persistence.jaxb.JAXBContext$TypeMappingInfoInput.createContextState(JAXBContext.java:825)
    at org.eclipse.persistence.jaxb.JAXBContext.<init>(JAXBContext.java:136)
    at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:142)
    at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:129)
    at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:93)
    at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:83)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:202)
    at javax.xml.bind.ContextFinder.find(ContextFinder.java:331)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:574)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:522)
    at com.aplia.q4.service.hosting.impl.JAXBAdapter.<init>(JAXBAdapter.java:35)
    ... 27 more
Caused by: java.lang.NullPointerException
    at org.eclipse.persistence.jaxb.compiler.MappingsGenerator.generateChoiceCollectionMapping(MappingsGenerator.java:686)
    at org.eclipse.persistence.jaxb.compiler.MappingsGenerator.generateMapping(MappingsGenerator.java:434)
    at org.eclipse.persistence.jaxb.compiler.MappingsGenerator.generateMappings(MappingsGenerator.java:1996)
    at org.eclipse.persistence.jaxb.compiler.MappingsGenerator.generateMappings(MappingsGenerator.java:1957)
    at org.eclipse.persistence.jaxb.compiler.MappingsGenerator.generateProject(MappingsGenerator.java:193)
    at org.eclipse.persistence.jaxb.compiler.Generator.generateProject(Generator.java:174)
    at org.eclipse.persistence.jaxb.JAXBContext$TypeMappingInfoInput.createContextState(JAXBContext.java:830)
    at org.eclipse.persistence.jaxb.JAXBContext$TypeMappingInfoInput.createContextState(JAXBContext.java:823)
    ... 41 more'

我有大约100个从模式生成的类(我不控制模式)。我尝试使用http://jaxb2-commons.dev.java.net/basic/inheritance来实现一个接口而不是xjc:superclass,但它需要我列出我的所有100个类,因为绑定xpath在多个节点上不匹配。除非这是解决问题的唯一方法,否则不愿意这样做。

这阻碍了我转换为Moxy impl的努力。

有关我的设置和放大器的详细信息为什么我选择添加XmlTransient注释:

来自https://sites.google.com/site/codingkb/java-2/jaxb/jaxb-4

“导致派生另一个类的类不允许使用@XmlValue 您希望自动生成的JAXB类扩展一个公共父类...所以你要向你的XSD添加这样的东西:

不幸的是,当你去编译时,会收到一条错误消息: 线程“main”中的异常com.sun.xml.bind.v2.runtime.IllegalAnnotationsException:IllegalAnnotationExceptions的1个计数 在派生另一个类的类上不允许使用@XmlValue。

解决方案是向基类添加注释: @ javax.xml.bind.annotation.XmlTransient 公共类JAXBSuperClass {   ... }“

我的pom文件(仅限相关部分):

<dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>eclipselink</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>2.1.13</version>
        <scope>runtime</scope>
    </dependency>

        <plugin>
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <version>0.7.4</version>
            <executions>
                <execution>
                    <id>generate-q4-document-jaxb</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <extension>true</extension>
                        <args>
                            <arg>-Xboolean-getter</arg>
                            <arg>-Xinheritance</arg>
                        </args>
                        <plugins>
                            <plugin>
                                    <groupId>org.jvnet.jaxb2_commons</groupId>
                                   <artifactId>jaxb2-basics</artifactId>
                                   <version>0.5.3</version>
                               </plugin>
                            <plugin>
                                <groupId>com.nebulent.xjc</groupId>
                                <artifactId>boolean-getter</artifactId>
                                <version>1.0</version>
                            </plugin>
                        </plugins>
                        <schemaDirectory>${project.build.directory}/generated-sources/schema</schemaDirectory>
                        <bindingDirectory>${basedir}/src/main/resources/jaxb-bindings</bindingDirectory>
                        <forceRegenerate>true</forceRegenerate>
                    </configuration>
                </execution>
            </executions>
        </plugin>

如果您需要架构或任何其他信息,请告诉我。

提前感谢!

1 个答案:

答案 0 :(得分:0)

注意:我是EclipseLink JAXB (MOXy)主管,是JAXB (JSR-222)专家组的成员。

您的问题有几个问题:


问题#1 - MOXy异常

你能提供一个小样本来证明你所看到的问题吗?

更新1: 我相信您看到的问题与以下EclipseLink错误有关:

更新2: 我们修复了EclipseLink 2.3.3(5月10日下载)和2.4.0(5月11日下载)流中的错误。您可以从以下位置下载这些:


问题#2 - 子类上的@XmlValue

JAXB RI无法处理此用例:

Exception in thread "main" com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
@XmlValue is not allowed on a class that derives another class.
    this problem is related to the following location:
        at public java.lang.String forum10437666.Child.getValue()
        at forum10437666.Child

    at com.sun.xml.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:102)
    at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:472)
    at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:302)
    at com.sun.xml.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1140)
    at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:154)
    at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:121)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:202)
    at javax.xml.bind.ContextFinder.find(ContextFinder.java:363)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:574)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:522)
    at forum10437666.ValueDemo.main(ValueDemo.java:11)
只要父类没有将其任何字段/属性映射到XML元素,EclipseLink JAXB(MOXy)就可以了:

<强>父

父类只包含XML属性的映射。

package forum10437666;

import javax.xml.bind.annotation.XmlAttribute;

public class Parent {

    private int id;

    @XmlAttribute
    public int getId() {
        return id;
    }

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

}

儿童

Child扩展Parent并使用@XmlValue映射属性。

package forum10437666;

import javax.xml.bind.annotation.XmlValue;

public class Child extends Parent {

    private String value;

    @XmlValue
    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

}

ValueDemo

package forum10437666;

import javax.xml.bind.*;
import javax.xml.namespace.QName;

public class ValueDemo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Child.class);

        Child child = new Child();
        child.setId(123);
        child.setValue("ABC");

        JAXBElement<Child> je = new JAXBElement<Child>(new QName("child"), Child.class, child);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.marshal(je, System.out);
    }

}

输出

<?xml version="1.0" encoding="UTF-8"?>
<child xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" id="123">ABC</child>