2017-03-08 80 views
0

我想要我的所有xjc生成的類實現可串行化接口。 在post閱讀解決方案後,我實現了,但下面的錯誤jaxb2-maven-plugin拋出:如何使用maven jaxb生成類實現可串行化

[ERROR] file: mapping.xsd [17,34] org.xml.sax.SAXParseException; systemId: file:mapping.xsd; lineNumber: 17; columnNumber: 34; src-annotation: elements can only contain and elements, but 'globalBindings' was found. at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203) at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134) at

我的XSD示例:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
      xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" 
      xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" 
      jaxb:extensionBindingPrefixes="xjc" 
      attributeFormDefault="unqualified" 
      elementFormDefault="qualified"> 
    <xs:element name="MappingFile" type="MappingFileType"> 
     <xs:annotation> 
      <jaxb:globalBindings> 
       <xjc:serializable uid="43538530765l"/> 
      </jaxb:globalBindings> 
     </xs:annotation> 

Maven插件:

<plugin> 
       <groupId>org.codehaus.mojo</groupId> 
       <artifactId>jaxb2-maven-plugin</artifactId> 
       <version>2.3</version> 
       <configuration> 
        <sources> 
         <source>xxxx/yyy/mapping.xsd</source> 
        </sources> 
        <packageName>xx.yy.zz.jaxp</packageName> 

       </configuration> 
      </plugin> 

是否有我需要的任何依賴用來避免這種異常?請摘錄。

回答

1

你綁定文件應該是這樣的:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<jaxb:bindings 
     xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" 
     xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" 
     xmlns:xs="http://www.w3.org/2001/XMLSchema" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd" 
     version="2.1"> 

    <jaxb:globalBindings> 
     <xjc:serializable uid="1" /> 
    </jaxb:globalBindings> 
</jaxb:bindings> 

此外,觸摸你的綁定文件中的特定目錄和Maven插件具體配置引用它。例如:

<configuration> 
    <sources> 
     <source>src/main/xjb/xsd</source> 
    </sources> 
    <packageName>xx.yy.zz.jaxp</packageName> 
    <xjbSources> 
     <xjbSource>src/main/xjb/jaxb-bindings.xjb</xjbSource> 
    </xjbSources> 
</configuration>