I\'m trying to ensure that all JSON responses from a Jersey based java application have a UTF-8 character encoding parameter appended to their ContentType header.
So
This is not going to work in this way.
When you call chain.doFilter(request, response); your headers are already flushed and you can't reset them later on.
What you can do is actually a quick and dirty trick:
public void doFilter(...) {
HttpServletResponse resp = new HttpServletResponseWrapper(response) {
public void setContentType(String ct) {
if(ct!=null && ct.toLowerCase().startsWith("application/json")) {
super.setContentType("application/json;charset=UTF-8");
} else {
super.setContentType(ct);
}
}
}
// Set content type manually to override any potential defaults,
// See if you need it at all
response.setContentType("application/json;charset=UTF-8");
chain.doFilter(request, resp); // Inject our response!
}
EDIT: ct.toUpperCase().startsWith("application/json") changed to ct.toLowerCase().startsWith("application/json").