1


我在超类上使用 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 可以t 在多个节点上匹配。不愿意这样做,除非这是解决问题的唯一方法。

这阻碍了我转换为 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 @XmlValue 的计数在派生另一个类的类上是不允许的.

解决方法是在基类中添加注解:@javax.xml.bind.annotation.XmlTransient public class 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>

让我知道您是否需要架构或任何其他信息。

提前致谢!

4

1 回答 1

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;
    }

}

价值演示

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>
于 2012-05-03T20:11:59.880 回答