Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jaxb entity print out as xml

Tags:

java

jaxb

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.

like image 648
Cristian Boariu Avatar asked Mar 17 '10 09:03

Cristian Boariu


People also ask

Is JAXB deprecated?

JAXB is one of the APIs in the Jakarta EE platform (formerly Java EE), part of the Java Web Services Development Pack (JWSDP), and one of the foundations for WSIT. It was also part of the Java SE platform (in version Java SE 6-10). As of Java SE 11, JAXB was removed.

Is JAXB XML parser?

JAXB — Java Architecture for XML Binding — is used to convert objects from/to XML. JAXB offers a fast and suitable way to marshal (write) Java objects into XML and unmarshal (read) XML into Java objects. It supports Java annotations to map XML elements to Java attributes.


3 Answers

You can make this as a common method accessible by your endpoints.

public String toXml(JAXBElement element) {
    try {
        JAXBContext jc = JAXBContext.newInstance(element.getValue().getClass());  
        Marshaller marshaller = jc.createMarshaller();  
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);  

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        marshaller.marshal(element, baos);
        return baos.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }      
    return "";
}
like image 140
java25 Avatar answered Oct 06 '22 02:10

java25


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());
like image 29
Cristian Boariu Avatar answered Oct 06 '22 00:10

Cristian Boariu


Setting Marshaller.JAXB_FORMATTED_OUTPUT may be not good for logging.

Instead suppress XML Prolog (or Declaration) with Marshaller.JAXB_FRAGMENT.

public static <J> String printXml(final J instance) throws JAXBException {
    return printXml(instance, instance.getClass());
}


public static <J> String printXml(final J instance,
                                  final Class<?>... classesToBeBound)
    throws JAXBException {

    final JAXBContext context = JAXBContext.newInstance(classesToBeBound);

    final ByteArrayOutputStream output = new ByteArrayOutputStream();

    final Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
    marshaller.marshal(instance, output);
    // output.flush(); // Nasty IOException
    final String jaxbEncoding = (String) marshaller.getProperty(
        Marshaller.JAXB_ENCODING);

    try {
        return new String(output.toByteArray(), jaxbEncoding);
    } catch (UnsupportedEncodingException uee) {
        throw new RuntimeException(uee);
    }
}

will prints a single line like this.

<user><name>cristi</name><surname>kevin</surname></user>
like image 45
Jin Kwon Avatar answered Oct 06 '22 02:10

Jin Kwon