【问题标题】:unable to marshal type as an element because it is missing an @XmlRootElement annotation for auto generated classes无法将类型编组为元素,因为它缺少自动生成的类的 @XmlRootElement 注释
【发布时间】:2014-08-22 13:19:58
【问题描述】:

我需要根据我提供正则表达式的模式来验证 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);

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

【问题讨论】:

    标签: java jakarta-ee jaxb schema


    【解决方案1】:

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

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

    【讨论】:

    • 感谢您的帮助。我已经根据您博客上的解决方案修改了我的代码,但我仍然遇到同样的错误。修改后的代码为:QName qName = new QName("http://www.w3.org/2001/XMLSchema", obj.toString());JAXBElement<T> e = createJAXBElement(qName, obj.getClass(), obj);jc = JAXBContext.newInstance(e.getValue().getClass());Marshaller m = jc.createMarshaller();source = new JAXBSource(jc, m.getClass());Schema schema = schemaInjector.getSchema();Validator validator = schema.newValidator();validator.validate(source);请指教
    • 我发现这个答案很有帮助:stackoverflow.com/a/2172942/58363 它为@Blaise-doughan 的答案提供了替代措辞。
    【解决方案2】:

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

    POM(Maven 插件配置):

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

    见Maven JAXB2 Plugin User Guide

    【讨论】:

    • 请注意,&lt;xjc:simple/&gt; 是此处的相关部分,它将添加 @XmlRootElement 注释。这是 JAXB RI 中的供应商定制,请参见此处:jaxb.java.net/nonav/2.2.1/docs/vendorCustomizations.html。它不仅仅是添加此注释。如果你只想要@XmlRootElement,你可以使用 jaxb2-basics-annotate 扩展。
    【解决方案3】:

    由于没有 xsd 架构的旧版 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>
    

    【讨论】:

    • 以及如何使用gradle解决这个问题?
    【解决方案4】:

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

    【讨论】:

    • 最简单的解决方案
    • 在我的例子中,模拟的“.createPostTransaction ()”没有参数((
    • 最佳答案..干净。如果有人想知道如何创建 ObjectFactory().createXyZ,您可以使用 jaxb maven 插件构建 jaxbObjects
    猜你喜欢
    • 2018-05-27
    • 2015-09-22
    • 2016-08-31
    • 1970-01-01
    • 2019-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-29
    相关资源
    最近更新 更多