Skip to content

Commit a9f0eae

Browse files
committed
Merge branch '5.1.x'
2 parents 77eef47 + 6e0dfd6 commit a9f0eae

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

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

Lines changed: 9 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.
@@ -22,6 +22,7 @@
2222
import java.util.ArrayList;
2323
import java.util.List;
2424

25+
import org.springframework.http.HttpHeaders;
2526
import org.springframework.http.HttpInputMessage;
2627
import org.springframework.http.HttpOutputMessage;
2728
import org.springframework.http.MediaType;
@@ -72,7 +73,9 @@ public StringHttpMessageConverter(Charset defaultCharset) {
7273

7374

7475
/**
75-
* Indicates whether the {@code Accept-Charset} should be written to any outgoing request.
76+
* Whether the {@code Accept-Charset} header should be written to any outgoing
77+
* request sourced from the value of {@link Charset#availableCharsets()}.
78+
* The behavior is suppressed if the header has already been set.
7679
* <p>Default is {@code true}.
7780
*/
7881
public void setWriteAcceptCharset(boolean writeAcceptCharset) {
@@ -99,10 +102,11 @@ protected Long getContentLength(String str, @Nullable MediaType contentType) {
99102

100103
@Override
101104
protected void writeInternal(String str, HttpOutputMessage outputMessage) throws IOException {
102-
if (this.writeAcceptCharset) {
103-
outputMessage.getHeaders().setAcceptCharset(getAcceptedCharsets());
105+
HttpHeaders headers = outputMessage.getHeaders();
106+
if (this.writeAcceptCharset && headers.get(HttpHeaders.ACCEPT_CHARSET) == null) {
107+
headers.setAcceptCharset(getAcceptedCharsets());
104108
}
105-
Charset charset = getContentTypeCharset(outputMessage.getHeaders().getContentType());
109+
Charset charset = getContentTypeCharset(headers.getContentType());
106110
StreamUtils.copy(str, charset, outputMessage.getBody());
107111
}
108112

0 commit comments

Comments
 (0)