Some servlet containers (at least Undertow and Tomcat) allow clearing a previously set character encoding by calling response.setCharacterEncoding(null). This is useful when CharacterEncodingFilter is used, but some handlers need to deliver binary data without the charset clause in the Content-Type response header.

Code doing that cannot currently be tested with MockMvc, because MockHttpServletResponse.setCharacterEncoding(null) throws an IllegalArgumentException.

The Javadoc of the Servlet API in JakartaEE has been updated a while ago to support null there: https://github.com/jakartaee/servlet/commit/22c8f34a4a649f4ffb1ddaf36e3102b574079612

The JakartaEE guys were quite unanimous about the subject: https://github.com/jakartaee/servlet/issues/377

MockHttpServletResponse.setCharacterEncoding(null) should clear any character encoding previously set on the response.

Comment From: rstoyanchev

Sounds like we need to catch up to and align with the Servlet 6 API updates.

Comment From: poutsma

This issue was closed by mistake, due to an invalid github comment