无法将类型编组为元素,因为它缺少自动生成的类的@XmlRootElement注释

use*_*213 39 java schema jaxb java-ee

我需要针对我的模式验证Class对象,在该模式中我提供了正则表达式来验证自动生成的JAXB类的字段.当我尝试验证我的类对象时,我得到以下错误:

无法编组类型"xyz"作为元素,因为它缺少@XmlRootElement注释

这是我用来验证自动生成的类对象的代码:

jc = JAXBContext.newInstance(obj.getClass());
source = new JAXBSource(jc, obj);
Schema schema = schemaInjector.getSchema();
Validator validator = schema.newValidator();
validator.validate(source);
Run Code Online (Sandbox Code Playgroud)

还有其他方法可以解决这个问题吗?

bdo*_*han 60

如果你的类没有@XmlRootElement注释,那么你可以将它包装在一个实例中JAXBElement.如果您从XML Schema生成类,那么生成的ObjectFactory可能有一个方便的方法.

我在博客上写了更多关于这个用例的文章:

  • 我发现这个答案很有帮助:http://stackoverflow.com/a/2172942/58363 它为 @Blaise-doughan 的答案提供了替代措辞。 (5认同)

小智 11

我通过使用ObjectFactory类解决了这个问题,如下例所示:

PostTransaction transactionRequest = new PostTransaction();
//Some code here

JAXBElement<PostTransaction> jAXBElement = new ObjectFactory().createPostTransaction(transactionRequest);
 try {
JAXBElement<PostTransactionResponse> aXBElementResponse = (JAXBElement<PostTransactionResponse>) webServiceTemplate.marshalSendAndReceive("wsdlUrl", jAXBElement, new SoapActionCallback("soapMethodName"));
Run Code Online (Sandbox Code Playgroud)

  • 就我而言,模拟“.createPostTransaction () ”没有参数(( (3认同)

Xst*_*ian 6

我建议您使用Maven插件“ maven-jaxb2-plugin”从XSD生成类。使用绑定文件*。xjb添加注释@XmlRootElement。

下面的例子

例如绑定文件

<bindings version="2.0" xmlns="http://java.sun.com/xml/ns/jaxb"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    xmlns:annox="http://annox.dev.java.net">

  <globalBindings>
        <xjc:serializable uid="12343" />
        <xjc:simple/>
  </globalBindings>

</bindings>
Run Code Online (Sandbox Code Playgroud)

例如Maven插件

http://confluence.highsource.org/display/MJIIP/User+Guide

 <plugin>
        <groupId>org.jvnet.jaxb2.maven2</groupId>
        <artifactId>maven-jaxb2-plugin</artifactId>
        <version>0.8.1</version>
        <executions>
            <execution>
                <phase>generate-sources</phase>
                <goals>
                    <goal>generate</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <args>
                <arg>-Xannotate</arg>
                <arg>-nv</arg>
            </args>
            <extension>true</extension>
            <forceRegenerate>true</forceRegenerate>
            <bindingDirectory>${basedir}/src/main/resources/schema/xjb</bindingDirectory>
            <bindingIncludes>
                <include>*.xjb</include>
            </bindingIncludes>
            <schemas>
                <schema>
                    <fileset>
                        <directory>${basedir}/src/main/resources/schema/</directory>
                        <includes>
                            <include>*.xsd</include>
                        </includes>
                    </fileset>
                </schema>
            </schemas>
            <debug>true</debug>
            <verbose>true</verbose>
            <plugins>
                <plugin>
                    <groupId>org.jvnet.jaxb2_commons</groupId>
                    <artifactId>jaxb2-basics</artifactId>
                    <version>0.6.2</version>
                </plugin>
                <plugin>
                    <groupId>org.jvnet.jaxb2_commons</groupId>
                    <artifactId>jaxb2-basics-annotate</artifactId>
                    <version>0.6.2</version>
                </plugin>
                <plugin>
                    <groupId>org.jvnet.jaxb2_commons</groupId>
                    <artifactId>jaxb2-namespace-prefix</artifactId>
                    <version>1.1</version>
                </plugin>
            </plugins>
        </configuration>
    </plugin>
Run Code Online (Sandbox Code Playgroud)

  • 请注意,此处&lt;xjc:simple /&gt;是相关的片段,它将添加@ XmlRootElement`注释。这是JAXB RI中的供应商定制,请参见此处:https://jaxb.java.net/nonav/2.2.1/docs/vendorCustomizations.html。它不仅仅添加此注释。如果只想使用@XmlRootElement,则可以使用jaxb2-basics-annotate扩展名。 (3认同)