2017-09-08 8 views
0

jaxb2-maven-plugin 1.6을 사용하여 Spring MVC 프로젝트에 구현 된 API 용 Java 객체를 생성합니다.들여 쓰기가 중복 된 JAXB 목록의 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> 

이 내입니다 : 이것은 내 XSD 정의입니다

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

:이 바인딩 지구 내 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> 

:

이 내 pom.xml 파일의 JAXB 섹션입니다 응답 인스턴스 :

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

방지 할 수 있습니까?

다른 complexType의 다른 시퀀스에서 요소로 사용하기 때문에 "UserRole"을 complexType으로 정의해야합니다.

인사말.

답변

0
는 자바 테스트 코드가

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> 

출력처럼 보일 수 있습니다

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

에 XSD (의 일부)를 변경

JAXB 2.2.