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
This commit is contained in:
parent
8be97fa47d
commit
cbd3c39640
|
|
@ -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<HttpMessageConverter<?>> 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<String> 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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue