简体   繁体   中英

Marshall null values with JAXB

I have an Java object of type rootObject I want to marshall .

This object looks something like :

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD);
public class rootObject extends Object implements Serializable {


    private static final long serialVersionUID = 1L;

    private typeObject1 Object1;
    private typeObject2 Object2;
    private typeObject3 Object3;
    private typeObject4 Object4;
    private typeObject5 Object5;
    private typeObject6 Object6;

}

Those private objects also have object that has objects that has object that has primitive types etc... (a biiiiiiiiig hierarchy) for example :

public class typeObject1 implements Serializable {

    private static final long serialVersionUID = 1L;

    private typeObject1_1 Object1_1;

}

public class typeObject1_1 implements Serializable {

    private static final long serialVersionUID = 1L;

    private int foo;
    private String bar;

}

All i want is to marshall an instance of rootObject by creating ALL the tags even when a String is empty/null, an object is null etc. all tags should be created.

The only thing I don't want to do is to put everywhere in the code things like @xmlElement(nillable=true) before each field (because i have a loooooooot of fields...)

This is possible using Jboss JAXBIntroductions . I'll walk you through an example built with Maven . I have made this example's source available as a git repo on Github .

Project layout

.
|-- pom.xml
`-- src
    |-- main
    |   |-- java
    |   |   `-- com
    |   |       `-- sahilm
    |   |           |-- ChildObject.java
    |   |           |-- Main.java
    |   |           `-- RootObject.java
    |   `-- resources
    |       `-- jaxb-intros.xml
    `-- test
        |-- java
        `-- resources

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.sahilm</groupId>
  <artifactId>jaxb-intros-example</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>jaxb-intros-example</name>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java-version>1.6</java-version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>jboss.jaxbintros</groupId>
      <artifactId>jboss-jaxb-intros</artifactId>
      <version>1.0.2.GA</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>${java-version}</source>
          <target>${java-version}</target>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.5</version>
        <configuration>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <repositories>
    <repository>
      <id>jboss-public-repository-group</id>
      <name>JBoss Public Maven Repository Group</name>
      <url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
      <layout>default</layout>
      <releases>
        <enabled>true</enabled>
        <updatePolicy>never</updatePolicy>
      </releases>
      <snapshots>
        <enabled>true</enabled>
        <updatePolicy>never</updatePolicy>
      </snapshots>
    </repository>
  </repositories>
</project>

Note how I have added the Jboss Maven repository since jaxb-intros isn't available in maven central yet.

jaxb-intros.xml

This is where you add your metadata. We leverage the power of regular expressions to annotate all our fields in one go.

<jaxb-intros xmlns="http://www.jboss.org/xsd/jaxb/intros">
  <Class name="com.sahilm.*">
    <XmlAccessorType value="FIELD" />
    <Field name=".+">
      <XmlElement nillable="true" />
    </Field>
  </Class>
</jaxb-intros>

All fields in all classes in the com.sahilm.* package have been marked nillable .

Mapped Objects

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class RootObject {
    private ChildObject obj1;
    private String obj2;
    private Date obj3;

    // No-arg ctor needed by jaxb
    public RootObject() {
    }

    public RootObject(final ChildObject obj1, final String obj2, final Date obj3) {
        this.obj1 = obj1;
        this.obj2 = obj2;
        this.obj3 = obj3;
    }

}


public class ChildObject {
    private String childObj1;
    private BigDecimal childObj2;

    // No-arg ctor needed by jaxb
    public ChildObject() {
    }

    public ChildObject(final String childObj1, final BigDecimal childObj2) {
        this.childObj1 = childObj1;
        this.childObj2 = childObj2;
    }

}

Notice how annotations are missing from all fields. We've already mapped them in jaxb-intros.xml .

Main.java

final JaxbIntros config = IntroductionsConfigParser.parseConfig(Main.class.getResourceAsStream("/jaxb-intros.xml"));
final IntroductionsAnnotationReader reader = new IntroductionsAnnotationReader(config);
final Map<String, Object> jaxbConfig = new HashMap<String, Object>();
jaxbConfig.put(JAXBRIContext.ANNOTATION_READER, reader);
final JAXBContext jc = JAXBContext.newInstance(new Class[] { RootObject.class, ChildObject.class }, jaxbConfig);
final Marshaller marshaller = jc.createMarshaller();
final RootObject rootObject = new RootObject(new ChildObject("Foobar", null), null, null);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(rootObject, System.out);

Output

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<rootObject>
    <obj1>
        <childObj1>Foobar</childObj1>
        <childObj2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
    </obj1>
    <obj2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
    <obj3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</rootObject>

All the best!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM