简体   繁体   English

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

[英]unable to marshal type as an element because it is missing an @XmlRootElement annotation for auto generated classes

I need to validate Class object against my schema in which I have provided regular expression to validate fields for auto generated JAXB classes.我需要根据我的模式验证 Class 对象,其中我提供了正则表达式来验证自动生成的 JAXB 类的字段。 When I try to validate my class object I get below error:当我尝试验证我的类对象时,出现以下错误:

unable to marshal type "xyz" as an element because it is missing an @XmlRootElement annotation无法将类型“xyz”编组为元素,因为它缺少 @XmlRootElement 注释

Here is the code that I use to validate my autogenerated class object:这是我用来验证自动生成的类对象的代码:

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

Is there any other way I can solve this?我还有其他方法可以解决这个问题吗?

If your class does not have an @XmlRootElement annotation then you can wrap it in an instance of JAXBElement .如果您的类没有@XmlRootElement注释,那么您可以将其包装在JAXBElement的实例中。 If you generated your classes from an XML Schema then the generated ObjectFactory may have a convenience method for you.如果您从 XML Schema 生成类,那么生成的ObjectFactory可能为您提供一个方便的方法。

I have written more about this use case on my blog :我在我的博客上写了更多关于这个用例的文章

I solved this problem by using the ObjectFactory class as shown in the example below:我通过使用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"));

I suggest you to use Maven plugin maven-jaxb2-plugin to generate classes from a .xsd file.我建议您使用 Maven 插件maven-jaxb2-plugin.xsd文件生成类。 Use a binding file eg .xjb to add annotations @XmlRootElement .使用绑定文件(例如.xjb添加注释@XmlRootElement Example:例子:

Binding file:绑定文件:

<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 config): 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>
        

See Maven JAXB2 Plugin User Guide请参阅Maven JAXB2 插件用户指南

I faced same issue due to legacy wsdl that doesn't have xsd schema inside wsdl definition.由于在 wsdl 定义中没有 xsd 架构的遗留 wsdl,我遇到了同样的问题。 I solved this issue by having two maven plugins to generate operations from wsdl as well DTD from xsd file as below and for marshalling new ObjectFactory().createHandShake(new HandShake());我通过使用两个 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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将类型编组为XML元素,因为缺少@XmlRootElement注释 - Unable to marshal type as XML element because @XmlRootElement annotation is missing 无法将类型 [...] 编组为元素,因为它缺少 @XmlRootElement 注释 - unable to marshal type […] as an element because it is missing an @XmlRootElement annotation JMS消息,由于缺少@XmlRootElement批注,因此无法将类型“ java.lang.String”作为元素封送 - JMS message, unable to marshal type “java.lang.String” as an element because it is missing an @XmlRootElement annotation 无法将类型“java.lang.Integer”编组为元素,因为它缺少 @XmlRootElement 注释 - unable to marshal type "java.lang.Integer" as an element because it is missing an @XmlRootElement annotation 我有@xmlrootelement,但一直收到此异常:无法将类型编组为元素,因为它缺少@XmlRootElement - I have @xmlrootelement , but keep getting this exception: unable to marshal type as an element because it is missing an @XmlRootElement 存在@XmlRootElement批注时,无法封送Java类 - Unable to marshal Java class when @XmlRootElement annotation is present Java:无法封装类型“entities.Person”作为元素因为 - Java : unable to marshal type “entities.Person” as an element because 为生成的类添加@XmlRootElement注释后,我无法在源类中访问它们 - After adding @XmlRootElement annotation for generated classes i can't access them in my source classes 生成的 Java 类的 @XmlSeeAlso 注释中缺少类 - Missing classes in @XmlSeeAlso annotation of generated Java class Marshal不是@XmlRootElement,JAXB为String - Marshal not @XmlRootElement with JAXB to String
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM