diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java index fbaa5c67bd..b7fce9dd15 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java @@ -178,6 +178,9 @@ public class ThymeleafAutoConfiguration { resolver.setTemplateEngine(this.templateEngine); resolver.setCharacterEncoding(this.environment.getProperty("encoding", "UTF-8")); + resolver.setContentType(addEncoding( + this.environment.getProperty("contentType", "text/html"), + resolver.getCharacterEncoding())); resolver.setExcludedViewNames(this.environment.getProperty( "excludedViewNames", String[].class)); resolver.setViewNames(this.environment.getProperty("viewNames", @@ -188,6 +191,15 @@ public class ThymeleafAutoConfiguration { return resolver; } + private String addEncoding(String type, String charset) { + if (type.contains("charset=")) { + return type; + } + else { + return type + ";charset=" + charset; + } + } + } @Configuration diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfigurationTests.java index 7f2724a85a..a344fe81ee 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfigurationTests.java @@ -84,6 +84,7 @@ public class ThymeleafAutoConfigurationTests { assertEquals("UTF-16", ((TemplateResolver) resolver).getCharacterEncoding()); ThymeleafViewResolver views = this.context.getBean(ThymeleafViewResolver.class); assertEquals("UTF-16", views.getCharacterEncoding()); + assertEquals("text/html;charset=UTF-16", views.getContentType()); } @Test