MockMvc在Spring Boot 2.2.0中不再处理UTF-8字符. [英] MockMvc no longer handles UTF-8 characters with Spring Boot 2.2.0.RELEASE

查看:352
本文介绍了MockMvc在Spring Boot 2.2.0中不再处理UTF-8字符.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

升级到新发布的Spring Boot的 2.2.0.RELEASE 版本后,我的某些测试失败了.看来 MediaType.APPLICATION_JSON_UTF8 已被弃用,并且不再从未明确指定内容类型的控制器方法中作为默认内容类型返回.

After I upgraded to the newly released 2.2.0.RELEASE version of Spring Boot some of my tests failed. It appears that the MediaType.APPLICATION_JSON_UTF8 has been deprecated and is no longer returned as default content type from controller methods that do not specify the content type explicitly.

类似的测试代码

String content = mockMvc.perform(get("/some-api")
            .contentType(MediaType.APPLICATION_JSON))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
            .andReturn()
            .getResponse()
            .getContentAsString();

突然无法正常工作,因为内容类型不匹配,如下所示

suddenly did not work anymore as the content type was mismatched as shown below

java.lang.AssertionError: Content type 
Expected :application/json;charset=UTF-8
Actual   :application/json

将代码更改为 .andExpect(content().contentType(MediaType.APPLICATION_JSON))即可解决此问题.

Changing the code to .andExpect(content().contentType(MediaType.APPLICATION_JSON)) resolved the issue for now.

但是现在,当将 content 与预期的序列化对象进行比较时,如果对象中有任何特殊字符,仍然会出现不匹配的情况.似乎 .getContentAsString()方法默认情况下(不再)没有使用UTF-8字符编码.

But now when comparing content to the expected serialized object there is still a mismatch if there are any special characters in the object. It appears that the .getContentAsString() method does not make use of the UTF-8 character encoding by default (any more).

java.lang.AssertionError: Response content expected:<[{"description":"Er hörte leise Schritte hinter sich."}]> but was:<[{"description":"Er hörte leise Schritte hinter sich."}]>
Expected :[{"description":"Er hörte leise Schritte hinter sich."}]
Actual   :[{"description":"Er hörte leise Schritte hinter sich."}]

如何获取UTF-8编码的 content ?

How can I get content in UTF-8 encoding?

推荐答案

是.这是从2.2.0 spring-boot起的问题.他们为默认字符集编码设置了弃用.

Yes. This is problem from 2.2.0 spring-boot. They set deprecation for default charset encoding.

.getContentAsString(StandardCharsets.UTF_8)-很好,但是默认情况下,在任何响应中都将填充ISO 8859-1.

.getContentAsString(StandardCharsets.UTF_8) - good but in any response would be populated ISO 8859-1 by default.

在我的项目中,我更新了当前创建的转换器:

In my project I updated current created converter:

@Configuration
public class SpringConfig implements WebMvcConfigurer {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.stream()
            .filter(converter -> converter instanceof MappingJackson2HttpMessageConverter)
            .findFirst()
            .ifPresent(converter -> ((MappingJackson2HttpMessageConverter) converter).setDefaultCharset(UTF_8));
    }
...

这篇关于MockMvc在Spring Boot 2.2.0中不再处理UTF-8字符.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆