logbook: The log contains Chinese, which is not displayed correctly.
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
The priority order for specifying the response body is:
- explicitly per request using setCharacterEncoding and setContentType
- implicitly per request using setLocale
- per web application via the deployment descriptor or ServletContext.setRequestCharacterEncoding(String)
- container default via vendor specific configuration
- ISO-8859-1
If any possible, get encoding form somewhere else.
Your Environment
- Version used:
org.zalando:logbook-spring-boot-starter:2.3.0
& Spring Boot Ver: 2.3.4.RELEASE
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 6
- Comments: 36 (15 by maintainers)
Commits related to this issue
- #870 use UTF-8 as a fallback for JSON content type — committed to zalando/logbook by kasmarian a year ago
- Use UTF-8 as a fallback for JSON content type (#1521) * #870 use UTF-8 as a fallback for JSON content type * #1500 recognize case-insensitive content-type headers * Refactor json media type met... — committed to zalando/logbook by kasmarian a year ago
- Use UTF-8 as a fallback for JSON content type (#1521) * #870 use UTF-8 as a fallback for JSON content type * #1500 recognize case-insensitive content-type headers * Refactor json media type met... — committed to zalando/logbook by kasmarian a year ago
Hi @kasmarian just sharing a small app to help you reproduce the problem. In the README.md I’ve shared two links to the different API paths (one working and other not working) being the only difference the usage of Thymeleaf.
Project Link
The PR is merged and will be available in the next release. I’d like to resolve this issue. Feel free to reopen it, if it’s not fully addressed.
I think we can do it this week. Let me chat with the team about it
ok, another fix is merged. Hope this one doesn’t break anything else 😄
Hi @kasmarian
I’ve check your PR and I believe that will fix our use-cases.
Thank you very much for your effort solving this problem
@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:
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.
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:
fixes the log message.
But
APPLICATION_JSON_UTF8_VALUE
is deprecated and should not be used.