@Override public void write(JSONEntity entity, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { // First write the entity to a JSON string try { HttpHeaders headers = outputMessage.getHeaders(); if (headers.getContentType() == null) { if (contentType == null || contentType.isWildcardType() || contentType.isWildcardSubtype()) { contentType = MediaType.APPLICATION_JSON; } if (contentType != null) { headers.setContentType(contentType); } } String jsonString = EntityFactory.createJSONStringForEntity(entity); long length = JSONEntityHttpMessageConverter.writeToStream( jsonString, outputMessage.getBody(), contentType.getCharSet()); if (headers.getContentLength() == -1) { headers.setContentLength(length); } } catch (JSONObjectAdapterException e) { throw new HttpMessageNotWritableException(e.getMessage()); } }