gpt4 book ai didi

java - 如何使用maven jaxb实现serialized生成类

转载 作者:行者123 更新时间:2023-12-02 13:42:18 25 4
gpt4 key购买 nike

我希望所有 xjc 生成的类都实现可序列化接口(interface)。阅读 post 的解决方案后我实现了它,但是 jaxb2-maven-plugin 抛出以下错误:

[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>

我需要使用任何依赖项来避免此异常吗?请提出建议。

最佳答案

您的绑定(bind)文件应如下所示:

<?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>

此外,触摸特定目录中的绑定(bind)文件并在 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>

关于java - 如何使用maven jaxb实现serialized生成类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42678802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com