2012-03-09 44 views
1

如果類是集合的成員,那麼我的類上的afterUnmarshal()方法不會被調用,我遇到問題了。JAXB/MOXy - afterUnmarshal()似乎調用不一致

除了在通過解組創建的類上聲明方法之外,還需要執行其他任何步驟嗎? (我看不到任何東西在the docs

這是一個測試這說明我有這個問題:

鑑於這兩個領域類:

@XmlRootElement(name="Parent") 
public class Parent { 

    public boolean unmarshalCalled = false; 

    @XmlPath("Children/Child") 
    List<Child> children; 

    void afterUnmarshal(Unmarshaller u, Object parent) 
    { 
     unmarshalCalled = true; 
    } 
} 


@XmlAccessorType(XmlAccessType.FIELD) 
public class Child { 

    public boolean unmarshalCalled = false; 

    @Getter @Setter 
    @XmlPath("@name") 
    private String name; 

    void afterUnmarshal(Unmarshaller u, Object parent) 
    { 
     unmarshalCalled = true; 
    } 
} 

該測試失敗:

public class UnmarshalTest { 

    @Test 
    @SneakyThrows 
    public void testUnmarshal() 
    { 
     String xml = "<Parent><Children><Child name='Jack' /><Child name='Jill' /></Children></Parent>"; 
     JAXBContext context = getContext(); 
     Parent parent = (Parent) context.createUnmarshaller().unmarshal(new StringReader(xml)); 
     assertTrue(parent.unmarshalCalled); 
     for (Child child : parent.children) 
     { 
      assertThat(child.getName(),notNullValue()); 
      assertTrue(child.unmarshalCalled); // This assertion fails 
     } 
    } 
    @SneakyThrows 
    public static JAXBContext getContext() 
    { 
     JAXBContext context; 
     context = org.eclipse.persistence.jaxb.JAXBContext.newInstance(Parent.class); 
     return context; 
    } 
} 

這是一個錯誤,還是我錯過了一些步驟,以使其正常工作?

回答

1

你所看到的問題是由於以下的EclipseLink莫西錯誤:

此bug已被固定的EclipseLink 2.3.3流中,夜間下載可

:從得到的

解決方法

可以解決辦法,你是通過確保所有與事件方法的類都包含傳遞中創建的JAXBContext類數組中發現的問題。我在下面修改了您的代碼以執行此操作:

@SneakyThrows 
public static JAXBContext getContext() 
{ 
    JAXBContext context; 
    context = org.eclipse.persistence.jaxb.JAXBContext.newInstance(Parent.class, Child.class); 
    return context; 
} 
相關問題