Skip to content

Commit cbd3c39

Browse files
committed
Override StringHttpMessageConverter with UTF-8
Override the default StringHttpMessageConverter provided by the standard Spring MVC configuration so that is uses UTF-8 instead of the aging default of the servlet spec (that is ISO-8859-1) Fixes gh-1800
1 parent 8be97fa commit cbd3c39

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/HttpMessageConvertersAutoConfiguration.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.autoconfigure.web;
1818

19+
import java.nio.charset.Charset;
1920
import java.util.Collections;
2021
import java.util.List;
2122

@@ -28,6 +29,7 @@
2829
import org.springframework.context.annotation.Bean;
2930
import org.springframework.context.annotation.Configuration;
3031
import org.springframework.http.converter.HttpMessageConverter;
32+
import org.springframework.http.converter.StringHttpMessageConverter;
3133
import org.springframework.http.converter.json.GsonHttpMessageConverter;
3234
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
3335
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
@@ -47,11 +49,14 @@
4749
* @author David Liu
4850
* @author Andy Wilkinson
4951
* @author Sebastien Deleuze
52+
* @author Stephane Nicoll
5053
*/
5154
@Configuration
5255
@ConditionalOnClass(HttpMessageConverter.class)
5356
public class HttpMessageConvertersAutoConfiguration {
5457

58+
private static final Charset UTF_8 = Charset.forName("UTF-8");
59+
5560
@Autowired(required = false)
5661
private final List<HttpMessageConverter<?>> converters = Collections.emptyList();
5762

@@ -118,4 +123,16 @@ public GsonHttpMessageConverter gsonHttpMessageConverter(Gson gson) {
118123

119124
}
120125

126+
@Configuration
127+
@ConditionalOnClass(StringHttpMessageConverter.class)
128+
protected static class StringHttpMessageConverterConfiguration {
129+
130+
@Bean
131+
@ConditionalOnMissingBean
132+
public StringHttpMessageConverter stringHttpMessageConverter() {
133+
return new StringHttpMessageConverter(UTF_8);
134+
}
135+
136+
}
137+
121138
}

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/HttpMessageConvertersAutoConfigurationTests.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2525
import org.springframework.context.annotation.Bean;
2626
import org.springframework.context.annotation.Configuration;
27+
import org.springframework.http.converter.StringHttpMessageConverter;
2728
import org.springframework.http.converter.json.GsonHttpMessageConverter;
2829
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
2930
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
@@ -46,7 +47,7 @@
4647
*/
4748
public class HttpMessageConvertersAutoConfigurationTests {
4849

49-
private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();;
50+
private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
5051

5152
@After
5253
public void close() {
@@ -134,6 +135,26 @@ public void customGsonConverter() throws Exception {
134135
assertConverterBeanRegisteredWithHttpMessageConverters(GsonHttpMessageConverter.class);
135136
}
136137

138+
@Test
139+
public void defaultStringConverter() throws Exception {
140+
this.context.register(HttpMessageConvertersAutoConfiguration.class);
141+
this.context.refresh();
142+
assertConverterBeanExists(StringHttpMessageConverter.class,
143+
"stringHttpMessageConverter");
144+
assertConverterBeanRegisteredWithHttpMessageConverters(StringHttpMessageConverter.class);
145+
}
146+
147+
@Test
148+
public void customStringConverter() throws Exception {
149+
this.context.register(StringConverterConfig.class,
150+
HttpMessageConvertersAutoConfiguration.class);
151+
this.context.refresh();
152+
assertConverterBeanExists(StringHttpMessageConverter.class,
153+
"customStringMessageConverter");
154+
155+
assertConverterBeanRegisteredWithHttpMessageConverters(StringHttpMessageConverter.class);
156+
}
157+
137158
private void assertConverterBeanExists(Class<?> type, String beanName) {
138159
assertEquals(1, this.context.getBeansOfType(type).size());
139160
List<String> beanNames = Arrays.asList(this.context.getBeanDefinitionNames());
@@ -202,4 +223,13 @@ public GsonHttpMessageConverter customGsonMessageConverter(Gson gson) {
202223
}
203224
}
204225

226+
@Configuration
227+
protected static class StringConverterConfig {
228+
229+
@Bean
230+
public StringHttpMessageConverter customStringMessageConverter() {
231+
return new StringHttpMessageConverter();
232+
}
233+
}
234+
205235
}

spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,8 @@ formatters, view controllers etc.) you can add your own `@Bean` of type
886886
Spring MVC uses the `HttpMessageConverter` interface to convert HTTP requests and
887887
responses. Sensible defaults are included out of the box, for example Objects can be
888888
automatically converted to JSON (using the Jackson library) or XML (using the Jackson
889-
XML extension if available, else using JAXB).
889+
XML extension if available, else using JAXB). Strings are encoded using `UTF-8` by
890+
default.
890891

891892
If you need to add or customize converters you can use Spring Boot's
892893
`HttpMessageConverters` class:
@@ -909,6 +910,9 @@ If you need to add or customize converters you can use Spring Boot's
909910
}
910911
----
911912

913+
Any `HttpMessageConverter` bean that is present in the context will be added to the list of
914+
converters. You can also override default converters that way.
915+
912916
[[boot-features-spring-message-codes]]
913917
==== MessageCodesResolver
914918
Spring MVC has a strategy for generating error codes for rendering error messages

0 commit comments

Comments
 (0)