public < T > void writeBodyFromObject ( T bodyAsObject , Charset charset ) { @ SuppressWarnings ( "unchecked" ) Class < T > clazz = ( Class < T > ) bodyAsObject . getClass ( ) ; ByteArrayOutputStream outputStream = new ByteArrayOutputStream ( ) ; Writer outputWriter = new OutputStreamWriter ( outputStream , charset ) ; try { Marshaller marshaller = JAXBContext . newInstance ( clazz ) . createMarshaller ( ) ; if ( clazz . isAnnotationPresent ( XmlRootElement . class ) ) { marshaller . marshal ( bodyAsObject , outputWriter ) ; } else { String tagName = unCapitalizedClassName ( clazz ) ; JAXBElement < T > element = new JAXBElement < T > ( new QName ( "" , tagName ) , clazz , bodyAsObject ) ; marshaller . marshal ( element , outputWriter ) ; } } catch ( JAXBException e ) { throw new RuntimeException ( e ) ; } byte [ ] bodyContent = outputStream . toByteArray ( ) ; message . contentType ( Message . APPLICATION_XML ) . contentEncoding ( charset . name ( ) ) ; message . body ( bodyContent ) ; }
Writes the body by serializing the given object to XML .