Jaxb StringWriter如何获得新的价值?

Jaxb StringWriter如何获得新的价值?,jaxb,resteasy,Jaxb,Resteasy,有人能向我解释writer.toString如何保存客户数据吗?似乎封送员(客户、编写者)以某种方式修改了编写器。请参阅下文,谢谢: Customer customer = new Customer(); customer.setId(42); customer.setName("Bill Burke"); JAXBContext ctx = JAXBContext.newInstance(Customer.class); StringWriter writer = new Strin

有人能向我解释writer.toString如何保存客户数据吗?似乎封送员(客户、编写者)以某种方式修改了编写器。请参阅下文,谢谢:

 Customer customer = new Customer();
 customer.setId(42);
 customer.setName("Bill Burke");
 JAXBContext ctx = JAXBContext.newInstance(Customer.class);
 StringWriter writer = new StringWriter();
 ctx.createMarshaller().marshal(customer, writer);
[见此处:]

 String custString = writer.toString();  // how did writer suddenly take on value?

 customer = (Customer)ctx.createUnmarshaller()
             .unmarshal(new StringReader(custString));

我猜编写器之所以能够编写内容,是因为封送处理程序必须在write对象内调用一个方法,该方法更改了write指向的任何内容。因此,在marshal()之后,编写器引用新数据


(请参见底部)

StringWriter是一种写入StringBuffer的写入器实现。调用write()时,将附加到该StringBuffer。ToString()调用该StringBuffer上的ToString()并返回它

Marshaller.marshall()只是将JAXB对象序列化为XML,并将它们写入编写器(从而写入StringBuffer)。然后,第二个代码段中的StringReader读取StringBuffer创建的字符串