Add UTF-8 encoding to ResponseEntity

stringhttpmessageconverter utf-8
responseentity error message
convert responseentity to json object
responseentity<void
mock(responseentity body)
responseentity return json
convert special characters to utf-8 java
resttemplate request utf 8

I have a responseEntity like this one:

HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
requestHeaders.setContentType(MediaType.valueOf("text/plain;charset=UTF-8"));

HttpEntity requestEntity = new HttpEntity(jsonQuery, requestHeaders);
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);

//Not in UTF-8!!
logger.debug("result: " + responseEntity.getBody());

But it's not encoded in UTF-8 and produces characters like this: Soci굩 . Is it possible to make it in UTF-8 or add the encoding somehow in restTemplate?

responseHeaders.add("Content-Type", "text/html; charset=utf-8");

Java Spring resttemplate character encoding, i tried to use ResponseEntity<String> and setting up content-Type but that is also that's why i need to set character encoding in the response. ResponseEntity represents an HTTP response, including headers, body, and status. While @ResponseBody puts the return value into the body of the response, ResponseEntity also allows us to add headers and status code.

The solution was to add a StringHttpMessageConverter with utf-8 in restTemplate bean like:

@Bean
public RestTemplate restTemplate() {
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.getMessageConverters()
            .add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
    return restTemplate;
}

setting content type for json response with @responsebody (Spring , ResponseEntity. ResponseEntity represents an HTTP response, including headers, body, and status. sourceEncoding>UTF-8</project.build. ResponseEntity. ResponseEntity represents an HTTP response, including headers, body, and status. While @ResponseBody puts the return value into the body of the response, ResponseEntity also allows us to add headers and status code. Spring Boot ResponseEntity example. In the following application, we demonstrate the usage of ResponseEntity.

I have solved this problem. I need to POST a string object in request body with UTF-8.

text/plain

httpHeaders.setContentType(new MediaType("text", "plain", StandardCharsets.UTF_8));

applicaton/json

httpHeaders.setContentType(new MediaType("applicaton", "json", StandardCharsets.UTF_8));

RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> resposeEntity = null;

HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(new MediaType("text", "plain", StandardCharsets.UTF_8));

HttpEntity<String> httpEntity = new HttpEntity<String>(stringContent, httpHeaders);
responseEntity = restTemplate.exchange(requestUrl, HttpMethod.POST, httpEntity, String.class);

if (HttpStatus.OK.equals(responseEntity.getStatusCode())) {
    logger.debug("... success ... result: " + responseEntity.getBody());
}

Spring Boot ResponseEntity tutorial, rest template with object mapping example code in utf8 communication import java.nio.charset.Charset; httpMessageConverter.add( stringHttpMessageConverter); ResponseEntity<List<LocationResponse>> responseEntity  (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.

I've solved the same problem just adding charset=utf8 to my RequestMapping.

Before

@RequestMapping(path = "/{id}", method = RequestMethod.GET, produces = "application/hal+json")

After

@RequestMapping(path = "/{id}", method = RequestMethod.GET, produces = "application/hal+json;charset=utf8")

I add this to my other methods too. I hope this helps.

rest template with object mapping example code in utf8 , POST, consumes="application/json") public ResponseEntity<Void> the format I gave. Any ideas on how I can set the charset to be UTF-8 for the entire project? On the page encoding UTF-8. But even if she were different, in any case there would be no question marks. Please advise how to correct the situation. I will add the question: It turned out very strange in this situation:

Java Spring resttemplate character encoding, With most view technologies Spring MVC will first try to determine the MIME type of the response, including the character encoding, and set it in the HTTP header   I' ve a Spring MVC bean and I would like to return turkish character by setting encoding UTF-8. but although my string is "şŞğĞİıçÇöÖüÜ" it returns as "?????çÇöÖüÜ". and also when I look at the response page, which is internet explorer page, encoding is western european iso, not UTF-8. Here is the code:

How to make spring boot default to application/json;charset=utf-8 , Set; import javax.security.auth. POST, produces = "application/json; charset=utf -8") public ResponseEntity<ApplicationResponse> login(HttpServletRequest  If an application want to explicitly disregard Encoding limitations (to read in JSON encoded using an encoding not listed as allowed), they can use java.io.Reader/ java.io.Writer instances as input You may should also always add the produces and consumes elements the @RequestMapping when it is applicable, You can also try adding

UTF-8 encoding a JSP with Spring MVC, when interface set produces=text/plain;charset=utf-8. and consumers set entity = new HttpEntity<>(headers); + ResponseEntity<String>  ResponseEntity represents the whole HTTP response: status code, headers, and body.Because of it, we can use it to fully configure the HTTP response. If we want to use it, we have to return it from the endpoint; Spring takes care of the rest.

Comments
  • Request headers, not response headers? Shouldn't request headers be unmodifiable? I mean, they're sent by the client and shouldn't be allowed to be changed!
  • down vote because of changing the question and eliminating the original question. you could ask the second question as an UPDATE or a new question
  • Code-only answers are considered low quality: make sure to provide an explanation what your code does and how it solves the problem. It will help the asker and future readers both if you can add more information in your post. See also Explaining entirely code-based answers: meta.stackexchange.com/questions/114762/…