2010-08-06 4 views
1

JAX-RS 웹 서비스에서 JAXB (EclipseLink 구현)를 사용하고 있습니다. 빈 요소가 XML 요청에 전달되면 빈 객체가 만들어집니다. JAXB를 대신 null 객체를 생성하도록 설정할 수 있습니까?null 객체 만들기 JAXB로 비어있는 요소를 비 정렬 화하기

예 XML :

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

비 정렬 화하는 경우, EntityOne의 인스턴스가 생성되고 ID가 "objectID에"속성 세트와 EntityTwo의 인스턴스가 널 (null) 속성으로 생성됩니다. 대신 JPA 지속성 작업에 문제가 발생하는 빈 개체를 갖는 EntityTwo null 개체를 싶습니다.

답변

0

MOXy의 NullPolicy를 사용하여이 동작을 지정할 수 있습니다. 기본 매핑을 수정하려면 DescriptorCustomizer를 만들어야합니다. 그것은 생각보다 쉽게 ​​걱정하지 마세요, 나는 아래에 설명합니다 : 당신이 당신의 모델 클래스와 카스타을 연결하는 방법을

다음
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); 
    } 

} 

은 다음과 같습니다

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) 주석을 통해이 작업을 수행 할 수 있습니다.

당신은 EclipseLink가 2.2.0 중 하나와 지금을 시도 할 수 있습니다

야간

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

+0

, 그래서 내 클래스에 주석을 추가 . 불행히도 비어 있지만 속성이 정의되어 있으면 요소를 null로 설정하는 동작이 있습니다. 예를 들어 위의 예제에서 EntityOne은 이제 null 객체를 반환합니다. 이것은 예상 된 행동입니까? – sdoca

+0

이것은 버그입니다. https://bugs.eclipse.org/322183을 참조하십시오. 이번 주에 문제가 해결 될 것입니다. 올바른 동작을 보려면 속성 대신 요소의 id 속성을 만들 수 있습니다. –

+0

속성에서 속성으로 변경되었으며 조금 더 자세한 XML을 의미하지만 작동합니다. 감사! – sdoca

관련 문제