ToStringBodyWriter implements MessageBodyWriter<Object> { @Override public void writeTo(Object o, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { StringBodyConverter converter = StringBodyConverter.valueOfOrDefault(mediaType.getSubtype()); entityStream.write(converter.convert(o).getBytes(StandardCharsets.UTF_8)); }  @Override boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType); @Override long getSize(Object o, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType); @Override void writeTo(Object o, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream);  }
@Test public void test() throws IOException { ToStringBodyWriter bw = new ToStringBodyWriter(); List<String> list = Arrays.asList("apple", "orange", "banana"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bw.writeTo(list, null, null, null, MediaType.TEXT_PLAIN_TYPE, null, baos); assertThat(new String(baos.toByteArray())).isEqualTo("[apple, orange, banana]"); baos.reset(); bw.writeTo(list, null, null, null, MediaType.TEXT_HTML_TYPE, null, baos); assertThat(new String(baos.toByteArray())).isEqualTo("<html><body>[apple, orange, banana]</body></html>"); baos.reset(); bw.writeTo(list, null, null, null, MediaType.TEXT_XML_TYPE, null, baos); assertThat(new String(baos.toByteArray())).isEqualTo("<message>[apple, orange, banana]</message>"); baos.reset(); bw.writeTo(list, null, null, null, MediaType.WILDCARD_TYPE, null, baos); assertThat(new String(baos.toByteArray())).isEqualTo("[apple, orange, banana]"); }