2017-09-08 33 views
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的另一个序列中的元素。

问候。

回答

0

您的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> 

Java类与产生的输出JAXB 2.2。