How can I tell RestTemplate to POST with UTF-8 encoding?Spring Boot and FCM special charactersSpring RestTemplate filename with accentsHow can I pretty-print JSON in a shell script?POST request via RestTemplate in JSONHow can I pretty-print JSON using JavaScript?How do I POST JSON data with Curl from a terminal/commandline to Test Spring REST?Can charset parameter be used with application/json content type in http/1.1?How to set an “Accept:” header on Spring RestTemplate request?java put 'UTF-8' String to java.lang.Object crushingDefault encoding of HTTP POST request with JSON bodyHow to POST form data with Spring RestTemplate?Send POST and PUT through RestTemplate to a Spring Data Rest Api
Do Legal Documents Require Signing In Standard Pen Colors?
How much character growth crosses the line into breaking the character
Two-sided logarithm inequality
Some numbers are more equivalent than others
Is XSS in canonical link possible?
Why did the EU agree to delay the Brexit deadline?
Will the technology I first learn determine the direction of my future career?
How can Trident be so inexpensive? Will it orbit Triton or just do a (slow) flyby?
Varistor? Purpose and principle
Bob has never been a M before
Can I use my Chinese passport to enter China after I acquired another citizenship?
How do I extrude a face to a single vertex
How do you respond to a colleague from another team when they're wrongly expecting that you'll help them?
MAXDOP Settings for SQL Server 2014
A social experiment. What is the worst that can happen?
Is there a word to describe the feeling of being transfixed out of horror?
How do ground effect vehicles perform turns?
Engineer refusing to file/disclose patents
Diode in opposite direction?
Find last 3 digits of this monster number
Are lightweight LN wallets vulnerable to transaction withholding?
Why do IPv6 unique local addresses have to have a /48 prefix?
Fly on a jet pack vs fly with a jet pack?
A Permanent Norse Presence in America
How can I tell RestTemplate to POST with UTF-8 encoding?
Spring Boot and FCM special charactersSpring RestTemplate filename with accentsHow can I pretty-print JSON in a shell script?POST request via RestTemplate in JSONHow can I pretty-print JSON using JavaScript?How do I POST JSON data with Curl from a terminal/commandline to Test Spring REST?Can charset parameter be used with application/json content type in http/1.1?How to set an “Accept:” header on Spring RestTemplate request?java put 'UTF-8' String to java.lang.Object crushingDefault encoding of HTTP POST request with JSON bodyHow to POST form data with Spring RestTemplate?Send POST and PUT through RestTemplate to a Spring Data Rest Api
I'm having problems posting JSON with UTF-8 encoding using RestTemplate. Default encoding for JSON is UTF-8 so the media type shouldn't even contain the charset. I have tried to put charset in the MediaType but it doesn't seem to work anyway.
My code:
String dataJson = ""food": "smörrebröd"";
HttpHeaders headers = new HttpHeaders();
MediaType mediaType = new MediaType("application", "json", StandardCharsets.UTF_8);
headers.setContentType(mediaType);
HttpEntity<String> entity = new HttpEntity<String>(dataJson, headers);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<Boolean> formEntity = restTemplate.exchange(postUrl, HttpMethod.POST, entity, Boolean.class);
json spring resttemplate
add a comment |
I'm having problems posting JSON with UTF-8 encoding using RestTemplate. Default encoding for JSON is UTF-8 so the media type shouldn't even contain the charset. I have tried to put charset in the MediaType but it doesn't seem to work anyway.
My code:
String dataJson = ""food": "smörrebröd"";
HttpHeaders headers = new HttpHeaders();
MediaType mediaType = new MediaType("application", "json", StandardCharsets.UTF_8);
headers.setContentType(mediaType);
HttpEntity<String> entity = new HttpEntity<String>(dataJson, headers);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<Boolean> formEntity = restTemplate.exchange(postUrl, HttpMethod.POST, entity, Boolean.class);
json spring resttemplate
add a comment |
I'm having problems posting JSON with UTF-8 encoding using RestTemplate. Default encoding for JSON is UTF-8 so the media type shouldn't even contain the charset. I have tried to put charset in the MediaType but it doesn't seem to work anyway.
My code:
String dataJson = ""food": "smörrebröd"";
HttpHeaders headers = new HttpHeaders();
MediaType mediaType = new MediaType("application", "json", StandardCharsets.UTF_8);
headers.setContentType(mediaType);
HttpEntity<String> entity = new HttpEntity<String>(dataJson, headers);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<Boolean> formEntity = restTemplate.exchange(postUrl, HttpMethod.POST, entity, Boolean.class);
json spring resttemplate
I'm having problems posting JSON with UTF-8 encoding using RestTemplate. Default encoding for JSON is UTF-8 so the media type shouldn't even contain the charset. I have tried to put charset in the MediaType but it doesn't seem to work anyway.
My code:
String dataJson = ""food": "smörrebröd"";
HttpHeaders headers = new HttpHeaders();
MediaType mediaType = new MediaType("application", "json", StandardCharsets.UTF_8);
headers.setContentType(mediaType);
HttpEntity<String> entity = new HttpEntity<String>(dataJson, headers);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<Boolean> formEntity = restTemplate.exchange(postUrl, HttpMethod.POST, entity, Boolean.class);
json spring resttemplate
json spring resttemplate
asked Apr 1 '15 at 13:43
anssiasanssias
7842818
7842818
add a comment |
add a comment |
7 Answers
7
active
oldest
votes
You need to add StringHttpMessageConverter to rest template's message converter with charset UTF-8. Like this
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters()
.add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
2
Worked like a charm. I had an issue with a special character which look's like '(single quotes). Had a nightmare in parsing it. Simple tweak worth a 1000 votes
– RahulArackal
Oct 31 '16 at 13:39
@mushfek0001 it didn't help i changed as mentioned below. I was able to change charset
– Sandesh
Jan 26 '17 at 20:08
How can I switch between charset 8 and 16
– Pasupathi Rajamanickam
May 31 '18 at 15:09
Why.add(0, ...
and not just.add(new StringHttp...
?
– Charles Wood
Jan 23 at 22:53
add a comment |
(Adding to solutions by mushfek0001 and zhouji)
By default RestTemplate has ISO-8859-1 StringHttpMessageConverter which is used to convert a JAVA object to request payload.
Difference between UTF-8 and ISO-8859:
UTF-8 is a multibyte encoding that can represent any Unicode
character. ISO 8859-1 is a single-byte encoding that can represent the
first 256 Unicode characters. Both encode ASCII exactly the same way.
Solution 1 (copied from mushfek001):
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters()
.add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
Solution 2:
Though above solution works, but as zhouji pointed out, it will add two string converters and it may lead to some regression issues.
If you set the right content type in http header, then ISO 8859 will take care of changing the UTF characters.
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
or
// HttpUriRequest request
request.addHeader("Content-Type", MediaType.APPLICATION_JSON_UTF8_VALUE);
Solution 2 is not an option for non-standard content types, like the one used for bulk operation in ElasticSearch REST API (application/x-ndjson).
– Krzysztof Tomaszewski
Oct 5 '18 at 13:07
solution 1 works, but solution 2headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
not work, sprint boot version 2.1.2, jdk 1.8
– xcaptain
Jan 17 at 8:12
add a comment |
The answer by @mushfek0001 produce two StringHttpMessageConverter
which will send two text/plain
and */*
, such as the debug picture.
Even the Accept
header of client will be:
text/plain, text/plain, */*, */*
So the better one is remove the ISO-8859-1 StringHttpMessageConverter
and use single UTF-8 StringHttpMessageConverter
.
Use this:
RestTemplate restTemplate = new RestTemplate();
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(StandardCharsets.UTF_8);
stringHttpMessageConverter.setWriteAcceptCharset(true);
for (int i = 0; i < restTemplate.getMessageConverters().size(); i++)
if (restTemplate.getMessageConverters().get(i) instanceof StringHttpMessageConverter)
restTemplate.getMessageConverters().remove(i);
restTemplate.getMessageConverters().add(i, stringHttpMessageConverter);
break;
I haven't observe such problem. In my case the Accept header had valueapplication/json, application/*+json
which is OK.
– Krzysztof Tomaszewski
Oct 5 '18 at 13:20
add a comment |
restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(StandardCharsets.UTF_16LE));
String response = restTemplate.getForObject(url, String.class);
Can you provide some context to your answer, that way future readers can learn how to apply it to their issues and not just in this situation.
– Newd
Jul 28 '15 at 14:28
add a comment |
Adding new StringHttpMessageConverter won't help.
In existing StringHttpMessageConverter we need to set writeAcceptCharset to false
and build httpheader with charset we want.
RestTemplate restTemplate = new RestTemplate();
List<HttpMessageConverter<?>> c = restTemplate.getMessageConverters();
for(HttpMessageConverter<?> mc :c)
if (mc instanceof StringHttpMessageConverter)
StringHttpMessageConverter mcc = (StringHttpMessageConverter) mc;
mcc.setWriteAcceptCharset(false);
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
headers.setAcceptCharset(Arrays.asList(Charset.forName("UTF-8")));
HttpEntity<String> entity = new HttpEntity<String>(jsonPayload, headers);
restTemplate.postForEntity(postResourceUrl, entity, String.class);
Adding new StringHttpMessageConverter helped.
– Alex78191
Apr 28 '18 at 12:25
add a comment |
Better you should remove the StringHttpMessageConverter first before adding new. so this way you will have one instance of StringHttpMessageConverter in our MessageConverters list.
RestTemplate restTemplate = new RestTemplate();
final Iterator<HttpMessageConverter<?>> iterator = restTemplate.getMessageConverters().iterator();
while (iterator.hasNext())
final HttpMessageConverter<?> converter = iterator.next();
if (converter instanceof StringHttpMessageConverter)
iterator.remove();
restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
add a comment |
Removing the existing converter, but with Java 8 (why do people still write Java 7 Code anyways?)
List<HttpMessageConverter<?>> converters = restTemplate.getMessageConverters();
converters .removeIf(httpMessageConverter -> httpMessageConverter instanceof StringHttpMessageConverter);
messageConverters
.add(0, new StringHttpMessageConverter(StandardCharsets.UTF_8));
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f29392422%2fhow-can-i-tell-resttemplate-to-post-with-utf-8-encoding%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
You need to add StringHttpMessageConverter to rest template's message converter with charset UTF-8. Like this
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters()
.add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
2
Worked like a charm. I had an issue with a special character which look's like '(single quotes). Had a nightmare in parsing it. Simple tweak worth a 1000 votes
– RahulArackal
Oct 31 '16 at 13:39
@mushfek0001 it didn't help i changed as mentioned below. I was able to change charset
– Sandesh
Jan 26 '17 at 20:08
How can I switch between charset 8 and 16
– Pasupathi Rajamanickam
May 31 '18 at 15:09
Why.add(0, ...
and not just.add(new StringHttp...
?
– Charles Wood
Jan 23 at 22:53
add a comment |
You need to add StringHttpMessageConverter to rest template's message converter with charset UTF-8. Like this
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters()
.add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
2
Worked like a charm. I had an issue with a special character which look's like '(single quotes). Had a nightmare in parsing it. Simple tweak worth a 1000 votes
– RahulArackal
Oct 31 '16 at 13:39
@mushfek0001 it didn't help i changed as mentioned below. I was able to change charset
– Sandesh
Jan 26 '17 at 20:08
How can I switch between charset 8 and 16
– Pasupathi Rajamanickam
May 31 '18 at 15:09
Why.add(0, ...
and not just.add(new StringHttp...
?
– Charles Wood
Jan 23 at 22:53
add a comment |
You need to add StringHttpMessageConverter to rest template's message converter with charset UTF-8. Like this
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters()
.add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
You need to add StringHttpMessageConverter to rest template's message converter with charset UTF-8. Like this
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters()
.add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
answered Apr 1 '15 at 16:04
mushfek0001mushfek0001
2,4501520
2,4501520
2
Worked like a charm. I had an issue with a special character which look's like '(single quotes). Had a nightmare in parsing it. Simple tweak worth a 1000 votes
– RahulArackal
Oct 31 '16 at 13:39
@mushfek0001 it didn't help i changed as mentioned below. I was able to change charset
– Sandesh
Jan 26 '17 at 20:08
How can I switch between charset 8 and 16
– Pasupathi Rajamanickam
May 31 '18 at 15:09
Why.add(0, ...
and not just.add(new StringHttp...
?
– Charles Wood
Jan 23 at 22:53
add a comment |
2
Worked like a charm. I had an issue with a special character which look's like '(single quotes). Had a nightmare in parsing it. Simple tweak worth a 1000 votes
– RahulArackal
Oct 31 '16 at 13:39
@mushfek0001 it didn't help i changed as mentioned below. I was able to change charset
– Sandesh
Jan 26 '17 at 20:08
How can I switch between charset 8 and 16
– Pasupathi Rajamanickam
May 31 '18 at 15:09
Why.add(0, ...
and not just.add(new StringHttp...
?
– Charles Wood
Jan 23 at 22:53
2
2
Worked like a charm. I had an issue with a special character which look's like '(single quotes). Had a nightmare in parsing it. Simple tweak worth a 1000 votes
– RahulArackal
Oct 31 '16 at 13:39
Worked like a charm. I had an issue with a special character which look's like '(single quotes). Had a nightmare in parsing it. Simple tweak worth a 1000 votes
– RahulArackal
Oct 31 '16 at 13:39
@mushfek0001 it didn't help i changed as mentioned below. I was able to change charset
– Sandesh
Jan 26 '17 at 20:08
@mushfek0001 it didn't help i changed as mentioned below. I was able to change charset
– Sandesh
Jan 26 '17 at 20:08
How can I switch between charset 8 and 16
– Pasupathi Rajamanickam
May 31 '18 at 15:09
How can I switch between charset 8 and 16
– Pasupathi Rajamanickam
May 31 '18 at 15:09
Why
.add(0, ...
and not just .add(new StringHttp...
?– Charles Wood
Jan 23 at 22:53
Why
.add(0, ...
and not just .add(new StringHttp...
?– Charles Wood
Jan 23 at 22:53
add a comment |
(Adding to solutions by mushfek0001 and zhouji)
By default RestTemplate has ISO-8859-1 StringHttpMessageConverter which is used to convert a JAVA object to request payload.
Difference between UTF-8 and ISO-8859:
UTF-8 is a multibyte encoding that can represent any Unicode
character. ISO 8859-1 is a single-byte encoding that can represent the
first 256 Unicode characters. Both encode ASCII exactly the same way.
Solution 1 (copied from mushfek001):
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters()
.add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
Solution 2:
Though above solution works, but as zhouji pointed out, it will add two string converters and it may lead to some regression issues.
If you set the right content type in http header, then ISO 8859 will take care of changing the UTF characters.
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
or
// HttpUriRequest request
request.addHeader("Content-Type", MediaType.APPLICATION_JSON_UTF8_VALUE);
Solution 2 is not an option for non-standard content types, like the one used for bulk operation in ElasticSearch REST API (application/x-ndjson).
– Krzysztof Tomaszewski
Oct 5 '18 at 13:07
solution 1 works, but solution 2headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
not work, sprint boot version 2.1.2, jdk 1.8
– xcaptain
Jan 17 at 8:12
add a comment |
(Adding to solutions by mushfek0001 and zhouji)
By default RestTemplate has ISO-8859-1 StringHttpMessageConverter which is used to convert a JAVA object to request payload.
Difference between UTF-8 and ISO-8859:
UTF-8 is a multibyte encoding that can represent any Unicode
character. ISO 8859-1 is a single-byte encoding that can represent the
first 256 Unicode characters. Both encode ASCII exactly the same way.
Solution 1 (copied from mushfek001):
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters()
.add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
Solution 2:
Though above solution works, but as zhouji pointed out, it will add two string converters and it may lead to some regression issues.
If you set the right content type in http header, then ISO 8859 will take care of changing the UTF characters.
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
or
// HttpUriRequest request
request.addHeader("Content-Type", MediaType.APPLICATION_JSON_UTF8_VALUE);
Solution 2 is not an option for non-standard content types, like the one used for bulk operation in ElasticSearch REST API (application/x-ndjson).
– Krzysztof Tomaszewski
Oct 5 '18 at 13:07
solution 1 works, but solution 2headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
not work, sprint boot version 2.1.2, jdk 1.8
– xcaptain
Jan 17 at 8:12
add a comment |
(Adding to solutions by mushfek0001 and zhouji)
By default RestTemplate has ISO-8859-1 StringHttpMessageConverter which is used to convert a JAVA object to request payload.
Difference between UTF-8 and ISO-8859:
UTF-8 is a multibyte encoding that can represent any Unicode
character. ISO 8859-1 is a single-byte encoding that can represent the
first 256 Unicode characters. Both encode ASCII exactly the same way.
Solution 1 (copied from mushfek001):
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters()
.add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
Solution 2:
Though above solution works, but as zhouji pointed out, it will add two string converters and it may lead to some regression issues.
If you set the right content type in http header, then ISO 8859 will take care of changing the UTF characters.
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
or
// HttpUriRequest request
request.addHeader("Content-Type", MediaType.APPLICATION_JSON_UTF8_VALUE);
(Adding to solutions by mushfek0001 and zhouji)
By default RestTemplate has ISO-8859-1 StringHttpMessageConverter which is used to convert a JAVA object to request payload.
Difference between UTF-8 and ISO-8859:
UTF-8 is a multibyte encoding that can represent any Unicode
character. ISO 8859-1 is a single-byte encoding that can represent the
first 256 Unicode characters. Both encode ASCII exactly the same way.
Solution 1 (copied from mushfek001):
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters()
.add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
Solution 2:
Though above solution works, but as zhouji pointed out, it will add two string converters and it may lead to some regression issues.
If you set the right content type in http header, then ISO 8859 will take care of changing the UTF characters.
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
or
// HttpUriRequest request
request.addHeader("Content-Type", MediaType.APPLICATION_JSON_UTF8_VALUE);
edited Jul 10 '17 at 16:35
answered Jun 3 '17 at 1:11
RohitRohit
2,907103975
2,907103975
Solution 2 is not an option for non-standard content types, like the one used for bulk operation in ElasticSearch REST API (application/x-ndjson).
– Krzysztof Tomaszewski
Oct 5 '18 at 13:07
solution 1 works, but solution 2headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
not work, sprint boot version 2.1.2, jdk 1.8
– xcaptain
Jan 17 at 8:12
add a comment |
Solution 2 is not an option for non-standard content types, like the one used for bulk operation in ElasticSearch REST API (application/x-ndjson).
– Krzysztof Tomaszewski
Oct 5 '18 at 13:07
solution 1 works, but solution 2headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
not work, sprint boot version 2.1.2, jdk 1.8
– xcaptain
Jan 17 at 8:12
Solution 2 is not an option for non-standard content types, like the one used for bulk operation in ElasticSearch REST API (application/x-ndjson).
– Krzysztof Tomaszewski
Oct 5 '18 at 13:07
Solution 2 is not an option for non-standard content types, like the one used for bulk operation in ElasticSearch REST API (application/x-ndjson).
– Krzysztof Tomaszewski
Oct 5 '18 at 13:07
solution 1 works, but solution 2
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
not work, sprint boot version 2.1.2, jdk 1.8– xcaptain
Jan 17 at 8:12
solution 1 works, but solution 2
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
not work, sprint boot version 2.1.2, jdk 1.8– xcaptain
Jan 17 at 8:12
add a comment |
The answer by @mushfek0001 produce two StringHttpMessageConverter
which will send two text/plain
and */*
, such as the debug picture.
Even the Accept
header of client will be:
text/plain, text/plain, */*, */*
So the better one is remove the ISO-8859-1 StringHttpMessageConverter
and use single UTF-8 StringHttpMessageConverter
.
Use this:
RestTemplate restTemplate = new RestTemplate();
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(StandardCharsets.UTF_8);
stringHttpMessageConverter.setWriteAcceptCharset(true);
for (int i = 0; i < restTemplate.getMessageConverters().size(); i++)
if (restTemplate.getMessageConverters().get(i) instanceof StringHttpMessageConverter)
restTemplate.getMessageConverters().remove(i);
restTemplate.getMessageConverters().add(i, stringHttpMessageConverter);
break;
I haven't observe such problem. In my case the Accept header had valueapplication/json, application/*+json
which is OK.
– Krzysztof Tomaszewski
Oct 5 '18 at 13:20
add a comment |
The answer by @mushfek0001 produce two StringHttpMessageConverter
which will send two text/plain
and */*
, such as the debug picture.
Even the Accept
header of client will be:
text/plain, text/plain, */*, */*
So the better one is remove the ISO-8859-1 StringHttpMessageConverter
and use single UTF-8 StringHttpMessageConverter
.
Use this:
RestTemplate restTemplate = new RestTemplate();
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(StandardCharsets.UTF_8);
stringHttpMessageConverter.setWriteAcceptCharset(true);
for (int i = 0; i < restTemplate.getMessageConverters().size(); i++)
if (restTemplate.getMessageConverters().get(i) instanceof StringHttpMessageConverter)
restTemplate.getMessageConverters().remove(i);
restTemplate.getMessageConverters().add(i, stringHttpMessageConverter);
break;
I haven't observe such problem. In my case the Accept header had valueapplication/json, application/*+json
which is OK.
– Krzysztof Tomaszewski
Oct 5 '18 at 13:20
add a comment |
The answer by @mushfek0001 produce two StringHttpMessageConverter
which will send two text/plain
and */*
, such as the debug picture.
Even the Accept
header of client will be:
text/plain, text/plain, */*, */*
So the better one is remove the ISO-8859-1 StringHttpMessageConverter
and use single UTF-8 StringHttpMessageConverter
.
Use this:
RestTemplate restTemplate = new RestTemplate();
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(StandardCharsets.UTF_8);
stringHttpMessageConverter.setWriteAcceptCharset(true);
for (int i = 0; i < restTemplate.getMessageConverters().size(); i++)
if (restTemplate.getMessageConverters().get(i) instanceof StringHttpMessageConverter)
restTemplate.getMessageConverters().remove(i);
restTemplate.getMessageConverters().add(i, stringHttpMessageConverter);
break;
The answer by @mushfek0001 produce two StringHttpMessageConverter
which will send two text/plain
and */*
, such as the debug picture.
Even the Accept
header of client will be:
text/plain, text/plain, */*, */*
So the better one is remove the ISO-8859-1 StringHttpMessageConverter
and use single UTF-8 StringHttpMessageConverter
.
Use this:
RestTemplate restTemplate = new RestTemplate();
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(StandardCharsets.UTF_8);
stringHttpMessageConverter.setWriteAcceptCharset(true);
for (int i = 0; i < restTemplate.getMessageConverters().size(); i++)
if (restTemplate.getMessageConverters().get(i) instanceof StringHttpMessageConverter)
restTemplate.getMessageConverters().remove(i);
restTemplate.getMessageConverters().add(i, stringHttpMessageConverter);
break;
edited Mar 31 '17 at 8:40
answered Mar 31 '17 at 8:35
zhoujizhouji
1,2651516
1,2651516
I haven't observe such problem. In my case the Accept header had valueapplication/json, application/*+json
which is OK.
– Krzysztof Tomaszewski
Oct 5 '18 at 13:20
add a comment |
I haven't observe such problem. In my case the Accept header had valueapplication/json, application/*+json
which is OK.
– Krzysztof Tomaszewski
Oct 5 '18 at 13:20
I haven't observe such problem. In my case the Accept header had value
application/json, application/*+json
which is OK.– Krzysztof Tomaszewski
Oct 5 '18 at 13:20
I haven't observe such problem. In my case the Accept header had value
application/json, application/*+json
which is OK.– Krzysztof Tomaszewski
Oct 5 '18 at 13:20
add a comment |
restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(StandardCharsets.UTF_16LE));
String response = restTemplate.getForObject(url, String.class);
Can you provide some context to your answer, that way future readers can learn how to apply it to their issues and not just in this situation.
– Newd
Jul 28 '15 at 14:28
add a comment |
restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(StandardCharsets.UTF_16LE));
String response = restTemplate.getForObject(url, String.class);
Can you provide some context to your answer, that way future readers can learn how to apply it to their issues and not just in this situation.
– Newd
Jul 28 '15 at 14:28
add a comment |
restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(StandardCharsets.UTF_16LE));
String response = restTemplate.getForObject(url, String.class);
restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(StandardCharsets.UTF_16LE));
String response = restTemplate.getForObject(url, String.class);
edited Jul 28 '15 at 9:24
answered Apr 1 '15 at 13:50
Ran AdlerRan Adler
2,2961821
2,2961821
Can you provide some context to your answer, that way future readers can learn how to apply it to their issues and not just in this situation.
– Newd
Jul 28 '15 at 14:28
add a comment |
Can you provide some context to your answer, that way future readers can learn how to apply it to their issues and not just in this situation.
– Newd
Jul 28 '15 at 14:28
Can you provide some context to your answer, that way future readers can learn how to apply it to their issues and not just in this situation.
– Newd
Jul 28 '15 at 14:28
Can you provide some context to your answer, that way future readers can learn how to apply it to their issues and not just in this situation.
– Newd
Jul 28 '15 at 14:28
add a comment |
Adding new StringHttpMessageConverter won't help.
In existing StringHttpMessageConverter we need to set writeAcceptCharset to false
and build httpheader with charset we want.
RestTemplate restTemplate = new RestTemplate();
List<HttpMessageConverter<?>> c = restTemplate.getMessageConverters();
for(HttpMessageConverter<?> mc :c)
if (mc instanceof StringHttpMessageConverter)
StringHttpMessageConverter mcc = (StringHttpMessageConverter) mc;
mcc.setWriteAcceptCharset(false);
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
headers.setAcceptCharset(Arrays.asList(Charset.forName("UTF-8")));
HttpEntity<String> entity = new HttpEntity<String>(jsonPayload, headers);
restTemplate.postForEntity(postResourceUrl, entity, String.class);
Adding new StringHttpMessageConverter helped.
– Alex78191
Apr 28 '18 at 12:25
add a comment |
Adding new StringHttpMessageConverter won't help.
In existing StringHttpMessageConverter we need to set writeAcceptCharset to false
and build httpheader with charset we want.
RestTemplate restTemplate = new RestTemplate();
List<HttpMessageConverter<?>> c = restTemplate.getMessageConverters();
for(HttpMessageConverter<?> mc :c)
if (mc instanceof StringHttpMessageConverter)
StringHttpMessageConverter mcc = (StringHttpMessageConverter) mc;
mcc.setWriteAcceptCharset(false);
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
headers.setAcceptCharset(Arrays.asList(Charset.forName("UTF-8")));
HttpEntity<String> entity = new HttpEntity<String>(jsonPayload, headers);
restTemplate.postForEntity(postResourceUrl, entity, String.class);
Adding new StringHttpMessageConverter helped.
– Alex78191
Apr 28 '18 at 12:25
add a comment |
Adding new StringHttpMessageConverter won't help.
In existing StringHttpMessageConverter we need to set writeAcceptCharset to false
and build httpheader with charset we want.
RestTemplate restTemplate = new RestTemplate();
List<HttpMessageConverter<?>> c = restTemplate.getMessageConverters();
for(HttpMessageConverter<?> mc :c)
if (mc instanceof StringHttpMessageConverter)
StringHttpMessageConverter mcc = (StringHttpMessageConverter) mc;
mcc.setWriteAcceptCharset(false);
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
headers.setAcceptCharset(Arrays.asList(Charset.forName("UTF-8")));
HttpEntity<String> entity = new HttpEntity<String>(jsonPayload, headers);
restTemplate.postForEntity(postResourceUrl, entity, String.class);
Adding new StringHttpMessageConverter won't help.
In existing StringHttpMessageConverter we need to set writeAcceptCharset to false
and build httpheader with charset we want.
RestTemplate restTemplate = new RestTemplate();
List<HttpMessageConverter<?>> c = restTemplate.getMessageConverters();
for(HttpMessageConverter<?> mc :c)
if (mc instanceof StringHttpMessageConverter)
StringHttpMessageConverter mcc = (StringHttpMessageConverter) mc;
mcc.setWriteAcceptCharset(false);
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
headers.setAcceptCharset(Arrays.asList(Charset.forName("UTF-8")));
HttpEntity<String> entity = new HttpEntity<String>(jsonPayload, headers);
restTemplate.postForEntity(postResourceUrl, entity, String.class);
answered Jan 26 '17 at 20:05
SandeshSandesh
612
612
Adding new StringHttpMessageConverter helped.
– Alex78191
Apr 28 '18 at 12:25
add a comment |
Adding new StringHttpMessageConverter helped.
– Alex78191
Apr 28 '18 at 12:25
Adding new StringHttpMessageConverter helped.
– Alex78191
Apr 28 '18 at 12:25
Adding new StringHttpMessageConverter helped.
– Alex78191
Apr 28 '18 at 12:25
add a comment |
Better you should remove the StringHttpMessageConverter first before adding new. so this way you will have one instance of StringHttpMessageConverter in our MessageConverters list.
RestTemplate restTemplate = new RestTemplate();
final Iterator<HttpMessageConverter<?>> iterator = restTemplate.getMessageConverters().iterator();
while (iterator.hasNext())
final HttpMessageConverter<?> converter = iterator.next();
if (converter instanceof StringHttpMessageConverter)
iterator.remove();
restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
add a comment |
Better you should remove the StringHttpMessageConverter first before adding new. so this way you will have one instance of StringHttpMessageConverter in our MessageConverters list.
RestTemplate restTemplate = new RestTemplate();
final Iterator<HttpMessageConverter<?>> iterator = restTemplate.getMessageConverters().iterator();
while (iterator.hasNext())
final HttpMessageConverter<?> converter = iterator.next();
if (converter instanceof StringHttpMessageConverter)
iterator.remove();
restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
add a comment |
Better you should remove the StringHttpMessageConverter first before adding new. so this way you will have one instance of StringHttpMessageConverter in our MessageConverters list.
RestTemplate restTemplate = new RestTemplate();
final Iterator<HttpMessageConverter<?>> iterator = restTemplate.getMessageConverters().iterator();
while (iterator.hasNext())
final HttpMessageConverter<?> converter = iterator.next();
if (converter instanceof StringHttpMessageConverter)
iterator.remove();
restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
Better you should remove the StringHttpMessageConverter first before adding new. so this way you will have one instance of StringHttpMessageConverter in our MessageConverters list.
RestTemplate restTemplate = new RestTemplate();
final Iterator<HttpMessageConverter<?>> iterator = restTemplate.getMessageConverters().iterator();
while (iterator.hasNext())
final HttpMessageConverter<?> converter = iterator.next();
if (converter instanceof StringHttpMessageConverter)
iterator.remove();
restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
answered Jan 18 '18 at 14:56
pankaj desaipankaj desai
265
265
add a comment |
add a comment |
Removing the existing converter, but with Java 8 (why do people still write Java 7 Code anyways?)
List<HttpMessageConverter<?>> converters = restTemplate.getMessageConverters();
converters .removeIf(httpMessageConverter -> httpMessageConverter instanceof StringHttpMessageConverter);
messageConverters
.add(0, new StringHttpMessageConverter(StandardCharsets.UTF_8));
add a comment |
Removing the existing converter, but with Java 8 (why do people still write Java 7 Code anyways?)
List<HttpMessageConverter<?>> converters = restTemplate.getMessageConverters();
converters .removeIf(httpMessageConverter -> httpMessageConverter instanceof StringHttpMessageConverter);
messageConverters
.add(0, new StringHttpMessageConverter(StandardCharsets.UTF_8));
add a comment |
Removing the existing converter, but with Java 8 (why do people still write Java 7 Code anyways?)
List<HttpMessageConverter<?>> converters = restTemplate.getMessageConverters();
converters .removeIf(httpMessageConverter -> httpMessageConverter instanceof StringHttpMessageConverter);
messageConverters
.add(0, new StringHttpMessageConverter(StandardCharsets.UTF_8));
Removing the existing converter, but with Java 8 (why do people still write Java 7 Code anyways?)
List<HttpMessageConverter<?>> converters = restTemplate.getMessageConverters();
converters .removeIf(httpMessageConverter -> httpMessageConverter instanceof StringHttpMessageConverter);
messageConverters
.add(0, new StringHttpMessageConverter(StandardCharsets.UTF_8));
answered Mar 14 at 12:21
Michel JungMichel Jung
1,1601325
1,1601325
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f29392422%2fhow-can-i-tell-resttemplate-to-post-with-utf-8-encoding%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown