使用JAXB创建空对象解组空元素 [英] Create Null Object Unmarshalling Empty Element with JAXB

查看:238
本文介绍了使用JAXB创建空对象解组空元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JAX-RS Web服务中使用JAXB(EclipseLink实现)。在XML请求中传递空元素时,会创建一个空对象。是否可以将JAXB设置为创建一个空对象?

I am using JAXB (EclipseLink implementation) in a JAX-RS webservice. When an empty element is passed in the XML request an empty object is created. Is it possible to set JAXB to create a null object instead?

示例XML:

<RootEntity>
    <AttributeOne>someText</AttributeOne>
    <EntityOne id="objectID" />
    <EntityTwo />
</RootEntity>

当解组时,创建一个EntityOne实例并将id属性设置为objectID和一个实例EntityTwo是使用null属性创建的。相反,我想为EntityTwo提供一个null对象,因为有一个空对象会导致JPA持久性操作出现问题。

When unmarshalling, an instance of EntityOne is created and the id attribute set to "objectID" and an instance of EntityTwo is created with null attributes. Instead I would like a null object for EntityTwo as having an empty object is causing me problems with JPA persistence operations.

推荐答案

你可以使用MOXy的NullPolicy指定此行为。您需要创建DescriptorCustomizer来修改底层映射。不要担心它比听起来容易,我将在下面演示:

You can specify this behaviour using MOXy's NullPolicy. You will need to create a DescriptorCustomizer to modify the underlying mappings. Don't worry it's easier than it sounds, I'll demonstrate below:

import org.eclipse.persistence.config.DescriptorCustomizer;
import org.eclipse.persistence.descriptors.ClassDescriptor;
import org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping;
import org.eclipse.persistence.oxm.mappings.nullpolicy.XMLNullRepresentationType;

public class RootEntityCustomizer implements DescriptorCustomizer {

    @Override
    public void customize(ClassDescriptor descriptor) throws Exception {
        XMLCompositeObjectMapping entityTwoMapping = (XMLCompositeObjectMapping) descriptor.getMappingForAttributeName("entityTwo");

        entityTwoMapping.getNullPolicy().setNullRepresentedByEmptyNode(true);
        entityTwoMapping.getNullPolicy().setMarshalNullRepresentation(XMLNullRepresentationType.EMPTY_NODE);
    }

}

以下是关联定制器的方法与您的模型类:

Below is how you associate the customizer with your model class:

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import org.eclipse.persistence.oxm.annotations.XmlCustomizer;

@XmlRootElement(name="RootEntity")
@XmlCustomizer(RootEntityCustomizer.class)
public class RootEntity {

    private String attributeOne;
    private Entity entityOne;
    private Entity entityTwo;

    @XmlElement(name="AttributeOne")
    public String getAttributeOne() {
        return attributeOne;
    }

    public void setAttributeOne(String attributeOne) {
        this.attributeOne = attributeOne;
    }

    @XmlElement(name="EntityOne")
    public Entity getEntityOne() {
        return entityOne;
    }

    public void setEntityOne(Entity entityOne) {
        this.entityOne = entityOne;
    }

    @XmlElement(name="EntityTwo")
    public Entity getEntityTwo() {
        return entityTwo;
    }

    public void setEntityTwo(Entity entityTwo) {
        this.entityTwo = entityTwo;
    }

}

在下一版MOXy中( 2.2)你可以通过注释来做到这一点。

In the next version of MOXy (2.2) you will be able to do this via annotations.

@XmlElement(name="EntityTwo")
@XmlNullPolicy(emptyNodeRepresentsNull=true,
              nullRepresentationForXml=XmlMarshalNullRepresentation.EMPTY_NODE)
public Entity getEntityTwo() {
    return entityTwo;
}

您现在可以使用EclipseLink 2.2.0夜间版本之一尝试:

You can try this now with one of the EclipseLink 2.2.0 nightly builds:

  • http://www.eclipse.org/eclipselink/downloads/nightly.php

这篇关于使用JAXB创建空对象解组空元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆