Jaxb object prints as xml

I have a class, call it User annotated with @XmlRootElement , with some properties (first name, last name, etc.).

I use this class for REST operations, like application/xml .

The client will be a POST User class, so I want to save the values ​​in a log.

Is there any method in jaxb to print this object as xml ?

For instance:

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

should produce this conclusion:

  Customer sent: 
 <user> <name>cristi</name> <surname>kevin</surname> </user> 

Thanks.

+4
source share
4 answers

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 this:

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

You can do this as a regular method available to 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 ""; } 
+17
source

Setting Marshaller.JAXB_FORMATTED_OUTPUT can be bad 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 print a single line like this.

 <user><name>cristi</name><surname>kevin</surname></user> 
+2
source
 public String toXml(Event event) { ByteArrayOutputStream baos = null; try { JAXBContext jc = JAXBContext.newInstance(event.getClass()); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); baos = new ByteArrayOutputStream(); marshaller.marshal(event, baos); return baos.toString(); } catch (JAXBException e) { LOGGER.log(Level.SEVERE, " problem in Logging raw XML :"+e.getMessage()); } return baos.toString(); 

It works well

0
source

Source: https://habr.com/ru/post/1304344/


All Articles