Buscar este blog

domingo, 26 de abril de 2015

Generate classes from xsd - @XmlRootElement

cxf-xjc-plugin

Pom.xml
<plugin>
 <groupId>org.apache.cxf</groupId>
 <artifactId>cxf-xjc-plugin</artifactId>
 <version>2.3.0</version>
 <configuration>
  <extensions>
   <extension>org.apache.cxf.xjcplugins:cxf-xjc-dv:2.3.0</extension>
  </extensions>
 </configuration>
 <executions>
  <execution>
   <id>generate-sources</id>
   <phase>generate-sources</phase>
   <goals>
    <goal>xsdtojava</goal>
   </goals>
   <configuration>
    <sourceRoot>${basedir}/src/main/java</sourceRoot>
    <xsdOptions>
     <xsdOption>
      <xsd>${basedir}/src/main/resources/META-INF/xsd/myTest-1.xsd</xsd>
      <packagename>es.pruebas.model1</packagename>
     </xsdOption>
     <xsdOption>
      <xsd>${basedir}/src/main/resources/META-INF/xsd/myTest-2.xsd</xsd>
      <packagename>es.pruebas.model2</packagename>
     </xsdOption>         
    </xsdOptions>
   </configuration>
  </execution>
 </executions>
</plugin>

This plugin does not generate @XmlRootElement annotation in classes.
In order to marshall and unmarshall this objects you have to use ObjectFactory class.

Java code for conversion.
ObjectFactory objectFactory = new ObjectFactory();
JAXBElement<myrootclass> respuestaJAXBElement = objectFactory.createPublicacion(myRootObject);

final Marshaller marshaller = JAXBContext.newInstance(ObjectFactory.class).createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

final StringWriter writer = new StringWriter();
marshaller.marshal(respuestaJAXBElement, writer);
return writer.toString();

final Unmarshaller unmarshaller = JAXBContext.newInstance(ObjectFactory.class).createUnmarshaller();
JAXBElement<myrootclass> jaxbElement = (JAXBElement<myrootclass>) unmarshaller.unmarshal(inputStream);
return jaxbElement.getValue();

maven-jaxb2-plugin

Pom.xml
<plugin>
 <groupId>org.jvnet.jaxb2.maven2</groupId>
 <artifactId>maven-jaxb2-plugin</artifactId>
 <version>0.12.3</version>
 <executions>
  <execution>
   <id>Generate-clases-xsd</id>
   <phase>generate-sources</phase>
   <goals>
    <goal>generate</goal>
   </goals>

   <configuration>
    <schemaDirectory>src/main/resources/META-INF/xsd/</schemaDirectory>
    <schemaIncludes>
     <include>myTest-1.xsd</include>
     <include>myTest-2.xsd</include>
    </schemaIncludes>
    <bindingDirectory>src/main/resources/META-INF/xsd</bindingDirectory>
    <bindingIncludes>
     <include>all-schemas-bindings.xjb</include>
    </bindingIncludes>
   </configuration>
  </execution>
 </executions>
 <configuration>
  <args>
   <arg>-Xannotate</arg>
   <arg>-nv</arg>
  </args>
  <extension>true</extension>
  <debug>false</debug>
  <verbose>false</verbose>
  <episode>false</episode>
  <forceRegenerate>false</forceRegenerate>
  <plugins>
   <plugin>
    <groupId>org.jvnet.jaxb2_commons</groupId>
    <artifactId>jaxb2-basics</artifactId>
    <version>0.9.4</version>
   </plugin>
   <plugin>
    <groupId>org.jvnet.jaxb2_commons</groupId>
    <artifactId>jaxb2-basics-annotate</artifactId>
    <version>1.0.1</version>
   </plugin>
  </plugins>
 </configuration>
</plugin>

In binding files you can specify wich class is the rootElement, so you can get @XmlRootElement annotation.

all-schemas-bindings.xjb
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings
    version="2.1"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:annox="http://annox.dev.java.net"  >

 <jaxb:globalBindings generateIsSetMethod="true"/>

 
    <jaxb:bindings schemaLocation="myTest-1.xsd" node="/xs:schema">    
        <jaxb:schemaBindings>
            <jaxb:package name="es.pruebas.model1"/>
        </jaxb:schemaBindings>
        
        <jaxb:bindings node="//xs:complexType[@name='MyRootElement1']">
            <annox:annotate>
                <annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement" name="MyRootElement1"/>
            </annox:annotate>
        </jaxb:bindings>        
    </jaxb:bindings>
    
           
    
     <jaxb:bindings schemaLocation="myTest-2.xsd" node="/xs:schema"> 
        <jaxb:schemaBindings>
            <jaxb:package name="es.pruebas.model2"/>
        </jaxb:schemaBindings>
        
        <jaxb:bindings node="//xs:element[@name='MyRootElement2']">
            <annox:annotate>
                <annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement" name="MyRootElement2"/>
            </annox:annotate>
        </jaxb:bindings>        
    </jaxb:bindings>

</jaxb:bindings>

No hay comentarios:

Publicar un comentario