0
我使用jaxb2-maven-plugin 1.6为在Spring MVC项目中实现的API生成Java对象。将XSD与带有JAXB列表的Java重复缩进
这是我的pom.xml JAXB部分:
<!-- JAXB 2 -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<id>set-additional-system-properties</id>
<goals>
<goal>set-system-properties</goal>
</goals>
</execution>
</executions>
<configuration>
<properties>
<property>
<name>javax.xml.accessExternalSchema</name>
<value>file,http</value>
</property>
</properties>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<id>api</id>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<xjbSources>
<xjbSource>${basedir}/src/main/resources/xsd/api/api.xjb</xjbSource>
</xjbSources>
<sources>
<source>${basedir}/src/main/resources/xsd/api/api.xsd</source>
</sources>
<packageName>com.example.ems.aaa.xsd.api</packageName>
</configuration>
</plugin>
这是我的JAXB全局绑定:
<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings version="2.1"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns="http://www.w3.org/2001/XMLSchema">
<jaxb:globalBindings>
<jaxb:serializable uid="1"/>
</jaxb:globalBindings>
</jaxb:bindings>
这是我的XSD定义:
<complexType name="UserPrivilege">
<sequence>
<element name="Id" type="string"></element>
<element name="Name" type="string"></element>
<element name="Method" type="string"></element>
<element name="Path" type="string"></element>
</sequence>
</complexType>
<complexType name="UserPrivilegeList">
<sequence>
<element name="Privilege" type="myapi:UserPrivilege" minOccurs="0" maxOccurs="unbounded" />
</sequence>
</complexType>
<complexType name="UserRole">
<sequence>
<element name="Id" type="string"></element>
<element name="Name" type="string"></element>
<element name="Privileges" type="myapi:UserPrivilegeList"></element>
</sequence>
</complexType>
这是我的响应实例:
<UserRole>
<id>17b55c53-7328-4444-95e2-648fb5f9de89</id>
<name>CONFIGURATOR</name>
<privileges>
<privilege>
<privilege>
<id>b5f7f39a-f87c-4874-9b16-381a5e0613b3</id>
<name>CONFIG_PUT</name>
<method>PUT</method>
<path>/config/**</path>
</privilege>
<privilege>
<id>7045c699-5608-4584-a3d0-41a96b9d7903</id>
<name>CONFIG_GET</name>
<method>GET</method>
<path>/config/**</path>
</privilege>
</privilege>
</privileges>
</UserRole>
如果你能看到列表的XML名称重复压痕:
<privileges>
<privilege>
<privilege>
我可以预防呢?
我需要将“UserRole”定义为complexType,因为我将它用作另一个complexType的另一个序列中的元素。
问候。