Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Jaxb API侦听器_Java_Jaxb_Eclipselink_Jaxb2 - Fatal编程技术网

Java Jaxb API侦听器

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("

我创建的Jaxb类包含以下侦听器方法,但只有unmarshall方法起作用:

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
对您覆盖的方法进行注释。在您的类中执行此操作后,您应该会出现错误,指出类
解组器#侦听器
没有任何方法
在封送前
在封送后

封送事件方法没有像解组方法那样的
对象
参数。当您从方法签名中删除它们时,一切都应按预期工作