Search code examples
spring-bootdockerspring-data-r2dbc

umlauts not working when running spring app in docker container


I have this test code in a service:

import io.r2dbc.postgresql.codec.Json

// ...

logger.info("test")
val x = "{\"x\": \"ü\"}"
logger.info(x)

val typeRef = object : TypeReference<LinkedHashMap<String, String>>() {}

val xParsed = objectMapper.readValue(x, typeRef)
if (xParsed["x"] == "ü") {
  logger.info("parsed ue correctly")
} else {
  logger.info("converted ue incorrect")
}

val xJ = Json.of(x)
val xConverted = objectMapper.readValue(xJ.asString(), typeRef)
logger.info(xConverted["x"])
if (xConverted["x"] == "ü") {
  logger.info("converted ue correctly")
} else {
  logger.info("converted ue incorrect")
}

When run from the IDE, it works as expected. This is the log output:

2021-08-11 14:21:16.237  INFO 25810 --- [tor-tcp-epoll-1] some.feature.serviceImpl      : test
2021-08-11 14:21:16.237  INFO 25810 --- [tor-tcp-epoll-1] some.feature.serviceImpl      : {"x": "ü"}
2021-08-11 14:21:16.237  INFO 25810 --- [tor-tcp-epoll-1] some.feature.serviceImpl      : parsed ue correctly
2021-08-11 14:21:16.238  INFO 25810 --- [tor-tcp-epoll-1] some.feature.serviceImpl      : ü
2021-08-11 14:21:16.238  INFO 25810 --- [tor-tcp-epoll-1] some.feature.serviceImpl      : converted ue correctly

When I run it from a docker container after building the image with ./gradlew bootBuildImage the output of the container is:

backend_1  | 2021-08-11 12:25:28.905  INFO 9 --- [tor-tcp-epoll-1] some.feature.serviceImpl      : test
backend_1  | 2021-08-11 12:25:28.905  INFO 9 --- [tor-tcp-epoll-1] some.feature.serviceImpl      : {"x": "?"}
backend_1  | 2021-08-11 12:25:28.906  INFO 9 --- [tor-tcp-epoll-1] some.feature.serviceImpl      : parsed ue correctly
backend_1  | 2021-08-11 12:25:28.906  INFO 9 --- [tor-tcp-epoll-1] some.feature.serviceImpl      : ??
backend_1  | 2021-08-11 12:25:28.906  INFO 9 --- [tor-tcp-epoll-1] some.feature.serviceImpl      : converted ue incorrect

As the log shows, the first part is fine, but after the Json conversion the umlaut changed. Why and how can I fix this?


Solution

  • Turns out setting the env var JAVA_OPTS to -Dfile.encoding=UTF8 fixes it. But I still don't understand why other services work fine that do not use a manual json conversion.

    E.g. in docker-compose.yml

      my_service:
        image: the_image
        ports:
          - 8080:8080
        environment:
          JAVA_OPTS: "-Dfile.encoding=UTF8"