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

时间:2014-07-01 21:20:31

标签: java java-ee jaxb schema

我需要针对我的模式验证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);

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

4 个答案:

答案 0 :(得分:53)

如果您的班级没有@XmlRootElement注释,那么您可以将其包装在JAXBElement的实例中。如果您是从XML Schema生成类,那么生成的ObjectFactory可能会为您提供方便的方法。

我在博客上写了更多关于此用例的内容:

答案 1 :(得分:5)

我建议你使用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>

例如Maven Plugin

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>

答案 2 :(得分:0)

由于没有wsdl的传统wsdl,我也遇到了同样的问题  在wsdl定义中。我通过使用两个Maven插件解决了这个问题  从wsdl以及xsd文件的DTD生成操作,如下所示, 用于编组new ObjectFactory().createHandShake(new HandShake());

  public boolean handShake() {
        JAXBElement<HandShake> request = new ObjectFactory().createHandShake(new HandShake());
        logger.info(String.format("request: {0}", "handshake request"));
        logger.debug("sending request");
        HandShakeResponse handShakeResponse = ((JAXBElement<HandShakeResponse>) getWebServiceTemplate()
                .marshalSendAndReceive(request, new SoapActionCallback(
                        "urn:handShake"))).getValue();
        logger.debug("receive response");
        return handShakeResponse.isReturn();
    }

<plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>0.14.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <schemaLanguage>WSDL</schemaLanguage>
                    <generatePackage>${contextPathWSDL}</generatePackage>
                    <schemas>
                        <schema>
                            <url>${merchant.WSDL}</url>
                        </schema>
                    </schemas>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>0.14.0</version>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <schemaDirectory>${basedir}/src/main/resources/xsds</schemaDirectory>
                    <schemaIncludes>
                        <include>*.xsd</include>
                    </schemaIncludes>
                    <generatePackage>${contextPathXSD}</generatePackage>
                    <generateDirectory>${basedir}/target/generated-sources/DTD</generateDirectory>
                </configuration>
            </plugin>

答案 3 :(得分:0)

我通过使用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"));