use*_*213 39 java schema jaxb java-ee
我需要针对我的模式验证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);
Run Code Online (Sandbox Code Playgroud)
还有其他方法可以解决这个问题吗?
bdo*_*han 60
如果你的类没有@XmlRootElement注释,那么你可以将它包装在一个实例中JAXBElement.如果您从XML Schema生成类,那么生成的ObjectFactory可能有一个方便的方法.
我在博客上写了更多关于这个用例的文章:
小智 11
我通过使用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"));
Run Code Online (Sandbox Code Playgroud)
我建议您使用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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
71436 次 |
| 最近记录: |