Merge branch '5.2.x'
This commit is contained in:
commit
f13ab6d18c
|
|
@ -43,6 +43,8 @@ import org.springframework.util.StreamUtils;
|
||||||
*/
|
*/
|
||||||
public class StringHttpMessageConverter extends AbstractHttpMessageConverter<String> {
|
public class StringHttpMessageConverter extends AbstractHttpMessageConverter<String> {
|
||||||
|
|
||||||
|
private static final MediaType APPLICATION_PLUS_JSON = new MediaType("application", "*+json");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The default charset used by the converter.
|
* The default charset used by the converter.
|
||||||
*/
|
*/
|
||||||
|
|
@ -104,7 +106,9 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverter<Str
|
||||||
@Override
|
@Override
|
||||||
protected void addDefaultHeaders(HttpHeaders headers, String s, @Nullable MediaType type) throws IOException {
|
protected void addDefaultHeaders(HttpHeaders headers, String s, @Nullable MediaType type) throws IOException {
|
||||||
if (headers.getContentType() == null ) {
|
if (headers.getContentType() == null ) {
|
||||||
if (type != null && type.isConcrete() && type.isCompatibleWith(MediaType.APPLICATION_JSON)) {
|
if (type != null && type.isConcrete() &&
|
||||||
|
(type.isCompatibleWith(MediaType.APPLICATION_JSON) ||
|
||||||
|
type.isCompatibleWith(APPLICATION_PLUS_JSON))) {
|
||||||
// Prevent charset parameter for JSON..
|
// Prevent charset parameter for JSON..
|
||||||
headers.setContentType(type);
|
headers.setContentType(type);
|
||||||
}
|
}
|
||||||
|
|
@ -144,7 +148,8 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverter<Str
|
||||||
if (charset != null) {
|
if (charset != null) {
|
||||||
return charset;
|
return charset;
|
||||||
}
|
}
|
||||||
else if (contentType.isCompatibleWith(MediaType.APPLICATION_JSON)) {
|
else if (contentType.isCompatibleWith(MediaType.APPLICATION_JSON) ||
|
||||||
|
contentType.isCompatibleWith(APPLICATION_PLUS_JSON)) {
|
||||||
// Matching to AbstractJackson2HttpMessageConverter#DEFAULT_CHARSET
|
// Matching to AbstractJackson2HttpMessageConverter#DEFAULT_CHARSET
|
||||||
return StandardCharsets.UTF_8;
|
return StandardCharsets.UTF_8;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -80,6 +80,16 @@ public class StringHttpMessageConverterTests {
|
||||||
assertThat(result).as("Invalid result").isEqualTo(body);
|
assertThat(result).as("Invalid result").isEqualTo(body);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test // gh-25328
|
||||||
|
public void readJsonApi() throws IOException {
|
||||||
|
String body = "{\"result\":\"\u0414\u0410\"}";
|
||||||
|
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
|
||||||
|
inputMessage.getHeaders().setContentType(new MediaType("application", "vnd.api.v1+json"));
|
||||||
|
String result = this.converter.read(String.class, inputMessage);
|
||||||
|
|
||||||
|
assertThat(result).as("Invalid result").isEqualTo(body);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void writeDefaultCharset() throws IOException {
|
public void writeDefaultCharset() throws IOException {
|
||||||
String body = "H\u00e9llo W\u00f6rld";
|
String body = "H\u00e9llo W\u00f6rld";
|
||||||
|
|
@ -94,7 +104,7 @@ public class StringHttpMessageConverterTests {
|
||||||
|
|
||||||
@Test // gh-24123
|
@Test // gh-24123
|
||||||
public void writeJson() throws IOException {
|
public void writeJson() throws IOException {
|
||||||
String body = "{\"foo\":\"bar\"}";
|
String body = "{\"føø\":\"bår\"}";
|
||||||
this.converter.write(body, MediaType.APPLICATION_JSON, this.outputMessage);
|
this.converter.write(body, MediaType.APPLICATION_JSON, this.outputMessage);
|
||||||
|
|
||||||
HttpHeaders headers = this.outputMessage.getHeaders();
|
HttpHeaders headers = this.outputMessage.getHeaders();
|
||||||
|
|
@ -104,6 +114,19 @@ public class StringHttpMessageConverterTests {
|
||||||
assertThat(headers.getAcceptCharset().isEmpty()).isTrue();
|
assertThat(headers.getAcceptCharset().isEmpty()).isTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test // gh-25328
|
||||||
|
public void writeJsonApi() throws IOException {
|
||||||
|
String body = "{\"føø\":\"bår\"}";
|
||||||
|
MediaType contentType = new MediaType("application", "vnd.api.v1+json");
|
||||||
|
this.converter.write(body, contentType, this.outputMessage);
|
||||||
|
|
||||||
|
HttpHeaders headers = this.outputMessage.getHeaders();
|
||||||
|
assertThat(this.outputMessage.getBodyAsString(StandardCharsets.UTF_8)).isEqualTo(body);
|
||||||
|
assertThat(headers.getContentType()).isEqualTo(contentType);
|
||||||
|
assertThat(headers.getContentLength()).isEqualTo(body.getBytes(StandardCharsets.UTF_8).length);
|
||||||
|
assertThat(headers.getAcceptCharset().isEmpty()).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void writeUTF8() throws IOException {
|
public void writeUTF8() throws IOException {
|
||||||
String body = "H\u00e9llo W\u00f6rld";
|
String body = "H\u00e9llo W\u00f6rld";
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue