diff --git a/spring-messaging/src/main/java/org/springframework/messaging/converter/MappingJackson2MessageConverter.java b/spring-messaging/src/main/java/org/springframework/messaging/converter/MappingJackson2MessageConverter.java index d193a4aaf9..abf98391db 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/converter/MappingJackson2MessageConverter.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/converter/MappingJackson2MessageConverter.java @@ -22,7 +22,6 @@ import java.io.StringWriter; import java.io.Writer; import java.lang.reflect.Type; import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.concurrent.atomic.AtomicReference; @@ -74,7 +73,7 @@ public class MappingJackson2MessageConverter extends AbstractMessageConverter { * the {@code application/json} MIME type with {@code UTF-8} character set. */ public MappingJackson2MessageConverter() { - super(new MimeType("application", "json", StandardCharsets.UTF_8)); + super(new MimeType("application", "json")); this.objectMapper = initObjectMapper(); } diff --git a/spring-messaging/src/test/java/org/springframework/messaging/converter/MappingJackson2MessageConverterTests.java b/spring-messaging/src/test/java/org/springframework/messaging/converter/MappingJackson2MessageConverterTests.java index db8dc11045..c93d17ba8b 100644 --- a/spring-messaging/src/test/java/org/springframework/messaging/converter/MappingJackson2MessageConverterTests.java +++ b/spring-messaging/src/test/java/org/springframework/messaging/converter/MappingJackson2MessageConverterTests.java @@ -48,7 +48,7 @@ public class MappingJackson2MessageConverterTests { public void defaultConstructor() { MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter(); assertThat(converter.getSupportedMimeTypes(), - contains(new MimeType("application", "json", StandardCharsets.UTF_8))); + contains(new MimeType("application", "json"))); assertFalse(converter.getObjectMapper().getDeserializationConfig() .isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)); } @@ -183,7 +183,7 @@ public class MappingJackson2MessageConverterTests { assertTrue(actual.contains("\"array\":[\"Foo\",\"Bar\"]")); assertTrue(actual.contains("\"bool\":true")); assertTrue(actual.contains("\"bytes\":\"AQI=\"")); - assertEquals("Invalid content-type", new MimeType("application", "json", StandardCharsets.UTF_8), + assertEquals("Invalid content-type", new MimeType("application", "json"), message.getHeaders().get(MessageHeaders.CONTENT_TYPE, MimeType.class)); } diff --git a/spring-test/src/test/java/org/springframework/test/web/client/samples/matchers/ContentRequestMatchersIntegrationTests.java b/spring-test/src/test/java/org/springframework/test/web/client/samples/matchers/ContentRequestMatchersIntegrationTests.java index c20e81696c..de3cfddeeb 100644 --- a/spring-test/src/test/java/org/springframework/test/web/client/samples/matchers/ContentRequestMatchersIntegrationTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/client/samples/matchers/ContentRequestMatchersIntegrationTests.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. @@ -66,7 +66,7 @@ public class ContentRequestMatchersIntegrationTests { @Test public void contentType() throws Exception { - this.mockServer.expect(content().contentType("application/json;charset=UTF-8")).andRespond(withSuccess()); + this.mockServer.expect(content().contentType("application/json")).andRespond(withSuccess()); executeAndVerify(new Person()); } diff --git a/spring-test/src/test/java/org/springframework/test/web/client/samples/matchers/JsonPathRequestMatchersIntegrationTests.java b/spring-test/src/test/java/org/springframework/test/web/client/samples/matchers/JsonPathRequestMatchersIntegrationTests.java index 6d47153572..ff5fd6901f 100644 --- a/spring-test/src/test/java/org/springframework/test/web/client/samples/matchers/JsonPathRequestMatchersIntegrationTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/client/samples/matchers/JsonPathRequestMatchersIntegrationTests.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. @@ -66,7 +66,7 @@ public class JsonPathRequestMatchersIntegrationTests { @Test public void exists() throws Exception { this.mockServer.expect(requestTo("/composers")) - .andExpect(content().contentType("application/json;charset=UTF-8")) + .andExpect(content().contentType("application/json")) .andExpect(jsonPath("$.composers[0]").exists()) .andExpect(jsonPath("$.composers[1]").exists()) .andExpect(jsonPath("$.composers[2]").exists()) @@ -79,7 +79,7 @@ public class JsonPathRequestMatchersIntegrationTests { @Test public void doesNotExist() throws Exception { this.mockServer.expect(requestTo("/composers")) - .andExpect(content().contentType("application/json;charset=UTF-8")) + .andExpect(content().contentType("application/json")) .andExpect(jsonPath("$.composers[?(@.name == 'Edvard Grieeeeeeg')]").doesNotExist()) .andExpect(jsonPath("$.composers[?(@.name == 'Robert Schuuuuuuman')]").doesNotExist()) .andExpect(jsonPath("$.composers[4]").doesNotExist()) @@ -91,7 +91,7 @@ public class JsonPathRequestMatchersIntegrationTests { @Test public void value() throws Exception { this.mockServer.expect(requestTo("/composers")) - .andExpect(content().contentType("application/json;charset=UTF-8")) + .andExpect(content().contentType("application/json")) .andExpect(jsonPath("$.composers[0].name").value("Johann Sebastian Bach")) .andExpect(jsonPath("$.performers[1].name").value("Yehudi Menuhin")) .andRespond(withSuccess()); @@ -102,7 +102,7 @@ public class JsonPathRequestMatchersIntegrationTests { @Test public void hamcrestMatchers() throws Exception { this.mockServer.expect(requestTo("/composers")) - .andExpect(content().contentType("application/json;charset=UTF-8")) + .andExpect(content().contentType("application/json")) .andExpect(jsonPath("$.composers[0].name").value(equalTo("Johann Sebastian Bach"))) .andExpect(jsonPath("$.performers[1].name").value(equalTo("Yehudi Menuhin"))) .andExpect(jsonPath("$.composers[0].name", startsWith("Johann"))) @@ -121,7 +121,7 @@ public class JsonPathRequestMatchersIntegrationTests { String performerName = "$.performers[%s].name"; this.mockServer.expect(requestTo("/composers")) - .andExpect(content().contentType("application/json;charset=UTF-8")) + .andExpect(content().contentType("application/json")) .andExpect(jsonPath(composerName, 0).value(startsWith("Johann"))) .andExpect(jsonPath(performerName, 0).value(endsWith("Ashkenazy"))) .andExpect(jsonPath(performerName, 1).value(containsString("di Me"))) @@ -134,7 +134,7 @@ public class JsonPathRequestMatchersIntegrationTests { @Test public void isArray() throws Exception { this.mockServer.expect(requestTo("/composers")) - .andExpect(content().contentType("application/json;charset=UTF-8")) + .andExpect(content().contentType("application/json")) .andExpect(jsonPath("$.composers").isArray()) .andRespond(withSuccess()); @@ -144,7 +144,7 @@ public class JsonPathRequestMatchersIntegrationTests { @Test public void isString() throws Exception { this.mockServer.expect(requestTo("/composers")) - .andExpect(content().contentType("application/json;charset=UTF-8")) + .andExpect(content().contentType("application/json")) .andExpect(jsonPath("$.composers[0].name").isString()) .andRespond(withSuccess()); @@ -154,7 +154,7 @@ public class JsonPathRequestMatchersIntegrationTests { @Test public void isNumber() throws Exception { this.mockServer.expect(requestTo("/composers")) - .andExpect(content().contentType("application/json;charset=UTF-8")) + .andExpect(content().contentType("application/json")) .andExpect(jsonPath("$.composers[0].someDouble").isNumber()) .andRespond(withSuccess()); @@ -164,7 +164,7 @@ public class JsonPathRequestMatchersIntegrationTests { @Test public void isBoolean() throws Exception { this.mockServer.expect(requestTo("/composers")) - .andExpect(content().contentType("application/json;charset=UTF-8")) + .andExpect(content().contentType("application/json")) .andExpect(jsonPath("$.composers[0].someBoolean").isBoolean()) .andRespond(withSuccess()); diff --git a/spring-test/src/test/java/org/springframework/test/web/reactive/server/HeaderAssertionTests.java b/spring-test/src/test/java/org/springframework/test/web/reactive/server/HeaderAssertionTests.java index 22282033df..af1d0a5689 100644 --- a/spring-test/src/test/java/org/springframework/test/web/reactive/server/HeaderAssertionTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/reactive/server/HeaderAssertionTests.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. @@ -109,7 +109,7 @@ public class HeaderAssertionTests { @Test public void valueMatches() { HttpHeaders headers = new HttpHeaders(); - headers.setContentType(MediaType.APPLICATION_JSON_UTF8); + headers.setContentType(MediaType.parseMediaType("application/json;charset=UTF-8")); HeaderAssertions assertions = headerAssertions(headers); // Success @@ -139,7 +139,7 @@ public class HeaderAssertionTests { @Test public void exists() { HttpHeaders headers = new HttpHeaders(); - headers.setContentType(MediaType.APPLICATION_JSON_UTF8); + headers.setContentType(MediaType.APPLICATION_JSON); HeaderAssertions assertions = headerAssertions(headers); // Success @@ -159,7 +159,7 @@ public class HeaderAssertionTests { @Test public void doesNotExist() { HttpHeaders headers = new HttpHeaders(); - headers.setContentType(MediaType.APPLICATION_JSON_UTF8); + headers.setContentType(MediaType.parseMediaType("application/json;charset=UTF-8")); HeaderAssertions assertions = headerAssertions(headers); // Success diff --git a/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/ErrorTests.java b/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/ErrorTests.java index fef80e1c32..2cf87b45fb 100644 --- a/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/ErrorTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/ErrorTests.java @@ -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. @@ -62,7 +62,7 @@ public class ErrorTests { public void badRequestBeforeRequestBodyConsumed() { EntityExchangeResult result = this.client.post() .uri("/post") - .contentType(MediaType.APPLICATION_JSON_UTF8) + .contentType(MediaType.APPLICATION_JSON) .syncBody(new Person("Dan")) .exchange() .expectStatus().isBadRequest() diff --git a/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/JsonContentTests.java b/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/JsonContentTests.java index 74704a9f90..f530268084 100644 --- a/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/JsonContentTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/JsonContentTests.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. @@ -48,7 +48,7 @@ public class JsonContentTests { @Test public void jsonContent() { this.client.get().uri("/persons") - .accept(MediaType.APPLICATION_JSON_UTF8) + .accept(MediaType.APPLICATION_JSON) .exchange() .expectStatus().isOk() .expectBody().json("[{\"name\":\"Jane\"},{\"name\":\"Jason\"},{\"name\":\"John\"}]"); @@ -57,7 +57,7 @@ public class JsonContentTests { @Test public void jsonPathIsEqualTo() { this.client.get().uri("/persons") - .accept(MediaType.APPLICATION_JSON_UTF8) + .accept(MediaType.APPLICATION_JSON) .exchange() .expectStatus().isOk() .expectBody() @@ -69,7 +69,7 @@ public class JsonContentTests { @Test public void jsonPathMatches() { this.client.get().uri("/persons/John") - .accept(MediaType.APPLICATION_JSON_UTF8) + .accept(MediaType.APPLICATION_JSON) .exchange() .expectStatus().isOk() .expectBody() @@ -79,7 +79,7 @@ public class JsonContentTests { @Test public void postJsonContent() { this.client.post().uri("/persons") - .contentType(MediaType.APPLICATION_JSON_UTF8) + .contentType(MediaType.APPLICATION_JSON) .syncBody("{\"name\":\"John\"}") .exchange() .expectStatus().isCreated() diff --git a/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/ResponseEntityTests.java b/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/ResponseEntityTests.java index 0ce3752d9a..b534208956 100644 --- a/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/ResponseEntityTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/ResponseEntityTests.java @@ -63,7 +63,7 @@ public class ResponseEntityTests { this.client.get().uri("/John") .exchange() .expectStatus().isOk() - .expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8) + .expectHeader().contentType(MediaType.APPLICATION_JSON) .expectBody(Person.class).isEqualTo(new Person("John")); } @@ -72,7 +72,7 @@ public class ResponseEntityTests { this.client.get().uri("/John") .exchange() .expectStatus().isOk() - .expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8) + .expectHeader().contentType(MediaType.APPLICATION_JSON) .expectBody(Person.class).value(Person::getName, startsWith("Joh")); } @@ -81,7 +81,7 @@ public class ResponseEntityTests { this.client.get().uri("/John") .exchange() .expectStatus().isOk() - .expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8) + .expectHeader().contentType(MediaType.APPLICATION_JSON) .expectBody(Person.class) .consumeWith(result -> assertEquals(new Person("John"), result.getResponseBody())); } @@ -95,7 +95,7 @@ public class ResponseEntityTests { this.client.get() .exchange() .expectStatus().isOk() - .expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8) + .expectHeader().contentType(MediaType.APPLICATION_JSON) .expectBodyList(Person.class).isEqualTo(expected); } @@ -105,7 +105,7 @@ public class ResponseEntityTests { this.client.get() .exchange() .expectStatus().isOk() - .expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8) + .expectHeader().contentType(MediaType.APPLICATION_JSON) .expectBodyList(Person.class).value(people -> { MatcherAssert.assertThat(people, hasItem(new Person("Jason"))); }); diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/AsyncTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/AsyncTests.java index 0763d15da0..4eb0f31e51 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/AsyncTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/AsyncTests.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. @@ -70,7 +70,7 @@ public class AsyncTests { this.mockMvc.perform(asyncDispatch(mvcResult)) .andExpect(status().isOk()) - .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}")); } @@ -98,7 +98,7 @@ public class AsyncTests { .andExpect(request().asyncStarted()) .andDo(MvcResult::getAsyncResult) .andExpect(status().isOk()) - .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.5}")); } @@ -112,7 +112,7 @@ public class AsyncTests { this.mockMvc.perform(asyncDispatch(mvcResult)) .andExpect(status().isOk()) - .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}")); } @@ -125,7 +125,7 @@ public class AsyncTests { this.mockMvc.perform(asyncDispatch(mvcResult)) .andExpect(status().isOk()) - .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}")); } @@ -150,7 +150,7 @@ public class AsyncTests { this.mockMvc.perform(asyncDispatch(mvcResult)) .andExpect(status().isOk()) - .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}")); } @@ -162,7 +162,7 @@ public class AsyncTests { this.mockMvc.perform(asyncDispatch(mvcResult)) .andExpect(status().isOk()) - .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}")); } @@ -183,7 +183,7 @@ public class AsyncTests { this.mockMvc.perform(asyncDispatch(mvcResult)) .andDo(print(writer)) .andExpect(status().isOk()) - .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}")); assertTrue(writer.toString().contains("Async started = false")); @@ -224,7 +224,7 @@ public class AsyncTests { @RequestMapping(params = "streamingJson") public ResponseEntity getStreamingJson() { - return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON_UTF8) + return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON) .body(os -> os.write("{\"name\":\"Joe\",\"someDouble\":0.5}".getBytes(StandardCharsets.UTF_8))); } diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/RequestParameterTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/RequestParameterTests.java index 9db137d8be..ff994a352c 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/RequestParameterTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/RequestParameterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 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,7 +41,7 @@ public class RequestParameterTests { standaloneSetup(new PersonController()).build() .perform(get("/search?name=George").accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) - .andExpect(content().contentType("application/json;charset=UTF-8")) + .andExpect(content().contentType("application/json")) .andExpect(jsonPath("$.name").value("George")); } diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/ResponseBodyTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/ResponseBodyTests.java index 69cc6c53fb..408fc7b9b1 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/ResponseBodyTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/ResponseBodyTests.java @@ -41,7 +41,7 @@ public class ResponseBodyTests { standaloneSetup(new PersonController()).build() .perform(get("/person/Lee").accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) - .andExpect(content().contentType("application/json;charset=UTF-8")) + .andExpect(content().contentType("application/json")) .andExpect(jsonPath("$.name").value("Lee")); } diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/JsonPathAssertionTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/JsonPathAssertionTests.java index 927d2eeaa3..2c51fcfbf8 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/JsonPathAssertionTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/JsonPathAssertionTests.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. @@ -51,7 +51,7 @@ public class JsonPathAssertionTests { this.mockMvc = standaloneSetup(new MusicController()) .defaultRequest(get("/").accept(MediaType.APPLICATION_JSON)) .alwaysExpect(status().isOk()) - .alwaysExpect(content().contentType("application/json;charset=UTF-8")) + .alwaysExpect(content().contentType("application/json")) .build(); } diff --git a/spring-test/src/test/kotlin/org/springframework/test/web/servlet/MockMvcExtensionsTests.kt b/spring-test/src/test/kotlin/org/springframework/test/web/servlet/MockMvcExtensionsTests.kt index a91b3a1afd..0dc5a6e13d 100644 --- a/spring-test/src/test/kotlin/org/springframework/test/web/servlet/MockMvcExtensionsTests.kt +++ b/spring-test/src/test/kotlin/org/springframework/test/web/servlet/MockMvcExtensionsTests.kt @@ -51,7 +51,7 @@ class MockMvcExtensionsTests { principal = Principal { "foo" } }.andExpect { status { isOk } - content { contentType(APPLICATION_JSON_UTF8) } + content { contentType(APPLICATION_JSON) } jsonPath("$.name") { value("Lee") } content { json("""{"someBoolean": false}""", false) } }.andDo { @@ -89,14 +89,14 @@ class MockMvcExtensionsTests { fun get() { mockMvc.get("/person/{name}", "Lee") { secure = true - accept = APPLICATION_JSON_UTF8 + accept = APPLICATION_JSON headers { contentLanguage = Locale.FRANCE } principal = Principal { "foo" } }.andExpect { status { isOk } - content { contentType(APPLICATION_JSON_UTF8) } + content { contentType(APPLICATION_JSON) } jsonPath("$.name") { value("Lee") } content { json("""{"someBoolean": false}""", false) } }.andDo { diff --git a/spring-web/src/main/java/org/springframework/http/MediaType.java b/spring-web/src/main/java/org/springframework/http/MediaType.java index fc162aa464..c80d997056 100644 --- a/spring-web/src/main/java/org/springframework/http/MediaType.java +++ b/spring-web/src/main/java/org/springframework/http/MediaType.java @@ -48,6 +48,7 @@ import org.springframework.util.StringUtils; * @see * HTTP 1.1: Semantics and Content, section 3.1.1.1 */ +@SuppressWarnings("deprecation") public class MediaType extends MimeType implements Serializable { private static final long serialVersionUID = 2069937152339670231L; @@ -84,7 +85,6 @@ public class MediaType extends MimeType implements Serializable { /** * Public constant media type for {@code application/json}. - * @see #APPLICATION_JSON_UTF8 */ public static final MediaType APPLICATION_JSON; @@ -96,24 +96,24 @@ public class MediaType extends MimeType implements Serializable { /** * Public constant media type for {@code application/json;charset=UTF-8}. - * - *

This {@link MediaType#APPLICATION_JSON} variant should be used to set JSON - * content type because while - * RFC7159 - * clearly states that "no charset parameter is defined for this registration", some - * browsers require it for interpreting correctly UTF-8 special characters. + * @deprecated Deprecated as of Spring Framework 5.2 in favor of {@link #APPLICATION_JSON} + * since major browsers like Chrome + * + * now comply with the specification and interpret correctly UTF-8 special + * characters without requiring a {@code charset=UTF-8} parameter. */ + @Deprecated public static final MediaType APPLICATION_JSON_UTF8; /** * A String equivalent of {@link MediaType#APPLICATION_JSON_UTF8}. - * - *

This {@link MediaType#APPLICATION_JSON_VALUE} variant should be used to set JSON - * content type because while - * RFC7159 - * clearly states that "no charset parameter is defined for this registration", some - * browsers require it for interpreting correctly UTF-8 special characters. + * @deprecated Deprecated as of Spring Framework 5.2 in favor of {@link #APPLICATION_JSON_VALUE} + * since major browsers like Chrome + * + * now comply with the specification and interpret correctly UTF-8 special + * characters without requiring a {@code charset=UTF-8} parameter. */ + @Deprecated public static final String APPLICATION_JSON_UTF8_VALUE = "application/json;charset=UTF-8"; /** @@ -157,13 +157,25 @@ public class MediaType extends MimeType implements Serializable { * @since 5.0 * @see * Problem Details for HTTP APIs, 6.1. application/problem+json + * @deprecated Deprecated as of Spring Framework 5.2 in favor of {@link #APPLICATION_PROBLEM_JSON} + * since major browsers like Chrome + * + * now comply with the specification and interpret correctly UTF-8 special + * characters without requiring a {@code charset=UTF-8} parameter. */ + @Deprecated public static final MediaType APPLICATION_PROBLEM_JSON_UTF8; /** * A String equivalent of {@link MediaType#APPLICATION_PROBLEM_JSON_UTF8}. * @since 5.0 + * @deprecated Deprecated as of Spring Framework 5.2 in favor of {@link #APPLICATION_PROBLEM_JSON_VALUE} + * since major browsers like Chrome + * + * now comply with the specification and interpret correctly UTF-8 special + * characters without requiring a {@code charset=UTF-8} parameter. */ + @Deprecated public static final String APPLICATION_PROBLEM_JSON_UTF8_VALUE = "application/problem+json;charset=UTF-8"; /** diff --git a/spring-web/src/main/java/org/springframework/http/codec/json/Jackson2CodecSupport.java b/spring-web/src/main/java/org/springframework/http/codec/json/Jackson2CodecSupport.java index 4e0105ddf2..5a3259d72b 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/json/Jackson2CodecSupport.java +++ b/spring-web/src/main/java/org/springframework/http/codec/json/Jackson2CodecSupport.java @@ -18,7 +18,6 @@ package org.springframework.http.codec.json; import java.lang.annotation.Annotation; import java.lang.reflect.Type; -import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -61,8 +60,8 @@ public abstract class Jackson2CodecSupport { private static final List DEFAULT_MIME_TYPES = Collections.unmodifiableList( Arrays.asList( - new MimeType("application", "json", StandardCharsets.UTF_8), - new MimeType("application", "*+json", StandardCharsets.UTF_8))); + new MimeType("application", "json"), + new MimeType("application", "*+json"))); protected final Log logger = HttpLogging.forLogName(getClass()); diff --git a/spring-web/src/main/java/org/springframework/http/codec/json/Jackson2SmileDecoder.java b/spring-web/src/main/java/org/springframework/http/codec/json/Jackson2SmileDecoder.java index 3d69190b18..4fc6095f2e 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/json/Jackson2SmileDecoder.java +++ b/spring-web/src/main/java/org/springframework/http/codec/json/Jackson2SmileDecoder.java @@ -16,8 +16,6 @@ package org.springframework.http.codec.json; -import java.nio.charset.StandardCharsets; - import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.dataformat.smile.SmileFactory; @@ -37,8 +35,8 @@ import org.springframework.util.MimeType; public class Jackson2SmileDecoder extends AbstractJackson2Decoder { private static final MimeType[] DEFAULT_SMILE_MIME_TYPES = new MimeType[] { - new MimeType("application", "x-jackson-smile", StandardCharsets.UTF_8), - new MimeType("application", "*+x-jackson-smile", StandardCharsets.UTF_8)}; + new MimeType("application", "x-jackson-smile"), + new MimeType("application", "*+x-jackson-smile")}; public Jackson2SmileDecoder() { diff --git a/spring-web/src/main/java/org/springframework/http/codec/json/Jackson2SmileEncoder.java b/spring-web/src/main/java/org/springframework/http/codec/json/Jackson2SmileEncoder.java index 60499abfa4..457679fdd4 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/json/Jackson2SmileEncoder.java +++ b/spring-web/src/main/java/org/springframework/http/codec/json/Jackson2SmileEncoder.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. @@ -16,7 +16,6 @@ package org.springframework.http.codec.json; -import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.List; @@ -41,8 +40,8 @@ import org.springframework.util.MimeType; public class Jackson2SmileEncoder extends AbstractJackson2Encoder { private static final MimeType[] DEFAULT_SMILE_MIME_TYPES = new MimeType[] { - new MimeType("application", "x-jackson-smile", StandardCharsets.UTF_8), - new MimeType("application", "*+x-jackson-smile", StandardCharsets.UTF_8)}; + new MimeType("application", "x-jackson-smile"), + new MimeType("application", "*+x-jackson-smile")}; public Jackson2SmileEncoder() { diff --git a/spring-web/src/main/java/org/springframework/http/converter/json/AbstractJackson2HttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/json/AbstractJackson2HttpMessageConverter.java index 9230ff025d..601b67192e 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/json/AbstractJackson2HttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/json/AbstractJackson2HttpMessageConverter.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. @@ -19,7 +19,6 @@ package org.springframework.http.converter.json; import java.io.IOException; import java.lang.reflect.Type; import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Collections; import java.util.concurrent.atomic.AtomicReference; @@ -49,6 +48,7 @@ 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; @@ -72,7 +72,9 @@ public abstract class AbstractJackson2HttpMessageConverter extends AbstractGener /** * The default charset used by the converter. */ - public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8; + @Nullable + @Deprecated + public static final Charset DEFAULT_CHARSET = null; protected ObjectMapper objectMapper; @@ -86,7 +88,6 @@ public abstract class AbstractJackson2HttpMessageConverter extends AbstractGener protected AbstractJackson2HttpMessageConverter(ObjectMapper objectMapper) { this.objectMapper = objectMapper; - setDefaultCharset(DEFAULT_CHARSET); DefaultPrettyPrinter prettyPrinter = new DefaultPrettyPrinter(); prettyPrinter.indentObjectsWith(new DefaultIndenter(" ", "\ndata:")); this.ssePrettyPrinter = prettyPrinter; @@ -250,6 +251,9 @@ 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); diff --git a/spring-web/src/main/java/org/springframework/http/converter/xml/MappingJackson2XmlHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/xml/MappingJackson2XmlHttpMessageConverter.java index 40bfc169fb..3e1d6729da 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/xml/MappingJackson2XmlHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/xml/MappingJackson2XmlHttpMessageConverter.java @@ -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. @@ -16,6 +16,8 @@ package org.springframework.http.converter.xml; +import java.nio.charset.StandardCharsets; + import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.dataformat.xml.XmlMapper; @@ -57,9 +59,9 @@ public class MappingJackson2XmlHttpMessageConverter extends AbstractJackson2Http * @see Jackson2ObjectMapperBuilder#xml() */ public MappingJackson2XmlHttpMessageConverter(ObjectMapper objectMapper) { - super(objectMapper, new MediaType("application", "xml"), - new MediaType("text", "xml"), - new MediaType("application", "*+xml")); + super(objectMapper, new MediaType("application", "xml", StandardCharsets.UTF_8), + new MediaType("text", "xml", StandardCharsets.UTF_8), + new MediaType("application", "*+xml", StandardCharsets.UTF_8)); Assert.isInstanceOf(XmlMapper.class, objectMapper, "XmlMapper required"); } diff --git a/spring-web/src/test/java/org/springframework/http/codec/json/Jackson2JsonDecoderTests.java b/spring-web/src/test/java/org/springframework/http/codec/json/Jackson2JsonDecoderTests.java index 3a0e941add..1f5d680871 100644 --- a/spring-web/src/test/java/org/springframework/http/codec/json/Jackson2JsonDecoderTests.java +++ b/spring-web/src/test/java/org/springframework/http/codec/json/Jackson2JsonDecoderTests.java @@ -72,7 +72,6 @@ public class Jackson2JsonDecoderTests extends AbstractDecoderTestCase>> result = Mono.just(bodyBuilder.build()); @@ -144,7 +144,7 @@ public class MultipartHttpMessageWriterTests extends AbstractLeakCheckingTestCas part = requestParts.getFirst("json"); assertEquals("json", part.name()); - assertEquals(MediaType.APPLICATION_JSON_UTF8, part.headers().getContentType()); + assertEquals(MediaType.APPLICATION_JSON, part.headers().getContentType()); String value = StringDecoder.textPlainOnly(false).decodeToMono(part.content(), ResolvableType.forClass(String.class), MediaType.TEXT_PLAIN, diff --git a/spring-web/src/test/java/org/springframework/http/converter/json/MappingJackson2HttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/json/MappingJackson2HttpMessageConverterTests.java index 8b2ef6bf64..68e6b9b65a 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/json/MappingJackson2HttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/json/MappingJackson2HttpMessageConverterTests.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. @@ -139,7 +139,7 @@ public class MappingJackson2HttpMessageConverterTests { assertTrue(result.contains("\"array\":[\"Foo\",\"Bar\"]")); assertTrue(result.contains("\"bool\":true")); assertTrue(result.contains("\"bytes\":\"AQI=\"")); - assertEquals("Invalid content-type", new MediaType("application", "json", StandardCharsets.UTF_8), + assertEquals("Invalid content-type", MediaType.APPLICATION_JSON, outputMessage.getHeaders().getContentType()); } @@ -161,7 +161,7 @@ public class MappingJackson2HttpMessageConverterTests { assertTrue(result.contains("\"array\":[\"Foo\",\"Bar\"]")); assertTrue(result.contains("\"bool\":true")); assertTrue(result.contains("\"bytes\":\"AQI=\"")); - assertEquals("Invalid content-type", new MediaType("application", "json", StandardCharsets.UTF_8), + assertEquals("Invalid content-type", MediaType.APPLICATION_JSON, outputMessage.getHeaders().getContentType()); } diff --git a/spring-web/src/test/java/org/springframework/http/converter/protobuf/ProtobufHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/protobuf/ProtobufHttpMessageConverterTests.java index e9671a06cf..7820c5de4b 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/protobuf/ProtobufHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/protobuf/ProtobufHttpMessageConverterTests.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. @@ -143,7 +143,7 @@ public class ProtobufHttpMessageConverterTests { new ProtobufHttpMessageConverter.ProtobufJavaUtilSupport(null, null), this.extensionRegistry); MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); - MediaType contentType = MediaType.APPLICATION_JSON_UTF8; + MediaType contentType = MediaType.APPLICATION_JSON; this.converter.write(this.testMsg, contentType, outputMessage); assertEquals(contentType, outputMessage.getHeaders().getContentType()); diff --git a/spring-web/src/test/java/org/springframework/http/converter/smile/MappingJackson2SmileHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/smile/MappingJackson2SmileHttpMessageConverterTests.java index 9860ae36ac..8a8c597369 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/smile/MappingJackson2SmileHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/smile/MappingJackson2SmileHttpMessageConverterTests.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. @@ -17,7 +17,6 @@ package org.springframework.http.converter.smile; import java.io.IOException; -import java.nio.charset.StandardCharsets; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.dataformat.smile.SmileFactory; @@ -90,7 +89,7 @@ public class MappingJackson2SmileHttpMessageConverterTests { body.setBytes(new byte[]{0x1, 0x2}); converter.write(body, null, outputMessage); assertArrayEquals(mapper.writeValueAsBytes(body), outputMessage.getBodyAsBytes()); - assertEquals("Invalid content-type", new MediaType("application", "x-jackson-smile", StandardCharsets.UTF_8), + assertEquals("Invalid content-type", new MediaType("application", "x-jackson-smile"), outputMessage.getHeaders().getContentType()); } diff --git a/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/ServerResponseExtensions.kt b/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/ServerResponseExtensions.kt index f7cac34324..fc5c212e9c 100644 --- a/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/ServerResponseExtensions.kt +++ b/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/ServerResponseExtensions.kt @@ -48,11 +48,11 @@ inline fun ServerResponse.BodyBuilder.bodyToServerSentEvents(p contentType(MediaType.TEXT_EVENT_STREAM).body(publisher, object : ParameterizedTypeReference() {}) /** - * 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. diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/config/WebFluxConfigurationSupportTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/config/WebFluxConfigurationSupportTests.java index 7dc13078a2..188a87638e 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/config/WebFluxConfigurationSupportTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/config/WebFluxConfigurationSupportTests.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. @@ -267,7 +267,7 @@ public class WebFluxConfigurationSupportTests { List 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)); } diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/result/HandlerResultHandlerTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/result/HandlerResultHandlerTests.java index c474f2677d..efcf8c8c08 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/result/HandlerResultHandlerTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/result/HandlerResultHandlerTests.java @@ -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 mediaTypes = Arrays.asList(TEXT_PLAIN, APPLICATION_JSON_UTF8); + List mediaTypes = Arrays.asList(TEXT_PLAIN, APPLICATION_JSON); MediaType actual = this.resultHandler.selectMediaType(exchange, () -> mediaTypes); - assertEquals(APPLICATION_JSON_UTF8, actual); + assertEquals(APPLICATION_JSON, actual); } @Test diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/JacksonHintsIntegrationTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/JacksonHintsIntegrationTests.java index 7fc395ed68..2755c06698 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/JacksonHintsIntegrationTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/JacksonHintsIntegrationTests.java @@ -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()); } diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/MessageWriterResultHandlerTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/MessageWriterResultHandlerTests.java index 395b992409..e240e86e0b 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/MessageWriterResultHandlerTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/MessageWriterResultHandlerTests.java @@ -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 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 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\"}]"); } diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestMappingExceptionHandlingIntegrationTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestMappingExceptionHandlingIntegrationTests.java index 2f79776389..fd4f6eaac2 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestMappingExceptionHandlingIntegrationTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestMappingExceptionHandlingIntegrationTests.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. @@ -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()); } } diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/result/view/HttpMessageWriterViewTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/result/view/HttpMessageWriterViewTests.java index 3835f47202..7916768ef4 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/result/view/HttpMessageWriterViewTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/result/view/HttpMessageWriterViewTests.java @@ -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()); } diff --git a/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/ServerResponseExtensionsTests.kt b/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/ServerResponseExtensionsTests.kt index 99a5f4a9dc..2108df991b 100644 --- a/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/ServerResponseExtensionsTests.kt +++ b/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/ServerResponseExtensionsTests.kt @@ -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 diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestResponseBodyMethodProcessorTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestResponseBodyMethodProcessorTests.java index 9a44139517..1fbd51e4d6 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestResponseBodyMethodProcessorTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestResponseBodyMethodProcessorTests.java @@ -692,7 +692,7 @@ public class RequestResponseBodyMethodProcessorTests { @Test // SPR-14520 public void resolveArgumentTypeVariableWithGenericInterface() throws Exception { this.servletRequest.setContent("\"foo\"".getBytes("UTF-8")); - this.servletRequest.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE); + this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE); Method method = MyControllerImplementingInterface.class.getMethod("handle", Object.class); HandlerMethod handlerMethod = new HandlerMethod(new MyControllerImplementingInterface(), method); diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ServletAnnotationControllerHandlerMethodTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ServletAnnotationControllerHandlerMethodTests.java index 791ae3fe81..9261cd255e 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ServletAnnotationControllerHandlerMethodTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ServletAnnotationControllerHandlerMethodTests.java @@ -1205,7 +1205,7 @@ public class ServletAnnotationControllerHandlerMethodTests extends AbstractServl response = new MockHttpServletResponse(); getServlet().service(request, response); assertEquals(500, response.getStatus()); - assertEquals("application/problem+json;charset=UTF-8", response.getContentType()); + assertEquals("application/problem+json", response.getContentType()); assertEquals("{\"reason\":\"error\"}", response.getContentAsString()); } diff --git a/src/docs/asciidoc/languages/kotlin.adoc b/src/docs/asciidoc/languages/kotlin.adoc index 4753fa147f..4260736018 100644 --- a/src/docs/asciidoc/languages/kotlin.adoc +++ b/src/docs/asciidoc/languages/kotlin.adoc @@ -350,7 +350,7 @@ mockMvc.get("/person/{name}", "Lee") { principal = Principal { "foo" } }.andExpect { status { isOk } - content { contentType(APPLICATION_JSON_UTF8) } + content { contentType(APPLICATION_JSON) } jsonPath("$.name") { value("Lee") } content { json("""{"someBoolean": false}""", false) } }.andDo { @@ -567,7 +567,7 @@ class UserHandler(builder: WebClient.Builder) { client.get().uri("...").awaitExchange().awaitBody())) suspend fun listApi(request: ServerRequest): ServerResponse = - ServerResponse.ok().contentType(MediaType.APPLICATION_JSON_UTF8).bodyAndAwait( + ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).bodyAndAwait( client.get().uri("...").awaitExchange().awaitBody()) } ---- diff --git a/src/docs/asciidoc/testing-webtestclient.adoc b/src/docs/asciidoc/testing-webtestclient.adoc index 78fc106209..daa1a74b13 100644 --- a/src/docs/asciidoc/testing-webtestclient.adoc +++ b/src/docs/asciidoc/testing-webtestclient.adoc @@ -141,10 +141,10 @@ Typically, you start by asserting the response status and headers, as follows: [subs="verbatim,quotes"] ---- client.get().uri("/persons/1") - .accept(MediaType.APPLICATION_JSON_UTF8) + .accept(MediaType.APPLICATION_JSON) .exchange() .expectStatus().isOk() - .expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8) + .expectHeader().contentType(MediaType.APPLICATION_JSON) // ... ---- diff --git a/src/docs/asciidoc/web/webflux.adoc b/src/docs/asciidoc/web/webflux.adoc index 11aedae930..eae28206c9 100644 --- a/src/docs/asciidoc/web/webflux.adoc +++ b/src/docs/asciidoc/web/webflux.adoc @@ -1342,7 +1342,7 @@ content types that a controller method produces, as the following example shows: [source,java,indent=0] [subs="verbatim,quotes"] ---- - @GetMapping(path = "/pets/{petId}", produces = "application/json;charset=UTF-8") + @GetMapping(path = "/pets/{petId}", produces = "application/json") @ResponseBody public Pet getPet(@PathVariable String petId) { // ... @@ -1352,17 +1352,12 @@ content types that a controller method produces, as the following example shows: The media type can specify a character set. Negated expressions are supported -- for example, `!text/plain` means any content type other than `text/plain`. -NOTE: For JSON content type, you should specify the UTF-8 `charset` even if -https://tools.ietf.org/html/rfc7159#section-11[RFC7159] -clearly states that "`no charset parameter is defined for this registration,`" because some -browsers require it to correctly interpret UTF-8 special characters. - You can declare a shared `produces` attribute at the class level. Unlike most other request mapping attributes, however, when used at the class level, a method-level `produces` attribute overrides rather than extend the class level declaration. TIP: `MediaType` provides constants for commonly used media types -- e.g. -`APPLICATION_JSON_UTF8_VALUE`, `APPLICATION_XML_VALUE`. +`APPLICATION_JSON_VALUE`, `APPLICATION_XML_VALUE`. [[webflux-ann-requestmapping-params-and-headers]] diff --git a/src/docs/asciidoc/web/webmvc.adoc b/src/docs/asciidoc/web/webmvc.adoc index 0713a14816..7dafe93760 100644 --- a/src/docs/asciidoc/web/webmvc.adoc +++ b/src/docs/asciidoc/web/webmvc.adoc @@ -1504,7 +1504,7 @@ content types that a controller method produces, as the following example shows: [source,java,indent=0] [subs="verbatim,quotes"] ---- - @GetMapping(path = "/pets/{petId}", produces = "application/json;charset=UTF-8") <1> + @GetMapping(path = "/pets/{petId}", produces = "application/json") <1> @ResponseBody public Pet getPet(@PathVariable String petId) { // ... @@ -1515,17 +1515,12 @@ content types that a controller method produces, as the following example shows: The media type can specify a character set. Negated expressions are supported -- for example, `!text/plain` means any content type other than "text/plain". -NOTE: For the JSON content type, the UTF-8 charset should be specified even if -https://tools.ietf.org/html/rfc7159#section-11[RFC7159] -clearly states that "`no charset parameter is defined for this registration`", because some -browsers require it to correctly interpret UTF-8 special characters. - You can declare a shared `produces` attribute at the class level. Unlike most other request-mapping attributes, however, when used at the class level, a method-level `produces` attribute overrides rather than extends the class-level declaration. TIP: `MediaType` provides constants for commonly used media types, such as -`APPLICATION_JSON_UTF8_VALUE` and `APPLICATION_XML_VALUE`. +`APPLICATION_JSON_VALUE` and `APPLICATION_XML_VALUE`. [[mvc-ann-requestmapping-params-and-headers]]