[英]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>
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.