question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

The log contains Chinese, which is not displayed correctly.

See original GitHub issue

Description

When print Chinese in Spring Boot projects, then looks like not right, for example:

{"code":45,"name":"广西壮族自治区"}

Expected Behavior

Print Chinese normal, like this:

{"code":45,"name":"广西壮族自治区"}

Actual Behavior

{"code":45,"name":"广西壮族自治区"}

Possible Fix

Must force set utf-8 encoding

server:
  servlet:
    encoding:
      enabled: true
      charset: UTF-8
      force: true

The root cause of this problem

image

The priority order for specifying the response body is:

  1. explicitly per request using setCharacterEncoding and setContentType
  2. implicitly per request using setLocale
  3. per web application via the deployment descriptor or ServletContext.setRequestCharacterEncoding(String)
  4. container default via vendor specific configuration
  5. ISO-8859-1

If any possible, get encoding form somewhere else. image

Your Environment

  • Version used: org.zalando:logbook-spring-boot-starter:2.3.0 & Spring Boot Ver: 2.3.4.RELEASE

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:4
  • Comments:15

github_iconTop GitHub Comments

1reaction
sohtsukacommented, Jul 21, 2021

@whiskeysierra Is it possible to reconsider the fix? As @kyle18th commented, Spring Boot does not provide the (legal) way to set the response charset when the content type is application/json.

This controller:

@RestController
public class Controller {
    @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public List<String> demo() {
	// "world" in Chinese characters: https://en.wiktionary.org/wiki/%E4%B8%96%E7%95%8C
	return List.of("世界");
    }
}

produces the UTF-8 JSON response below. It works without setting the response charset.

["世界"]

However, in the Logbook’s log message, the response body is garbled.

2021-07-21 19:38:59.398 TRACE 11671 --- [nio-8080-exec-1] org.zalando.logbook.Logbook              : {"origin":"local","type":"response","correlation":"ce55b6230e12728d","duration":437,"protocol":"HTTP/1.1","status":200,"headers":{"Connection":["keep-alive"],"Content-Type":["application/json"],"Date":["Wed, 21 Jul 2021 19:38:59 GMT"],"Keep-Alive":["timeout=60"],"Transfer-Encoding":["chunked"]},"body":["ããã«ã¡ã¯"]}

This seems to be because Logbook uses the fallback encoding (ISO-8859-1) even if the document is JSON.

Setting the response character encoding explictly:

@GetMapping(produces = MediaType.APPLICATION_JSON_UTF8_VALUE)

fixes the log message.

2021-07-21 19:59:25.295 TRACE 12606 --- [nio-8080-exec-4] org.zalando.logbook.Logbook              : {"origin":"local","type":"response","correlation":"ac7dc50a7cf0fcd0","duration":5,"protocol":"HTTP/1.1","status":200,"headers":{"Connection":["keep-alive"],"Content-Type":["application/json;charset=UTF-8"],"Date":["Wed, 21 Jul 2021 19:59:25 GMT"],"Keep-Alive":["timeout=60"],"Transfer-Encoding":["chunked"]},"body":["世界"]}

But APPLICATION_JSON_UTF8_VALUE is deprecated and should not be used.

0reactions
kmastercommented, Nov 29, 2022

set the response encoding in the previous filter:

response.setCharacterEncoding(“utf-8”);

Read more comments on GitHub >

github_iconTop Results From Across the Web

Chinese characters not displaying correctly
You can have this checked by pressing the Windows button + R on your keyboard to open the Run box, then type winver...
Read more >
utf 8 - unable to display chinese characters in error_log
I have a php script that pump chinese characters into stdout via error_log. It goes like this: error_log(">>>>>>>>>>article imported: ".
Read more >
Chinese characters are not displayed correctly - CodeRanch
However, in the tomcat console, the characters are not displayed properly, instead of chinese characters, ??? are displayed.
Read more >
Simplified Chinese characters are not displayed correctly in ...
This started when I checked 'Beta: use Unicode UTF-8 for the support of world langauges' in Change system locale, Country or Region in...
Read more >
Doesn't display GB2312 encoded texts correctly for Chinese ...
Now some emails are not displayed correctly. ... sure that charsetalias.properties has no mappings with GB2312 on the right-hand side of the equals...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found