使用JAXB处理Marshall空值

3

我有一个类型为rootObject的Java对象,我想进行编组。

这个对象看起来像这样:

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

}

那些私有对象也有包含其他对象的对象,这些对象又包含着其他对象,直到最后是基本类型等等(一个巨大的层次结构)。 例如:

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;

}

我想做的就是通过创建所有标记来编排rootObject的实例,即使字符串为空/ null,对象为空等。必须创建所有标记。
我不想在代码的每个地方都添加类似 @xmlElement(nillable = true)的东西(因为我有很多字段...)。
1个回答

2

使用Jboss JAXBIntroductions可以实现这一点。 我将通过使用Maven构建的示例来演示。我已经将此示例的源代码作为Github上的git repo提供。

项目布局

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

请注意,我已添加了Jboss Maven存储库,因为jaxb-intros尚未在maven中央可用。

jaxb-intros.xml

这是您添加元数据的位置。我们利用正则表达式的强大功能来一次性注释所有字段。

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

com.sahilm.*包中所有类的所有字段都已标记为nillable

映射对象

@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;
    }

}

注意到所有字段都缺失了注释。我们已经在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);

输出

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

祝你一切顺利!


示例源代码可在 https://github.com/sahilm/jaxb-intros-example 上获取。 - Sahil Muthoo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接