XSD到Java带有JAXB列表,并具有重复的凹痕



我使用jaxb2-maven-plugin 1.6生成我在弹簧MVC项目中实现的API的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"定义为复杂型,因为我将其用作另一个复杂型的另一个序列。

问候。

将您的XSD(一部分)更改为

<element name="UserRole">
<complexType >
<sequence>
    <element name="Id" type="string"></element>
    <element name="Name" type="string"></element>
    <element name="Privileges" type="myapi:UserPrivilegeList"></element>
</sequence>
</complexType>
</element>

Java测试代码可能看起来像

public class PrivIlegeTester {
    public static void main(String[] args) throws JAXBException {
        // TODO Auto-generated method stub
        JAXBContext context = JAXBContext.newInstance(UserRole.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        UserPrivilege uPriv = new UserPrivilege();
        uPriv.setId("45455-4466");
        uPriv.setMethod("GET");
        uPriv.setName("GET-CONFIG");
        uPriv.setPath("/*");
        UserPrivilege uPriv0 = new UserPrivilege();
        uPriv0.setId("45888-4466");
        uPriv0.setMethod("POST");
        uPriv0.setName("POST-CONFIG");
        uPriv0.setPath("/*");
        UserRole ur = new UserRole();
        ur.setPrivileges(new UserPrivilegeList());
        ur.getPrivileges().getPrivilege().add(uPriv);
        ur.getPrivileges().getPrivilege().add(uPriv0);
        m.marshal(ur, System.out);

    }
}

和输出

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<userRole xmlns="http://www.example.org/Privilege">
    <Privileges>
        <Privilege>
            <Id>45455-4466</Id>
            <Name>GET-CONFIG</Name>
            <Method>GET</Method>
            <Path>/*</Path>
        </Privilege>
        <Privilege>
            <Id>45888-4466</Id>
            <Name>POST-CONFIG</Name>
            <Method>POST</Method>
            <Path>/*</Path>
        </Privilege>
    </Privileges>
</userRole>

Jaxb 2.2。

生成Java类

最新更新