Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffsheets/616bc9c2ffa6e5b48dc6 to your computer and use it in GitHub Desktop.
Save jeffsheets/616bc9c2ffa6e5b48dc6 to your computer and use it in GitHub Desktop.
A MappingJackson2HttpMessageConverter that forces the response Content-Type header to be application/json
public class ContentTypeMappingJackson2HttpMessageConverter extends MappingJackson2HttpMessageConverter {
/**
* Always set the Content-Type response header to application/json when using the Jackson message converter
*
* Without this there appears to be a conflict with Meteor/Atmosphere servlet that causes Spring
* to leave the Content-Type blank in the response
* https://groups.google.com/forum/#!topic/atmosphere-framework/uZOrfXl3Bu8
*/
@Override
protected void writeInternal(Object object, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
outputMessage.getHeaders().setContentType(MediaType.APPLICATION_JSON);
super.writeInternal(object, outputMessage);
}
}
@greatbody
Copy link

where should we place this class?

@jeffsheets
Copy link
Author

@greatbody, you'll need to register it as a messageConverter, usually in a WebMvcConfigurer extension class.

sorry for the slow response, apparently github still doesn't notify for comments on gists...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment