Affects: \


version : see Springboot version on the below picture Spring use Resttemplate occur mijobake Chinese characters

When you use Spring Boot mvc, provide a endpoint,

request the endpoint with this content Hello, this is a text/plain message.<h1>语言</h1> ,

maybe confront mojibake chinese characters.

It's not the problem of Spring Boot, but what the client you use.

result form is below.

server receive content-type client Content-Type client Accept-Charset lib use worked?
application/json; charset=UTF-8 application/json charset=UTF-8 RestTemplate Yes
application/json; charset=UTF-8 application/json; charset=UTF-8 RestTemplate Yes
application/json; charset=UTF-8 application/json charset=UTF-8 HttpClient Yes
application/json; charset=UTF-8 application/json; charset=UTF-8 HttpClient Yes
text/plain; charset=UTF-8 application/json charset=UTF-8 RestTemplate No
text/plain; charset=UTF-8 application/json; charset=UTF-8 RestTemplate Yes
text/plain; charset=UTF-8 application/json charset=UTF-8 HttpClient Yes
text/plain; charset=UTF-8 application/json; charset=UTF-8 HttpClient Yes

finally conclusion about the form , only when you use RestTemplate, and separately set the Content-Type

and Accept-Charset, the problem will occur.

Comment From: Halcyon666

// 创建RestTemplate
        RestTemplate restTemplate = new RestTemplate();

        // 设置请求头
        HttpHeaders headers = new HttpHeaders();

        // this doesn't work
//        headers.setContentType(MediaType.TEXT_PLAIN);
//        headers.set("Accept-Charset", StandardCharsets.UTF_8.name());
        // same to above
       headers.set("Content-Type", "application/json");
        headers.set("Accept-Charset", "UTF-8");



        // 设置请求体
        HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, headers);

        // 发送请求并获取响应
        ResponseEntity<String> responseEntity = restTemplate.exchange(
                url,
                HttpMethod.POST,
                requestEntity,
                String.class
        );

Comment From: snicoll

@Halcyon666 I am a bit confused about your report. Are you saying that when you send a text it's garbled and when you send a json file it isn't? Can you please move the screenshot and code in text into something that we can run ourselves?

You can attach a zip to this issue or push the code to a separate GitHub repository.

Comment From: Halcyon666

sorry about that confusion.

The test code is here. A Controller https://raw.githubusercontent.com/Halcyon666/learn-cases/main/learncases/src/main/java/com/whalefall/learncases/mojibake/MojibakeController.java and the RestTemplate Client https://raw.githubusercontent.com/Halcyon666/learn-cases/main/learncases/src/main/java/com/whalefall/learncases/mojibake/RestTemplateExample.java.

If setting the headers like this way

// wrong way 1: this doesn't work // headers.setContentType(MediaType.TEXT_PLAIN); headers.set("Accept-Charset", StandardCharsets.UTF_8.name());

// another way same to above // headers.set("Content-Type", "application/json"); headers.set("Accept-Charset", "UTF-8");

The Chinese character will be strange gibberish like “?”.

Wish clearly describe the problem.

Comment From: poutsma

HTTP clients send a Accept-Charset header to specify the character set that are available client-side, so that the server can use a supported character set. It has nothing to do with specifying the character set of the request content, which is done by setting the content type, as you do here.