JAXBObjectEntityProvider implements EntityProvider<Object> { @Override public void writeTo(Object t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException { try { JAXBContext jaxbContext = getJAXBContext(type, 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)); } } JAXBObjectEntityProvider(@Context Providers providers); @Override boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType); @Override Object readFrom(Class<Object> type,
                           Type genericType,
                           Annotation[] annotations,
                           MediaType mediaType,
                           MultivaluedMap<String, String> httpHeaders,
                           InputStream entityStream); @Override long getSize(Object t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType); @Override boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType); @Override void writeTo(Object t,
                        Class<?> type,
                        Type genericType,
                        Annotation[] annotations,
                        MediaType mediaType,
                        MultivaluedMap<String, Object> httpHeaders,
                        OutputStream entityStream);  }
@Test public void writesObjectToOutputStream() throws Exception { Book book = createBook(); ByteArrayOutputStream out = new ByteArrayOutputStream(); writer.writeTo(book, Book.class, Book.class, null, APPLICATION_XML_TYPE, null, out); Book unmarshaledBook = unmarshalBook(out.toByteArray()); assertEquals(book.getTitle(), unmarshaledBook.getTitle()); assertEquals(book.isSendByPost(), unmarshaledBook.isSendByPost()); assertEquals(book.getAuthor(), unmarshaledBook.getAuthor()); assertEquals(book.getPrice().getCurrency(), unmarshaledBook.getPrice().getCurrency()); assertEquals(book.getPrice().getValue(), unmarshaledBook.getPrice().getValue()); assertEquals(book.getMemberPrice().getCurrency(), unmarshaledBook.getMemberPrice().getCurrency()); assertEquals(book.getMemberPrice().getValue(), unmarshaledBook.getMemberPrice().getValue()); }