Deprecate MediaType.APPLICATION_JSON_UTF8

This commit deprecates MediaType.APPLICATION_JSON_UTF8 and
MediaType.APPLICATION_PROBLEM_JSON_UTF8 in favor of
MediaType.APPLICATION_JSON and MediaType.APPLICATION_PROBLEM_JSON since
UTF-8 encoding is now handled correctly by most browsers
(related bug has been fixed in Chrome since September 2017).

MediaType.APPLICATION_JSON is now used as the default JSON content type.

Closes gh-22788
This commit is contained in:
Sebastien Deleuze
2019-04-30 16:05:09 +02:00
parent 2e6059f6b0
commit 89454e69c3
39 changed files with 144 additions and 147 deletions

View File

@@ -48,11 +48,11 @@ inline fun <reified T : Any> ServerResponse.BodyBuilder.bodyToServerSentEvents(p
contentType(MediaType.TEXT_EVENT_STREAM).body(publisher, object : ParameterizedTypeReference<T>() {})
/**
* Shortcut for setting [MediaType.APPLICATION_JSON_UTF8] `Content-Type` header.
* Shortcut for setting [MediaType.APPLICATION_JSON] `Content-Type` header.
* @author Sebastien Deleuze
* @since 5.1
*/
fun ServerResponse.BodyBuilder.json() = contentType(MediaType.APPLICATION_JSON_UTF8)
fun ServerResponse.BodyBuilder.json() = contentType(MediaType.APPLICATION_JSON)
/**
* Shortcut for setting [MediaType.APPLICATION_XML] `Content-Type` header.

View File

@@ -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.
@@ -267,7 +267,7 @@ public class WebFluxConfigurationSupportTests {
List<View> views = handler.getDefaultViews();
assertEquals(1, views.size());
MimeType type = MimeTypeUtils.parseMimeType("application/json;charset=UTF-8");
MimeType type = MimeTypeUtils.parseMimeType("application/json");
assertEquals(type, views.get(0).getSupportedMediaTypes().get(0));
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 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.
@@ -33,7 +33,7 @@ import org.springframework.web.reactive.accept.RequestedContentTypeResolver;
import static org.junit.Assert.assertEquals;
import static org.springframework.http.MediaType.ALL;
import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.http.MediaType.APPLICATION_OCTET_STREAM;
import static org.springframework.http.MediaType.IMAGE_GIF;
import static org.springframework.http.MediaType.IMAGE_JPEG;
@@ -76,10 +76,10 @@ public class HandlerResultHandlerTests {
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/path")
.header("Accept", "text/plain; q=0.5, application/json"));
List<MediaType> mediaTypes = Arrays.asList(TEXT_PLAIN, APPLICATION_JSON_UTF8);
List<MediaType> mediaTypes = Arrays.asList(TEXT_PLAIN, APPLICATION_JSON);
MediaType actual = this.resultHandler.selectMediaType(exchange, () -> mediaTypes);
assertEquals(APPLICATION_JSON_UTF8, actual);
assertEquals(APPLICATION_JSON, actual);
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 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.
@@ -56,39 +56,39 @@ public class JacksonHintsIntegrationTests extends AbstractRequestMappingIntegrat
@Test
public void jsonViewResponse() throws Exception {
String expected = "{\"withView1\":\"with\"}";
assertEquals(expected, performGet("/response/raw", MediaType.APPLICATION_JSON_UTF8, String.class).getBody());
assertEquals(expected, performGet("/response/raw", MediaType.APPLICATION_JSON, String.class).getBody());
}
@Test
public void jsonViewWithMonoResponse() throws Exception {
String expected = "{\"withView1\":\"with\"}";
assertEquals(expected, performGet("/response/mono", MediaType.APPLICATION_JSON_UTF8, String.class).getBody());
assertEquals(expected, performGet("/response/mono", MediaType.APPLICATION_JSON, String.class).getBody());
}
@Test // SPR-16098
public void jsonViewWithMonoResponseEntity() throws Exception {
String expected = "{\"withView1\":\"with\"}";
assertEquals(expected, performGet("/response/entity", MediaType.APPLICATION_JSON_UTF8, String.class).getBody());
assertEquals(expected, performGet("/response/entity", MediaType.APPLICATION_JSON, String.class).getBody());
}
@Test
public void jsonViewWithFluxResponse() throws Exception {
String expected = "[{\"withView1\":\"with\"},{\"withView1\":\"with\"}]";
assertEquals(expected, performGet("/response/flux", MediaType.APPLICATION_JSON_UTF8, String.class).getBody());
assertEquals(expected, performGet("/response/flux", MediaType.APPLICATION_JSON, String.class).getBody());
}
@Test
public void jsonViewWithRequest() throws Exception {
String expected = "{\"withView1\":\"with\",\"withView2\":null,\"withoutView\":null}";
assertEquals(expected, performPost("/request/raw", MediaType.APPLICATION_JSON,
new JacksonViewBean("with", "with", "without"), MediaType.APPLICATION_JSON_UTF8, String.class).getBody());
new JacksonViewBean("with", "with", "without"), MediaType.APPLICATION_JSON, String.class).getBody());
}
@Test
public void jsonViewWithMonoRequest() throws Exception {
String expected = "{\"withView1\":\"with\",\"withView2\":null,\"withoutView\":null}";
assertEquals(expected, performPost("/request/mono", MediaType.APPLICATION_JSON,
new JacksonViewBean("with", "with", "without"), MediaType.APPLICATION_JSON_UTF8, String.class).getBody());
new JacksonViewBean("with", "with", "without"), MediaType.APPLICATION_JSON, String.class).getBody());
}
@Test // SPR-16098
@@ -96,7 +96,7 @@ public class JacksonHintsIntegrationTests extends AbstractRequestMappingIntegrat
String expected = "{\"withView1\":\"with\",\"withView2\":null,\"withoutView\":null}";
assertEquals(expected, performPost("/request/entity/mono", MediaType.APPLICATION_JSON,
new JacksonViewBean("with", "with", "without"),
MediaType.APPLICATION_JSON_UTF8, String.class).getBody());
MediaType.APPLICATION_JSON, String.class).getBody());
}
@Test // SPR-16098
@@ -107,7 +107,7 @@ public class JacksonHintsIntegrationTests extends AbstractRequestMappingIntegrat
assertEquals(expected, performPost("/request/entity/flux", MediaType.APPLICATION_JSON,
Arrays.asList(new JacksonViewBean("with", "with", "without"),
new JacksonViewBean("with", "with", "without")),
MediaType.APPLICATION_JSON_UTF8, String.class).getBody());
MediaType.APPLICATION_JSON, String.class).getBody());
}
@Test
@@ -119,7 +119,7 @@ public class JacksonHintsIntegrationTests extends AbstractRequestMappingIntegrat
new JacksonViewBean("with", "with", "without"),
new JacksonViewBean("with", "with", "without"));
assertEquals(expected, performPost("/request/flux", MediaType.APPLICATION_JSON, beans,
MediaType.APPLICATION_JSON_UTF8, String.class).getBody());
MediaType.APPLICATION_JSON, String.class).getBody());
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 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.
@@ -41,6 +41,7 @@ import org.springframework.core.codec.ByteBufferEncoder;
import org.springframework.core.codec.CharSequenceEncoder;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.http.codec.EncoderHttpMessageWriter;
import org.springframework.http.codec.HttpMessageWriter;
import org.springframework.http.codec.ResourceHttpMessageWriter;
@@ -56,7 +57,6 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.springframework.core.io.buffer.support.DataBufferTestUtils.dumpString;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8;
import static org.springframework.web.method.ResolvableMethod.on;
import static org.springframework.web.reactive.HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE;
@@ -108,7 +108,7 @@ public class MessageWriterResultHandlerTests {
MethodParameter type = on(TestController.class).resolveReturnType(String.class);
this.resultHandler.writeBody(body, type, this.exchange).block(Duration.ofSeconds(5));
assertEquals(APPLICATION_JSON_UTF8, this.exchange.getResponse().getHeaders().getContentType());
assertEquals(MediaType.parseMediaType("application/json;charset=UTF-8"), this.exchange.getResponse().getHeaders().getContentType());
}
@Test
@@ -155,7 +155,7 @@ public class MessageWriterResultHandlerTests {
List<ParentClass> body = Arrays.asList(new Foo("foo"), new Bar("bar"));
this.resultHandler.writeBody(body, returnType, this.exchange).block(Duration.ofSeconds(5));
assertEquals(APPLICATION_JSON_UTF8, this.exchange.getResponse().getHeaders().getContentType());
assertEquals(APPLICATION_JSON, this.exchange.getResponse().getHeaders().getContentType());
assertResponseBody("[{\"type\":\"foo\",\"parentProperty\":\"foo\"}," +
"{\"type\":\"bar\",\"parentProperty\":\"bar\"}]");
}
@@ -166,7 +166,7 @@ public class MessageWriterResultHandlerTests {
MethodParameter type = on(TestController.class).resolveReturnType(Identifiable.class);
this.resultHandler.writeBody(body, type, this.exchange).block(Duration.ofSeconds(5));
assertEquals(APPLICATION_JSON_UTF8, this.exchange.getResponse().getHeaders().getContentType());
assertEquals(APPLICATION_JSON, this.exchange.getResponse().getHeaders().getContentType());
assertResponseBody("{\"id\":123,\"name\":\"foo\"}");
}
@@ -178,7 +178,7 @@ public class MessageWriterResultHandlerTests {
List<SimpleBean> body = Arrays.asList(new SimpleBean(123L, "foo"), new SimpleBean(456L, "bar"));
this.resultHandler.writeBody(body, returnType, this.exchange).block(Duration.ofSeconds(5));
assertEquals(APPLICATION_JSON_UTF8, this.exchange.getResponse().getHeaders().getContentType());
assertEquals(APPLICATION_JSON, this.exchange.getResponse().getHeaders().getContentType());
assertResponseBody("[{\"id\":123,\"name\":\"foo\"},{\"id\":456,\"name\":\"bar\"}]");
}

View File

@@ -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.
@@ -99,7 +99,7 @@ public class RequestMappingExceptionHandlingIntegrationTests extends AbstractReq
}
catch (HttpStatusCodeException ex) {
assertEquals(500, ex.getRawStatusCode());
assertEquals("application/problem+json;charset=UTF-8", ex.getResponseHeaders().getContentType().toString());
assertEquals("application/problem+json", ex.getResponseHeaders().getContentType().toString());
assertEquals("{\"reason\":\"error\"}", ex.getResponseBodyAsString());
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 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.
@@ -59,8 +59,8 @@ public class HttpMessageWriterViewTests {
@Test
public void supportedMediaTypes() throws Exception {
assertEquals(Arrays.asList(
MediaType.parseMediaType("application/json;charset=UTF-8"),
MediaType.parseMediaType("application/*+json;charset=UTF-8")),
MediaType.APPLICATION_JSON,
MediaType.parseMediaType("application/*+json")),
this.view.getSupportedMediaTypes());
}

View File

@@ -26,9 +26,7 @@ import org.junit.Assert.assertEquals
import org.junit.Test
import org.reactivestreams.Publisher
import org.springframework.core.ParameterizedTypeReference
import org.springframework.http.MediaType
import org.springframework.http.MediaType.*
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
/**
@@ -58,7 +56,7 @@ class ServerResponseExtensionsTests {
@Test
fun `BodyBuilder#json`() {
bodyBuilder.json()
verify { bodyBuilder.contentType(APPLICATION_JSON_UTF8) }
verify { bodyBuilder.contentType(APPLICATION_JSON) }
}
@Test