Browse Source

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
pull/23041/head
Sebastien Deleuze 6 years ago
parent
commit
bc205e0dbf
  1. 4
      spring-web/src/main/java/org/springframework/http/converter/StringHttpMessageConverter.java
  2. 4
      spring-web/src/main/java/org/springframework/http/converter/json/AbstractJackson2HttpMessageConverter.java
  3. 18
      spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestResponseBodyMethodProcessorTests.java

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

@ -130,10 +130,6 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverter<Str @@ -130,10 +130,6 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverter<Str
if (contentType != null && contentType.getCharset() != null) {
return contentType.getCharset();
}
else if (contentType != null && contentType.isCompatibleWith(MediaType.APPLICATION_JSON)) {
// Matching to AbstractJackson2HttpMessageConverter#DEFAULT_CHARSET
return StandardCharsets.UTF_8;
}
else {
Charset charset = getDefaultCharset();
Assert.state(charset != null, "No default charset");

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

@ -48,7 +48,6 @@ import org.springframework.http.converter.HttpMessageConversionException; @@ -48,7 +48,6 @@ import org.springframework.http.converter.HttpMessageConversionException;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.http.server.ServletServerHttpResponse;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.TypeUtils;
@ -251,9 +250,6 @@ public abstract class AbstractJackson2HttpMessageConverter extends AbstractGener @@ -251,9 +250,6 @@ public abstract class AbstractJackson2HttpMessageConverter extends AbstractGener
protected void writeInternal(Object object, @Nullable Type type, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
if (getDefaultCharset() == null && outputMessage instanceof ServletServerHttpResponse) {
((ServletServerHttpResponse)outputMessage).getServletResponse().setCharacterEncoding("UTF-8");
}
MediaType contentType = outputMessage.getHeaders().getContentType();
JsonEncoding encoding = getJsonEncoding(contentType);
JsonGenerator generator = this.objectMapper.getFactory().createGenerator(outputMessage.getBody(), encoding);

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

@ -293,7 +293,7 @@ public class RequestResponseBodyMethodProcessorTests { @@ -293,7 +293,7 @@ public class RequestResponseBodyMethodProcessorTests {
processor.writeWithMessageConverters("Foo", returnTypeString, request);
assertThat(servletResponse.getHeader("Content-Type")).isEqualTo("application/json;charset=UTF-8");
assertThat(servletResponse.getHeader("Content-Type")).isEqualTo(MediaType.APPLICATION_JSON_VALUE);
}
@Test
@ -676,22 +676,6 @@ public class RequestResponseBodyMethodProcessorTests { @@ -676,22 +676,6 @@ public class RequestResponseBodyMethodProcessorTests {
assertThat(content.contains("\"name\":\"bar\"")).isTrue();
}
@Test // SPR-13631
public void defaultCharset() throws Exception {
Method method = JacksonController.class.getMethod("defaultCharset");
HandlerMethod handlerMethod = new HandlerMethod(new JacksonController(), method);
MethodParameter methodReturnType = handlerMethod.getReturnType();
List<HttpMessageConverter<?>> converters = new ArrayList<>();
converters.add(new MappingJackson2HttpMessageConverter());
RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);
Object returnValue = new JacksonController().defaultCharset();
processor.handleReturnValue(returnValue, methodReturnType, this.container, this.request);
assertThat(this.servletResponse.getCharacterEncoding()).isEqualTo("UTF-8");
}
@Test // SPR-14520
public void resolveArgumentTypeVariableWithGenericInterface() throws Exception {
this.servletRequest.setContent("\"foo\"".getBytes("UTF-8"));

Loading…
Cancel
Save