jaxb entity print out as xml

java, jaxb

Solution

Found:)

public void toXml() {
    try {
        JAXBContext ctx = JAXBContext.newInstance(User.class);
        Marshaller marshaller = ctx.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.marshal(this, System.out);
    }
    catch (Exception
            e) {

              //catch exception 
    }
}

Call it like:

log.info("Customers sent: "+user.toXml());

Problem

I have a class, let's call it User annotated with `@XmlRootElement`, with some properties (name, surname etc). I use this class for REST operations, as `application/xml`. The client will POST User class so i want to keep the values in the log. Is there any method in jaxb to prints out this object as xml? For instance: ``` log.info("Customers sent: "+user.whichMethod()); ``` should produce this output: ``` Customer sent: ``` ``` <user> <name>cristi</name> <surname>kevin</surname> </user> ``` Thanks.

Original source