Java 无法将类型编组为元素,因为它缺少自动生成的类的 @XmlRootElement 注释
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24519449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
unable to marshal type as an element because it is missing an @XmlRootElement annotation for auto generated classes
提问by user656213
I need to validate Class object against my schema in which I have provided regular expression to validate fields for auto generated JAXB classes. When I try to validate my class object I get below error:
我需要根据我的模式验证 Class 对象,其中我提供了正则表达式来验证自动生成的 JAXB 类的字段。当我尝试验证我的类对象时,出现以下错误:
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?
我还有其他方法可以解决这个问题吗?
回答by bdoughan
If your class does not have an @XmlRootElement
annotation then you can wrap it in an instance of JAXBElement
. If you generated your classes from an XML Schema then the generated ObjectFactory
may have a convenience method for you.
如果您的类没有@XmlRootElement
注释,那么您可以将其包装在JAXBElement
. 如果您从 XML Schema 生成类,那么生成的类ObjectFactory
可能为您提供了一个方便的方法。
I have written more about this use case on my blog:
我在我的博客上写了更多关于这个用例的文章:
回答by Xstian
I suggest you to use maven plugin "maven-jaxb2-plugin" to generate classes from an XSD. Use a binding file *. xjb to add annotations @XmlRootElement.
我建议您使用 maven 插件“maven-jaxb2-plugin”从 XSD 生成类。使用绑定文件 *. xjb 添加注解@XmlRootElement。
Following some example
以下是一些例子
e.g 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>
e.g Maven Plugin
例如 Maven 插件
http://confluence.highsource.org/display/MJIIP/User+Guide
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>
回答by Mohamed.Abdo
I faced same issue due to legacy wsdl that doesn't have xsd schema
inside wsdl definition. 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());
由于在 wsdl 定义中没有 xsd 架构的遗留 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>
回答by Brighton Berejena
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"));