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;
}
}
這是一個錯誤,還是我錯過了一些步驟,以使其正常工作?