Stack Exchange
Cannot set Content-Type / MIME-type in java servlet
I'm using the following filter, explicitly setting the content-type for the response to application/json:
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    HttpServletResponse response = (HttpServletResponse) servletResponse;
    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE");
    response.setHeader("Access-Control-Max-Age", "3600");
    response.setContentType("application/json");
    response.setCharacterEncoding("utf-8");
    response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    filterChain.doFilter(servletRequest, servletResponse);
}

It does not work, however, and the content-type when requested in a browser is text/html. The other headers work fine. 

What happens downstream to the HttpServletResponse?

Is something down the filterChain overriding the headers? What if you move the setContentType after the doFilter?

The response is dispatched by a Spring @RestController. Moving the setContentType to the bottom of the method didn't change the outcome.

I realize this is old.
I've had this issue and my workaround is:
    resp.setHeader("Content-Disposition","attachment; filename="+fname);

See: http://archive.oreilly.com/pub/a/onjava/excerpt/jebp_3/index3.html
