diff --git a/spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java index 9656be51b3..394b907ef8 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java @@ -117,11 +117,8 @@ public class FormHttpMessageConverter implements HttpMessageConverterDefault is {@code true}. + * Delegates to {@link StringHttpMessageConverter#setWriteAcceptCharset(boolean)}. */ public void setWriteAcceptCharset(boolean writeAcceptCharset) { this.stringHttpMessageConverter.setWriteAcceptCharset(writeAcceptCharset); diff --git a/spring-web/src/main/java/org/springframework/http/converter/StringHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/StringHttpMessageConverter.java index 6f393b6235..4671c59a8f 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/StringHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/StringHttpMessageConverter.java @@ -52,7 +52,7 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverter availableCharsets; - private boolean writeAcceptCharset = true; + private boolean writeAcceptCharset = false; /** @@ -76,7 +76,7 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverterDefault is {@code true}. + *

As of 5.2, by default is set to {@code false}. */ public void setWriteAcceptCharset(boolean writeAcceptCharset) { this.writeAcceptCharset = writeAcceptCharset; diff --git a/spring-web/src/test/java/org/springframework/http/converter/ObjectToStringHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/ObjectToStringHttpMessageConverterTests.java index 1c56cab10a..4c174b07bb 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/ObjectToStringHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/ObjectToStringHttpMessageConverterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -107,6 +107,7 @@ public class ObjectToStringHttpMessageConverterTests { @Test public void writeAcceptCharset() throws IOException { + this.converter.setWriteAcceptCharset(true); this.converter.write(new Date(), null, this.response); assertNotNull(this.servletResponse.getHeader("Accept-Charset")); diff --git a/spring-web/src/test/java/org/springframework/http/converter/StringHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/StringHttpMessageConverterTests.java index 4a4803b368..3a18e0dff4 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/StringHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/StringHttpMessageConverterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -79,7 +79,7 @@ public class StringHttpMessageConverterTests { assertEquals(body, this.outputMessage.getBodyAsString(StandardCharsets.ISO_8859_1)); assertEquals(new MediaType("text", "plain", StandardCharsets.ISO_8859_1), headers.getContentType()); assertEquals(body.getBytes(StandardCharsets.ISO_8859_1).length, headers.getContentLength()); - assertFalse(headers.getAcceptCharset().isEmpty()); + assertTrue(headers.getAcceptCharset().isEmpty()); } @Test @@ -91,7 +91,7 @@ public class StringHttpMessageConverterTests { assertEquals(body, this.outputMessage.getBodyAsString(StandardCharsets.UTF_8)); assertEquals(TEXT_PLAIN_UTF_8, headers.getContentType()); assertEquals(body.getBytes(StandardCharsets.UTF_8).length, headers.getContentLength()); - assertFalse(headers.getAcceptCharset().isEmpty()); + assertTrue(headers.getAcceptCharset().isEmpty()); } @Test // SPR-8867 @@ -106,7 +106,7 @@ public class StringHttpMessageConverterTests { assertEquals(body, this.outputMessage.getBodyAsString(StandardCharsets.UTF_8)); assertEquals(TEXT_PLAIN_UTF_8, headers.getContentType()); assertEquals(body.getBytes(StandardCharsets.UTF_8).length, headers.getContentLength()); - assertFalse(headers.getAcceptCharset().isEmpty()); + assertTrue(headers.getAcceptCharset().isEmpty()); } } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java index a785da4f9c..f9a937dce0 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java @@ -831,11 +831,8 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv * @param messageConverters the list to add the default message converters to */ protected final void addDefaultHttpMessageConverters(List> messageConverters) { - StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(); - stringHttpMessageConverter.setWriteAcceptCharset(false); // see SPR-7316 - messageConverters.add(new ByteArrayHttpMessageConverter()); - messageConverters.add(stringHttpMessageConverter); + messageConverters.add(new StringHttpMessageConverter()); messageConverters.add(new ResourceHttpMessageConverter()); messageConverters.add(new ResourceRegionHttpMessageConverter()); try { diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/function/support/RouterFunctionMapping.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/function/support/RouterFunctionMapping.java index a4d27bc298..47931503b7 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/function/support/RouterFunctionMapping.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/function/support/RouterFunctionMapping.java @@ -149,12 +149,8 @@ public class RouterFunctionMapping extends AbstractHandlerMapping implements Ini */ private void initMessageConverters() { List> messageConverters = new ArrayList<>(4); - messageConverters.add(new ByteArrayHttpMessageConverter()); - - StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(); - stringHttpMessageConverter.setWriteAcceptCharset(false); // see SPR-7316 - messageConverters.add(stringHttpMessageConverter); + messageConverters.add(new StringHttpMessageConverter()); try { messageConverters.add(new SourceHttpMessageConverter<>()); diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ExceptionHandlerExceptionResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ExceptionHandlerExceptionResolver.java index 6b51d04b12..74d29db10a 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ExceptionHandlerExceptionResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ExceptionHandlerExceptionResolver.java @@ -105,12 +105,9 @@ public class ExceptionHandlerExceptionResolver extends AbstractHandlerMethodExce public ExceptionHandlerExceptionResolver() { - StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(); - stringHttpMessageConverter.setWriteAcceptCharset(false); // see SPR-7316 - this.messageConverters = new ArrayList<>(); this.messageConverters.add(new ByteArrayHttpMessageConverter()); - this.messageConverters.add(stringHttpMessageConverter); + this.messageConverters.add(new StringHttpMessageConverter()); try { this.messageConverters.add(new SourceHttpMessageConverter<>()); } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java index 132641f71f..4bb3490261 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java @@ -193,12 +193,9 @@ public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter public RequestMappingHandlerAdapter() { - StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(); - stringHttpMessageConverter.setWriteAcceptCharset(false); // see SPR-7316 - this.messageConverters = new ArrayList<>(4); this.messageConverters.add(new ByteArrayHttpMessageConverter()); - this.messageConverters.add(stringHttpMessageConverter); + this.messageConverters.add(new StringHttpMessageConverter()); try { this.messageConverters.add(new SourceHttpMessageConverter<>()); }