Java Jaxb API侦听器
我创建的Jaxb类包含以下侦听器方法,但只有unmarshall方法起作用:Java Jaxb API侦听器,java,jaxb,eclipselink,jaxb2,Java,Jaxb,Eclipselink,Jaxb2,我创建的Jaxb类包含以下侦听器方法,但只有unmarshall方法起作用: void afterUnmarshal(Unmarshaller unmarshaller, Object parent) { System.out.println("afterUnmarshal 1"); } void beforeMarshal(Marshaller marshaller, Object parent) { System.out.println("
void afterUnmarshal(Unmarshaller unmarshaller, Object parent) {
System.out.println("afterUnmarshal 1");
}
void beforeMarshal(Marshaller marshaller, Object parent) {
System.out.println("beforeMarshal 2");
}
void beforeUnmarshal(Unmarshaller unmarshaller, Object parent) {
System.out.println("beforeUnmarshal 3");
}
void afterMarshal(Marshaller marshaller, Object parent) {
System.out.println("afterMarshal 4");
}
输出:
beforeUnmarshal 3
afterUnmarshal 1
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Customer.class,ObjectFactory.class);
Unmarshaller u = jc.createUnmarshaller();
File xml = new File("src/testRJE/input.xml");
Customer customer = (Customer) u.unmarshal(xml);
Marshaller m = jc.createMarshaller();
m.marshal(customer, xml);
}
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
@XmlElementRef(name = "billing-address")
@XmlJavaTypeAdapter(AddressAdapter.class)
private Address address;
public Address getAddress() {
return address;
}
void afterUnmarshal(Unmarshaller unmarshaller, Object parent) {//XML to Object
System.out.println("afterUnmarshal Customer");
}
void beforeMarshal(Marshaller marshaller ) {
System.out.println("beforeMarshal Customer");
}
}
@XmlAccessorType(XmlAccessType.FIELD)
public class Address {
public Address() {
}
private String street;
private String city;
void afterUnmarshal(Unmarshaller unmarshaller, Object parent) {
System.out.println("afterUnmarshal Address");
}
void beforeMarshal(Marshaller marshaller ) {
System.out.println("beforeMarshal Address");
}
}
在执行编组代码时不调用marshall方法
更新问题: 问题:在封送客户两次电话之前。 输出:
afterUnmarshal Address
afterUnmarshal Customer
beforeMarshal Customer
beforeMarshal Customer
beforeMarshal Address
节目:
beforeUnmarshal 3
afterUnmarshal 1
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Customer.class,ObjectFactory.class);
Unmarshaller u = jc.createUnmarshaller();
File xml = new File("src/testRJE/input.xml");
Customer customer = (Customer) u.unmarshal(xml);
Marshaller m = jc.createMarshaller();
m.marshal(customer, xml);
}
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
@XmlElementRef(name = "billing-address")
@XmlJavaTypeAdapter(AddressAdapter.class)
private Address address;
public Address getAddress() {
return address;
}
void afterUnmarshal(Unmarshaller unmarshaller, Object parent) {//XML to Object
System.out.println("afterUnmarshal Customer");
}
void beforeMarshal(Marshaller marshaller ) {
System.out.println("beforeMarshal Customer");
}
}
@XmlAccessorType(XmlAccessType.FIELD)
public class Address {
public Address() {
}
private String street;
private String city;
void afterUnmarshal(Unmarshaller unmarshaller, Object parent) {
System.out.println("afterUnmarshal Address");
}
void beforeMarshal(Marshaller marshaller ) {
System.out.println("beforeMarshal Address");
}
}
显然,您已经扩展了类,该类只有方法
afterUnmarshal
和beforenunmarshal
,这解释了为什么只调用这两个方法
为了侦听封送处理事件,您需要另一个侦听器类进行扩展,该类反过来具有您需要的方法,并且必须重写这些方法
另外,请用注释
@override
对您覆盖的方法进行注释。在您的类中执行此操作后,您应该会出现错误,指出类解组器#侦听器没有任何方法在封送前
或在封送后
封送事件方法没有像解组方法那样的对象
参数。当您从方法签名中删除它们时,一切都应按预期工作