diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/HttpMessageConvertersAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/HttpMessageConvertersAutoConfiguration.java index d4a2171505..9e03aa64aa 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/HttpMessageConvertersAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/HttpMessageConvertersAutoConfiguration.java @@ -16,6 +16,7 @@ package org.springframework.boot.autoconfigure.web; +import java.nio.charset.Charset; import java.util.Collections; import java.util.List; @@ -28,6 +29,7 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.http.converter.json.GsonHttpMessageConverter; import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; @@ -47,11 +49,14 @@ import com.google.gson.Gson; * @author David Liu * @author Andy Wilkinson * @author Sebastien Deleuze + * @author Stephane Nicoll */ @Configuration @ConditionalOnClass(HttpMessageConverter.class) public class HttpMessageConvertersAutoConfiguration { + private static final Charset UTF_8 = Charset.forName("UTF-8"); + @Autowired(required = false) private final List> converters = Collections.emptyList(); @@ -118,4 +123,16 @@ public class HttpMessageConvertersAutoConfiguration { } + @Configuration + @ConditionalOnClass(StringHttpMessageConverter.class) + protected static class StringHttpMessageConverterConfiguration { + + @Bean + @ConditionalOnMissingBean + public StringHttpMessageConverter stringHttpMessageConverter() { + return new StringHttpMessageConverter(UTF_8); + } + + } + } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/HttpMessageConvertersAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/HttpMessageConvertersAutoConfigurationTests.java index 99cddea47f..5ad7080e97 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/HttpMessageConvertersAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/HttpMessageConvertersAutoConfigurationTests.java @@ -24,6 +24,7 @@ import org.junit.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.http.converter.json.GsonHttpMessageConverter; import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; @@ -46,7 +47,7 @@ import static org.junit.Assert.assertTrue; */ public class HttpMessageConvertersAutoConfigurationTests { - private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();; + private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); @After public void close() { @@ -134,6 +135,26 @@ public class HttpMessageConvertersAutoConfigurationTests { assertConverterBeanRegisteredWithHttpMessageConverters(GsonHttpMessageConverter.class); } + @Test + public void defaultStringConverter() throws Exception { + this.context.register(HttpMessageConvertersAutoConfiguration.class); + this.context.refresh(); + assertConverterBeanExists(StringHttpMessageConverter.class, + "stringHttpMessageConverter"); + assertConverterBeanRegisteredWithHttpMessageConverters(StringHttpMessageConverter.class); + } + + @Test + public void customStringConverter() throws Exception { + this.context.register(StringConverterConfig.class, + HttpMessageConvertersAutoConfiguration.class); + this.context.refresh(); + assertConverterBeanExists(StringHttpMessageConverter.class, + "customStringMessageConverter"); + + assertConverterBeanRegisteredWithHttpMessageConverters(StringHttpMessageConverter.class); + } + private void assertConverterBeanExists(Class type, String beanName) { assertEquals(1, this.context.getBeansOfType(type).size()); List beanNames = Arrays.asList(this.context.getBeanDefinitionNames()); @@ -202,4 +223,13 @@ public class HttpMessageConvertersAutoConfigurationTests { } } + @Configuration + protected static class StringConverterConfig { + + @Bean + public StringHttpMessageConverter customStringMessageConverter() { + return new StringHttpMessageConverter(); + } + } + } diff --git a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index c2cf92b37d..7464e86b3a 100644 --- a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -886,7 +886,8 @@ formatters, view controllers etc.) you can add your own `@Bean` of type Spring MVC uses the `HttpMessageConverter` interface to convert HTTP requests and responses. Sensible defaults are included out of the box, for example Objects can be automatically converted to JSON (using the Jackson library) or XML (using the Jackson -XML extension if available, else using JAXB). +XML extension if available, else using JAXB). Strings are encoded using `UTF-8` by +default. If you need to add or customize converters you can use Spring Boot's `HttpMessageConverters` class: @@ -909,6 +910,9 @@ If you need to add or customize converters you can use Spring Boot's } ---- +Any `HttpMessageConverter` bean that is present in the context will be added to the list of +converters. You can also override default converters that way. + [[boot-features-spring-message-codes]] ==== MessageCodesResolver Spring MVC has a strategy for generating error codes for rendering error messages