Skip to content

Commit 89454e6

Browse files
committed
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
1 parent 2e6059f commit 89454e6

File tree

39 files changed

+144
-147
lines changed

39 files changed

+144
-147
lines changed

spring-messaging/src/main/java/org/springframework/messaging/converter/MappingJackson2MessageConverter.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.io.Writer;
2323
import java.lang.reflect.Type;
2424
import java.nio.charset.Charset;
25-
import java.nio.charset.StandardCharsets;
2625
import java.util.Arrays;
2726
import java.util.concurrent.atomic.AtomicReference;
2827

@@ -74,7 +73,7 @@ public class MappingJackson2MessageConverter extends AbstractMessageConverter {
7473
* the {@code application/json} MIME type with {@code UTF-8} character set.
7574
*/
7675
public MappingJackson2MessageConverter() {
77-
super(new MimeType("application", "json", StandardCharsets.UTF_8));
76+
super(new MimeType("application", "json"));
7877
this.objectMapper = initObjectMapper();
7978
}
8079

spring-messaging/src/test/java/org/springframework/messaging/converter/MappingJackson2MessageConverterTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public class MappingJackson2MessageConverterTests {
4848
public void defaultConstructor() {
4949
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
5050
assertThat(converter.getSupportedMimeTypes(),
51-
contains(new MimeType("application", "json", StandardCharsets.UTF_8)));
51+
contains(new MimeType("application", "json")));
5252
assertFalse(converter.getObjectMapper().getDeserializationConfig()
5353
.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
5454
}
@@ -183,7 +183,7 @@ public void toMessage() {
183183
assertTrue(actual.contains("\"array\":[\"Foo\",\"Bar\"]"));
184184
assertTrue(actual.contains("\"bool\":true"));
185185
assertTrue(actual.contains("\"bytes\":\"AQI=\""));
186-
assertEquals("Invalid content-type", new MimeType("application", "json", StandardCharsets.UTF_8),
186+
assertEquals("Invalid content-type", new MimeType("application", "json"),
187187
message.getHeaders().get(MessageHeaders.CONTENT_TYPE, MimeType.class));
188188
}
189189

spring-test/src/test/java/org/springframework/test/web/client/samples/matchers/ContentRequestMatchersIntegrationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -66,7 +66,7 @@ public void setup() {
6666

6767
@Test
6868
public void contentType() throws Exception {
69-
this.mockServer.expect(content().contentType("application/json;charset=UTF-8")).andRespond(withSuccess());
69+
this.mockServer.expect(content().contentType("application/json")).andRespond(withSuccess());
7070
executeAndVerify(new Person());
7171
}
7272

spring-test/src/test/java/org/springframework/test/web/client/samples/matchers/JsonPathRequestMatchersIntegrationTests.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -66,7 +66,7 @@ public class JsonPathRequestMatchersIntegrationTests {
6666
@Test
6767
public void exists() throws Exception {
6868
this.mockServer.expect(requestTo("/composers"))
69-
.andExpect(content().contentType("application/json;charset=UTF-8"))
69+
.andExpect(content().contentType("application/json"))
7070
.andExpect(jsonPath("$.composers[0]").exists())
7171
.andExpect(jsonPath("$.composers[1]").exists())
7272
.andExpect(jsonPath("$.composers[2]").exists())
@@ -79,7 +79,7 @@ public void exists() throws Exception {
7979
@Test
8080
public void doesNotExist() throws Exception {
8181
this.mockServer.expect(requestTo("/composers"))
82-
.andExpect(content().contentType("application/json;charset=UTF-8"))
82+
.andExpect(content().contentType("application/json"))
8383
.andExpect(jsonPath("$.composers[?(@.name == 'Edvard Grieeeeeeg')]").doesNotExist())
8484
.andExpect(jsonPath("$.composers[?(@.name == 'Robert Schuuuuuuman')]").doesNotExist())
8585
.andExpect(jsonPath("$.composers[4]").doesNotExist())
@@ -91,7 +91,7 @@ public void doesNotExist() throws Exception {
9191
@Test
9292
public void value() throws Exception {
9393
this.mockServer.expect(requestTo("/composers"))
94-
.andExpect(content().contentType("application/json;charset=UTF-8"))
94+
.andExpect(content().contentType("application/json"))
9595
.andExpect(jsonPath("$.composers[0].name").value("Johann Sebastian Bach"))
9696
.andExpect(jsonPath("$.performers[1].name").value("Yehudi Menuhin"))
9797
.andRespond(withSuccess());
@@ -102,7 +102,7 @@ public void value() throws Exception {
102102
@Test
103103
public void hamcrestMatchers() throws Exception {
104104
this.mockServer.expect(requestTo("/composers"))
105-
.andExpect(content().contentType("application/json;charset=UTF-8"))
105+
.andExpect(content().contentType("application/json"))
106106
.andExpect(jsonPath("$.composers[0].name").value(equalTo("Johann Sebastian Bach")))
107107
.andExpect(jsonPath("$.performers[1].name").value(equalTo("Yehudi Menuhin")))
108108
.andExpect(jsonPath("$.composers[0].name", startsWith("Johann")))
@@ -121,7 +121,7 @@ public void hamcrestMatchersWithParameterizedJsonPaths() throws Exception {
121121
String performerName = "$.performers[%s].name";
122122

123123
this.mockServer.expect(requestTo("/composers"))
124-
.andExpect(content().contentType("application/json;charset=UTF-8"))
124+
.andExpect(content().contentType("application/json"))
125125
.andExpect(jsonPath(composerName, 0).value(startsWith("Johann")))
126126
.andExpect(jsonPath(performerName, 0).value(endsWith("Ashkenazy")))
127127
.andExpect(jsonPath(performerName, 1).value(containsString("di Me")))
@@ -134,7 +134,7 @@ public void hamcrestMatchersWithParameterizedJsonPaths() throws Exception {
134134
@Test
135135
public void isArray() throws Exception {
136136
this.mockServer.expect(requestTo("/composers"))
137-
.andExpect(content().contentType("application/json;charset=UTF-8"))
137+
.andExpect(content().contentType("application/json"))
138138
.andExpect(jsonPath("$.composers").isArray())
139139
.andRespond(withSuccess());
140140

@@ -144,7 +144,7 @@ public void isArray() throws Exception {
144144
@Test
145145
public void isString() throws Exception {
146146
this.mockServer.expect(requestTo("/composers"))
147-
.andExpect(content().contentType("application/json;charset=UTF-8"))
147+
.andExpect(content().contentType("application/json"))
148148
.andExpect(jsonPath("$.composers[0].name").isString())
149149
.andRespond(withSuccess());
150150

@@ -154,7 +154,7 @@ public void isString() throws Exception {
154154
@Test
155155
public void isNumber() throws Exception {
156156
this.mockServer.expect(requestTo("/composers"))
157-
.andExpect(content().contentType("application/json;charset=UTF-8"))
157+
.andExpect(content().contentType("application/json"))
158158
.andExpect(jsonPath("$.composers[0].someDouble").isNumber())
159159
.andRespond(withSuccess());
160160

@@ -164,7 +164,7 @@ public void isNumber() throws Exception {
164164
@Test
165165
public void isBoolean() throws Exception {
166166
this.mockServer.expect(requestTo("/composers"))
167-
.andExpect(content().contentType("application/json;charset=UTF-8"))
167+
.andExpect(content().contentType("application/json"))
168168
.andExpect(jsonPath("$.composers[0].someBoolean").isBoolean())
169169
.andRespond(withSuccess());
170170

spring-test/src/test/java/org/springframework/test/web/reactive/server/HeaderAssertionTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -109,7 +109,7 @@ public void valueEqualsWithMultipleValues() {
109109
@Test
110110
public void valueMatches() {
111111
HttpHeaders headers = new HttpHeaders();
112-
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
112+
headers.setContentType(MediaType.parseMediaType("application/json;charset=UTF-8"));
113113
HeaderAssertions assertions = headerAssertions(headers);
114114

115115
// Success
@@ -139,7 +139,7 @@ public void valueMatcher() {
139139
@Test
140140
public void exists() {
141141
HttpHeaders headers = new HttpHeaders();
142-
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
142+
headers.setContentType(MediaType.APPLICATION_JSON);
143143
HeaderAssertions assertions = headerAssertions(headers);
144144

145145
// Success
@@ -159,7 +159,7 @@ public void exists() {
159159
@Test
160160
public void doesNotExist() {
161161
HttpHeaders headers = new HttpHeaders();
162-
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
162+
headers.setContentType(MediaType.parseMediaType("application/json;charset=UTF-8"));
163163
HeaderAssertions assertions = headerAssertions(headers);
164164

165165
// Success

spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/ErrorTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -62,7 +62,7 @@ public void serverException() {
6262
public void badRequestBeforeRequestBodyConsumed() {
6363
EntityExchangeResult<Void> result = this.client.post()
6464
.uri("/post")
65-
.contentType(MediaType.APPLICATION_JSON_UTF8)
65+
.contentType(MediaType.APPLICATION_JSON)
6666
.syncBody(new Person("Dan"))
6767
.exchange()
6868
.expectStatus().isBadRequest()

spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/JsonContentTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -48,7 +48,7 @@ public class JsonContentTests {
4848
@Test
4949
public void jsonContent() {
5050
this.client.get().uri("/persons")
51-
.accept(MediaType.APPLICATION_JSON_UTF8)
51+
.accept(MediaType.APPLICATION_JSON)
5252
.exchange()
5353
.expectStatus().isOk()
5454
.expectBody().json("[{\"name\":\"Jane\"},{\"name\":\"Jason\"},{\"name\":\"John\"}]");
@@ -57,7 +57,7 @@ public void jsonContent() {
5757
@Test
5858
public void jsonPathIsEqualTo() {
5959
this.client.get().uri("/persons")
60-
.accept(MediaType.APPLICATION_JSON_UTF8)
60+
.accept(MediaType.APPLICATION_JSON)
6161
.exchange()
6262
.expectStatus().isOk()
6363
.expectBody()
@@ -69,7 +69,7 @@ public void jsonPathIsEqualTo() {
6969
@Test
7070
public void jsonPathMatches() {
7171
this.client.get().uri("/persons/John")
72-
.accept(MediaType.APPLICATION_JSON_UTF8)
72+
.accept(MediaType.APPLICATION_JSON)
7373
.exchange()
7474
.expectStatus().isOk()
7575
.expectBody()
@@ -79,7 +79,7 @@ public void jsonPathMatches() {
7979
@Test
8080
public void postJsonContent() {
8181
this.client.post().uri("/persons")
82-
.contentType(MediaType.APPLICATION_JSON_UTF8)
82+
.contentType(MediaType.APPLICATION_JSON)
8383
.syncBody("{\"name\":\"John\"}")
8484
.exchange()
8585
.expectStatus().isCreated()

spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/ResponseEntityTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void entity() {
6363
this.client.get().uri("/John")
6464
.exchange()
6565
.expectStatus().isOk()
66-
.expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8)
66+
.expectHeader().contentType(MediaType.APPLICATION_JSON)
6767
.expectBody(Person.class).isEqualTo(new Person("John"));
6868
}
6969

@@ -72,7 +72,7 @@ public void entityMatcher() {
7272
this.client.get().uri("/John")
7373
.exchange()
7474
.expectStatus().isOk()
75-
.expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8)
75+
.expectHeader().contentType(MediaType.APPLICATION_JSON)
7676
.expectBody(Person.class).value(Person::getName, startsWith("Joh"));
7777
}
7878

@@ -81,7 +81,7 @@ public void entityWithConsumer() {
8181
this.client.get().uri("/John")
8282
.exchange()
8383
.expectStatus().isOk()
84-
.expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8)
84+
.expectHeader().contentType(MediaType.APPLICATION_JSON)
8585
.expectBody(Person.class)
8686
.consumeWith(result -> assertEquals(new Person("John"), result.getResponseBody()));
8787
}
@@ -95,7 +95,7 @@ public void entityList() {
9595
this.client.get()
9696
.exchange()
9797
.expectStatus().isOk()
98-
.expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8)
98+
.expectHeader().contentType(MediaType.APPLICATION_JSON)
9999
.expectBodyList(Person.class).isEqualTo(expected);
100100
}
101101

@@ -105,7 +105,7 @@ public void entityListWithConsumer() {
105105
this.client.get()
106106
.exchange()
107107
.expectStatus().isOk()
108-
.expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8)
108+
.expectHeader().contentType(MediaType.APPLICATION_JSON)
109109
.expectBodyList(Person.class).value(people -> {
110110
MatcherAssert.assertThat(people, hasItem(new Person("Jason")));
111111
});

spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/AsyncTests.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -70,7 +70,7 @@ public void callable() throws Exception {
7070

7171
this.mockMvc.perform(asyncDispatch(mvcResult))
7272
.andExpect(status().isOk())
73-
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
73+
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
7474
.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
7575
}
7676

@@ -98,7 +98,7 @@ public void streamingJson() throws Exception {
9898
.andExpect(request().asyncStarted())
9999
.andDo(MvcResult::getAsyncResult)
100100
.andExpect(status().isOk())
101-
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
101+
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
102102
.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.5}"));
103103
}
104104

@@ -112,7 +112,7 @@ public void deferredResult() throws Exception {
112112

113113
this.mockMvc.perform(asyncDispatch(mvcResult))
114114
.andExpect(status().isOk())
115-
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
115+
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
116116
.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
117117
}
118118

@@ -125,7 +125,7 @@ public void deferredResultWithImmediateValue() throws Exception {
125125

126126
this.mockMvc.perform(asyncDispatch(mvcResult))
127127
.andExpect(status().isOk())
128-
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
128+
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
129129
.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
130130
}
131131

@@ -150,7 +150,7 @@ public void listenableFuture() throws Exception {
150150

151151
this.mockMvc.perform(asyncDispatch(mvcResult))
152152
.andExpect(status().isOk())
153-
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
153+
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
154154
.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
155155
}
156156

@@ -162,7 +162,7 @@ public void completableFutureWithImmediateValue() throws Exception {
162162

163163
this.mockMvc.perform(asyncDispatch(mvcResult))
164164
.andExpect(status().isOk())
165-
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
165+
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
166166
.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
167167
}
168168

@@ -183,7 +183,7 @@ public void printAsyncResult() throws Exception {
183183
this.mockMvc.perform(asyncDispatch(mvcResult))
184184
.andDo(print(writer))
185185
.andExpect(status().isOk())
186-
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
186+
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
187187
.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
188188

189189
assertTrue(writer.toString().contains("Async started = false"));
@@ -224,7 +224,7 @@ public StreamingResponseBody getStreamingSlow() {
224224

225225
@RequestMapping(params = "streamingJson")
226226
public ResponseEntity<StreamingResponseBody> getStreamingJson() {
227-
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON_UTF8)
227+
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON)
228228
.body(os -> os.write("{\"name\":\"Joe\",\"someDouble\":0.5}".getBytes(StandardCharsets.UTF_8)));
229229
}
230230

spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/RequestParameterTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -41,7 +41,7 @@ public void queryParameter() throws Exception {
4141
standaloneSetup(new PersonController()).build()
4242
.perform(get("/search?name=George").accept(MediaType.APPLICATION_JSON))
4343
.andExpect(status().isOk())
44-
.andExpect(content().contentType("application/json;charset=UTF-8"))
44+
.andExpect(content().contentType("application/json"))
4545
.andExpect(jsonPath("$.name").value("George"));
4646
}
4747

spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/ResponseBodyTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void json() throws Exception {
4141
standaloneSetup(new PersonController()).build()
4242
.perform(get("/person/Lee").accept(MediaType.APPLICATION_JSON))
4343
.andExpect(status().isOk())
44-
.andExpect(content().contentType("application/json;charset=UTF-8"))
44+
.andExpect(content().contentType("application/json"))
4545
.andExpect(jsonPath("$.name").value("Lee"));
4646
}
4747

spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/JsonPathAssertionTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -51,7 +51,7 @@ public void setup() {
5151
this.mockMvc = standaloneSetup(new MusicController())
5252
.defaultRequest(get("/").accept(MediaType.APPLICATION_JSON))
5353
.alwaysExpect(status().isOk())
54-
.alwaysExpect(content().contentType("application/json;charset=UTF-8"))
54+
.alwaysExpect(content().contentType("application/json"))
5555
.build();
5656
}
5757

0 commit comments

Comments
 (0)