Since Spring 5.2 MediaType.APPLICATION_JSON_UTF8 is @Deprecated as described within MediaType class:
/**
* A String equivalent of \{@link MediaType#APPLICATION_JSON_UTF8}.
* @deprecated as of 5.2 in favor of \{@link #APPLICATION_JSON_VALUE}
* since major browsers like Chrome
* <a href="https://bugs.chromium.org/p/chromium/issues/detail?id=438464">
* now comply with the specification</a> and interpret correctly UTF-8 special
* characters without requiring a \{@code charset=UTF-8} parameter.
*/
@Deprecated
public static final String APPLICATION_JSON_UTF8_VALUE = "application/json;charset=UTF-8";
So basically everyone should be using MediaType.APPLICATION_JSON_VALUE
without (UTF8 information) instead. This is unfortunately in conflict with MockHttpServletResponse
:
private String characterEncoding = "ISO-8859-1";
So even thought UTF8 is expected by default, the testing environment is in violation with aboves:
now comply with the specification</a> and interpret correctly UTF-8 special
The tests which explicitly don't override this encoding will fail.
Comment From: rstoyanchev
The characterEncoding in MockHttpServletResponse
simply reflects the Serlvet spec default. It shouldn't interfere with MappingJackson2HttpMessageConverter
which writes UTF-8 directly to the OutputStream
. Can you clarify how tests fail?
Comment From: vojkny
The characterEncoding in
MockHttpServletResponse
simply reflects the Serlvet spec default. It shouldn't interfere withMappingJackson2HttpMessageConverter
which writes UTF-8 directly to theOutputStream
. Can you clarify how tests fail?
When the response from a JSON endpoint contains UTF-8 characters (ěščřžýá), then the MockHttpServletResponse will consider them as ISO-8859-1 and the these characters will be received broken. If the tests perform assertEquals on such strings, they will fail.
Comment From: rstoyanchev
So you're probably doing this?
assertEquals("...", response.getContentAsString());
but should be:
assertEquals("...", response.getContentAsString(StandardCharsets.UTF_8));
Comment From: vojkny
The problem is that until having original aplication/json; charset=UTF-8
this worked correctly.
again:
now comply with the specification</a> and interpret correctly UTF-8 special
it is just strange to migrate to version without charset=UTF-8
when all tests are now broken.
Comment From: rstoyanchev
Right but the change reflects the fact that it is now up to clients to be compliant and interpret this correctly. In MockMvc we automatically interpret JSON as UTF-8 but in this case you're reading directly from the response and it is up to your tests to do that. It is too low level a place for us to start adding functionality like that.
Comment From: vojkny
Ok, thanks.