JAXBElementEntityProvider implements EntityProvider<JAXBElement<?>> { @Override public void writeTo(JAXBElement<?> t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException { Class<?> declaredType = t.getDeclaredType(); try { JAXBContext jaxbContext = getJAXBContext(declaredType, mediaType); Marshaller marshaller = jaxbContext.createMarshaller(); String charset = getCharset(mediaType); if (!isNullOrEmpty(charset)) { marshaller.setProperty(Marshaller.JAXB_ENCODING, charset); } marshaller.marshal(t, entityStream); } catch (JAXBException e) { throw new IOException(String.format("Can't write to output stream, %s", e)); } } JAXBElementEntityProvider(@Context Providers providers); @Override boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType); @Override JAXBElement<?> readFrom(Class<JAXBElement<?>> type,
                                   Type genericType,
                                   Annotation[] annotations,
                                   MediaType mediaType,
                                   MultivaluedMap<String, String> httpHeaders,
                                   InputStream entityStream); @Override long getSize(JAXBElement<?> t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType); @Override boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType); @Override void writeTo(JAXBElement<?> t,
                        Class<?> type,
                        Type genericType,
                        Annotation[] annotations,
                        MediaType mediaType,
                        MultivaluedMap<String, Object> httpHeaders,
                        OutputStream entityStream);  }
@Test public void writesJAXBElementToOutputStream() throws Exception { Class<?> type = JAXBElement.class; Type genericType = newParameterizedType(JAXBElement.class, Book.class); JAXBElement<Book> bookJAXBElement = unmarshalJAXBElement(TEST_XML_CONTENT_BYTES); ByteArrayOutputStream out = new ByteArrayOutputStream(); writer.writeTo(bookJAXBElement, type, genericType, null, APPLICATION_XML_TYPE, null, out); JAXBElement<Book> writtenBookJAXBElement = unmarshalJAXBElement(out.toByteArray()); Book book = writtenBookJAXBElement.getValue(); assertEquals("Java and XML Data Binding", book.getTitle()); assertTrue(book.isSendByPost()); assertEquals("Brett McLaughlin", book.getAuthor()); assertEquals("USD", book.getPrice().getCurrency()); assertEquals(new BigDecimal("34.95"), book.getPrice().getValue()); assertEquals("USD", book.getMemberPrice().getCurrency()); assertEquals(new BigDecimal("26.56"), book.getMemberPrice().getValue()); }