Skip to content

Commit bc205e0

Browse files
committed
Remove charset parameter from Spring MVC JSON content type
This change is a follow-up to the ones done as part of gh-22788. Closes gh-22954
1 parent 30944ed commit bc205e0

File tree

3 files changed

+1
-25
lines changed

3 files changed

+1
-25
lines changed

spring-web/src/main/java/org/springframework/http/converter/StringHttpMessageConverter.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,6 @@ private Charset getContentTypeCharset(@Nullable MediaType contentType) {
130130
if (contentType != null && contentType.getCharset() != null) {
131131
return contentType.getCharset();
132132
}
133-
else if (contentType != null && contentType.isCompatibleWith(MediaType.APPLICATION_JSON)) {
134-
// Matching to AbstractJackson2HttpMessageConverter#DEFAULT_CHARSET
135-
return StandardCharsets.UTF_8;
136-
}
137133
else {
138134
Charset charset = getDefaultCharset();
139135
Assert.state(charset != null, "No default charset");

spring-web/src/main/java/org/springframework/http/converter/json/AbstractJackson2HttpMessageConverter.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
import org.springframework.http.converter.HttpMessageConverter;
4949
import org.springframework.http.converter.HttpMessageNotReadableException;
5050
import org.springframework.http.converter.HttpMessageNotWritableException;
51-
import org.springframework.http.server.ServletServerHttpResponse;
5251
import org.springframework.lang.Nullable;
5352
import org.springframework.util.Assert;
5453
import org.springframework.util.TypeUtils;
@@ -251,9 +250,6 @@ private Object readJavaType(JavaType javaType, HttpInputMessage inputMessage) th
251250
protected void writeInternal(Object object, @Nullable Type type, HttpOutputMessage outputMessage)
252251
throws IOException, HttpMessageNotWritableException {
253252

254-
if (getDefaultCharset() == null && outputMessage instanceof ServletServerHttpResponse) {
255-
((ServletServerHttpResponse)outputMessage).getServletResponse().setCharacterEncoding("UTF-8");
256-
}
257253
MediaType contentType = outputMessage.getHeaders().getContentType();
258254
JsonEncoding encoding = getJsonEncoding(contentType);
259255
JsonGenerator generator = this.objectMapper.getFactory().createGenerator(outputMessage.getBody(), encoding);

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestResponseBodyMethodProcessorTests.java

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ public void handleReturnValueSortByQuality() throws Exception {
293293

294294
processor.writeWithMessageConverters("Foo", returnTypeString, request);
295295

296-
assertThat(servletResponse.getHeader("Content-Type")).isEqualTo("application/json;charset=UTF-8");
296+
assertThat(servletResponse.getHeader("Content-Type")).isEqualTo(MediaType.APPLICATION_JSON_VALUE);
297297
}
298298

299299
@Test
@@ -676,22 +676,6 @@ public void jacksonSubTypeList() throws Exception {
676676
assertThat(content.contains("\"name\":\"bar\"")).isTrue();
677677
}
678678

679-
@Test // SPR-13631
680-
public void defaultCharset() throws Exception {
681-
Method method = JacksonController.class.getMethod("defaultCharset");
682-
HandlerMethod handlerMethod = new HandlerMethod(new JacksonController(), method);
683-
MethodParameter methodReturnType = handlerMethod.getReturnType();
684-
685-
List<HttpMessageConverter<?>> converters = new ArrayList<>();
686-
converters.add(new MappingJackson2HttpMessageConverter());
687-
RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);
688-
689-
Object returnValue = new JacksonController().defaultCharset();
690-
processor.handleReturnValue(returnValue, methodReturnType, this.container, this.request);
691-
692-
assertThat(this.servletResponse.getCharacterEncoding()).isEqualTo("UTF-8");
693-
}
694-
695679
@Test // SPR-14520
696680
public void resolveArgumentTypeVariableWithGenericInterface() throws Exception {
697681
this.servletRequest.setContent("\"foo\"".getBytes("UTF-8"));

0 commit comments

Comments
 (0)