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













27















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);









share|improve this question


























    27















    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);









    share|improve this question
























      27












      27








      27


      11






      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);









      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Apr 1 '15 at 13:43









      anssiasanssias

      7842818




      7842818






















          7 Answers
          7






          active

          oldest

          votes


















          76














          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")));





          share|improve this answer


















          • 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



















          8














          (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);





          share|improve this answer

























          • 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


















          6














          The answer by @mushfek0001 produce two StringHttpMessageConverter which will send two text/plain and */*, such as the debug picture.



          enter image description here



          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;







          share|improve this answer

























          • 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


















          1














          restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(StandardCharsets.UTF_16LE));

          String response = restTemplate.getForObject(url, String.class);





          share|improve this answer

























          • 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


















          0














          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);





          share|improve this answer























          • Adding new StringHttpMessageConverter helped.

            – Alex78191
            Apr 28 '18 at 12:25


















          0














          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")));





          share|improve this answer






























            0














            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));





            share|improve this answer






















              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
              );



              );













              draft saved

              draft discarded


















              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









              76














              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")));





              share|improve this answer


















              • 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
















              76














              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")));





              share|improve this answer


















              • 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














              76












              76








              76







              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")));





              share|improve this answer













              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")));






              share|improve this answer












              share|improve this answer



              share|improve this answer










              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













              • 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














              8














              (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);





              share|improve this answer

























              • 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















              8














              (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);





              share|improve this answer

























              • 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













              8












              8








              8







              (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);





              share|improve this answer















              (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);






              share|improve this answer














              share|improve this answer



              share|improve this answer








              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 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 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 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











              6














              The answer by @mushfek0001 produce two StringHttpMessageConverter which will send two text/plain and */*, such as the debug picture.



              enter image description here



              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;







              share|improve this answer

























              • 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















              6














              The answer by @mushfek0001 produce two StringHttpMessageConverter which will send two text/plain and */*, such as the debug picture.



              enter image description here



              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;







              share|improve this answer

























              • 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













              6












              6








              6







              The answer by @mushfek0001 produce two StringHttpMessageConverter which will send two text/plain and */*, such as the debug picture.



              enter image description here



              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;







              share|improve this answer















              The answer by @mushfek0001 produce two StringHttpMessageConverter which will send two text/plain and */*, such as the debug picture.



              enter image description here



              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;








              share|improve this answer














              share|improve this answer



              share|improve this answer








              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 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
















              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











              1














              restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(StandardCharsets.UTF_16LE));

              String response = restTemplate.getForObject(url, String.class);





              share|improve this answer

























              • 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















              1














              restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(StandardCharsets.UTF_16LE));

              String response = restTemplate.getForObject(url, String.class);





              share|improve this answer

























              • 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













              1












              1








              1







              restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(StandardCharsets.UTF_16LE));

              String response = restTemplate.getForObject(url, String.class);





              share|improve this answer















              restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(StandardCharsets.UTF_16LE));

              String response = restTemplate.getForObject(url, String.class);






              share|improve this answer














              share|improve this answer



              share|improve this answer








              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

















              • 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











              0














              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);





              share|improve this answer























              • Adding new StringHttpMessageConverter helped.

                – Alex78191
                Apr 28 '18 at 12:25















              0














              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);





              share|improve this answer























              • Adding new StringHttpMessageConverter helped.

                – Alex78191
                Apr 28 '18 at 12:25













              0












              0








              0







              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);





              share|improve this answer













              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);






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jan 26 '17 at 20:05









              SandeshSandesh

              612




              612












              • 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





              Adding new StringHttpMessageConverter helped.

              – Alex78191
              Apr 28 '18 at 12:25











              0














              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")));





              share|improve this answer



























                0














                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")));





                share|improve this answer

























                  0












                  0








                  0







                  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")));





                  share|improve this answer













                  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")));






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 18 '18 at 14:56









                  pankaj desaipankaj desai

                  265




                  265





















                      0














                      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));





                      share|improve this answer



























                        0














                        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));





                        share|improve this answer

























                          0












                          0








                          0







                          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));





                          share|improve this answer













                          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));






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Mar 14 at 12:21









                          Michel JungMichel Jung

                          1,1601325




                          1,1601325



























                              draft saved

                              draft discarded
















































                              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.




                              draft saved


                              draft discarded














                              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





















































                              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







                              Popular posts from this blog

                              Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                              SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                              은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현