@EnableWebMvc annotation changes response encoding

and*_*ero 0 spring spring-mvc character-encoding http-headers spring-boot

By default, my Spring Boot application responds to any request with the following Content-Type headers:

Content-Type: text/html;charset=UTF-8
Run Code Online (Sandbox Code Playgroud)

I believe this has been so from the beginning and I would like to keep it this way.

However, by making a simple change and only adding @EnableWebMvc to my @SpringBootApplication annotated class, the charset attribute is changed:

Content-Type: text/html;charset=ISO-8859-1
Run Code Online (Sandbox Code Playgroud)

And so the UTF-8 encoded content of the response is displayed incorrectly on the website.

And*_*son 6

您无需添加@EnableWebMvc到实现的类WebMvcConfigurer。如果要添加一个拦截器,则使您的WebMvcConfigurer实现作为bean可用。您可以通过用注释类@Component(假设其包已被类路径扫描覆盖)或通过从类@Bean上的方法返回其实例来实现@Configuration