From b119316bf4705f97a68c93bc46406970ec24797b Mon Sep 17 00:00:00 2001 From: Tapan Avasthi Date: Sun, 2 Jul 2023 21:17:44 +0530 Subject: [PATCH] BAEL-6647: How to handle character encoding in JSON POST Request (#14335) Co-authored-by: Tapan Avasthi --- .../resttemplate/RestTemplateApplication.java | 16 +++++ ...stTemplatePostRequestEncodingLiveTest.java | 59 +++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 spring-web-modules/spring-resttemplate-2/src/main/java/com/baeldung/resttemplate/RestTemplateApplication.java create mode 100644 spring-web-modules/spring-resttemplate-2/src/test/java/com/baeldung/resttemplate/postjson/RestTemplatePostRequestEncodingLiveTest.java diff --git a/spring-web-modules/spring-resttemplate-2/src/main/java/com/baeldung/resttemplate/RestTemplateApplication.java b/spring-web-modules/spring-resttemplate-2/src/main/java/com/baeldung/resttemplate/RestTemplateApplication.java new file mode 100644 index 0000000000..ca1e141d44 --- /dev/null +++ b/spring-web-modules/spring-resttemplate-2/src/main/java/com/baeldung/resttemplate/RestTemplateApplication.java @@ -0,0 +1,16 @@ +package com.baeldung.resttemplate; + +import java.util.Collections; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class RestTemplateApplication { + + public static void main(String[] args) { + SpringApplication app = new SpringApplication(RestTemplateApplication.class); + app.setDefaultProperties(Collections.singletonMap("server.servlet.encoding.charset", "ISO-8859-1")); + app.run(args); + } +} diff --git a/spring-web-modules/spring-resttemplate-2/src/test/java/com/baeldung/resttemplate/postjson/RestTemplatePostRequestEncodingLiveTest.java b/spring-web-modules/spring-resttemplate-2/src/test/java/com/baeldung/resttemplate/postjson/RestTemplatePostRequestEncodingLiveTest.java new file mode 100644 index 0000000000..803ab96ba4 --- /dev/null +++ b/spring-web-modules/spring-resttemplate-2/src/test/java/com/baeldung/resttemplate/postjson/RestTemplatePostRequestEncodingLiveTest.java @@ -0,0 +1,59 @@ +package com.baeldung.resttemplate.postjson; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; + +import org.json.JSONException; + +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.web.client.RestTemplate; + +import com.baeldung.resttemplate.RestTemplateApplication; +import com.baeldung.resttemplate.web.dto.Person; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = RestTemplateApplication.class) +public class RestTemplatePostRequestEncodingLiveTest { + private static String createPersonUrl; + private static RestTemplate restTemplate; + + @BeforeClass + public static void runBeforeAllTestMethods() throws JSONException { + createPersonUrl = "http://localhost:8080/spring-rest/createPerson"; + restTemplate = new RestTemplate(); + } + + @Test + public void givenJapaneseNameInDataWithoutHeaderEncoding_whenDataIsPostedByPostForObject_thenSaveIncorrectly() { + Person japanese = new Person(100, "関連当"); + + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + HttpEntity request = new HttpEntity<>(japanese, headers); + + Person person = restTemplate.postForObject(createPersonUrl, request, Person.class); + assertNotNull(person); + assertNotEquals("関連当", person.getName()); + } + + @Test + public void givenJapaneseNameInDataWithHeaderEncoding_whenDataIsPostedByPostForObject_thenSaveCorrectly() { + Person japanese = new Person(100, "関連当"); + + HttpHeaders headers = new HttpHeaders(); + headers.set("Content-type", "application/json;charset=UTF-8"); + HttpEntity request = new HttpEntity<>(japanese, headers); + + Person person = restTemplate.postForObject(createPersonUrl, request, Person.class); + assertNotNull(person); + assertEquals("関連当", person.getName()); + } +} \ No newline at end of file