public String marshall(Object entity, MediaType mediaType, Charset charset) throws IOException { Type entityType; if (entity instanceof GenericEntity) { final GenericEntity ge = (GenericEntity) entity; entityType = ge.getType(); entity = ge.getEntity(); } else { entityType = entity.getClass(); } final Class entityClass = entity.getClass(); MessageBodyWriter writer = messageBodyFactory.getMessageBodyWriter(entityClass, entityType, new Annotation[0], mediaType); if (writer == null) { throw new RuntimeException("Unable to find a message body writer for " + entityClass); } ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); writer.writeTo(entity, entityClass, entityType, new Annotation[0], mediaType, new OutBoundHeaders(), outputStream); try { return outputStream.toString(charset.name()); } catch (UnsupportedEncodingException e) { throw new RuntimeException("Should never happen as we have already seen the " + "Charset is supported as we have it as a parameter"); } }