How to set UTF-8 character encoding in Spring boot?

前端 未结 4 1265
被撕碎了的回忆
被撕碎了的回忆 2020-12-16 15:36

I use spring-boot in my project, and I run this jar file which is built by spring-boot as a service on Centos. When I run this service:

service myApp

相关标签:
4条回答
  • 2020-12-16 16:09

    Based on my answer here: https://stackoverflow.com/a/48952844/986160 you need to run:

    mvn spring-boot:run -Drun.jvmArguments="-Dfile.encoding=UTF-8"
    
    0 讨论(0)
  • 2020-12-16 16:18

    Finally I found a way so that I can launch Spring-boot project properly.

    The solution is use sudo /etc/init.d/myApp start instead of service myApp start

    0 讨论(0)
  • 2020-12-16 16:24

    SOLUTION

    Add to config file (for application /var/app/app.jar it will be /var/app/app.conf) below line:

    export LANG='en_US.UTF-8'
    

    CAUSE

    The problem is in linux systemd (service). I tested it with script /etc/init.d/test:

    #/bin/bash
    locale
    

    result of command $ /etc/init.d/test:

    LANG=en_US.UTF-8
    LC_CTYPE="en_US.UTF-8"
    LC_NUMERIC="en_US.UTF-8"
    LC_TIME="en_US.UTF-8"
    LC_COLLATE="en_US.UTF-8"
    LC_MONETARY="en_US.UTF-8"
    LC_MESSAGES="en_US.UTF-8"
    LC_PAPER="en_US.UTF-8"
    LC_NAME="en_US.UTF-8"
    LC_ADDRESS="en_US.UTF-8"
    LC_TELEPHONE="en_US.UTF-8"
    LC_MEASUREMENT="en_US.UTF-8"
    LC_IDENTIFICATION="en_US.UTF-8"
    LC_ALL=
    

    and result of command $ service test:

    LANG=
    LC_CTYPE="POSIX"
    LC_NUMERIC="POSIX"
    LC_TIME="POSIX"
    LC_COLLATE="POSIX"
    LC_MONETARY="POSIX"
    LC_MESSAGES="POSIX"
    LC_PAPER="POSIX"
    LC_NAME="POSIX"
    LC_ADDRESS="POSIX"
    LC_TELEPHONE="POSIX"
    LC_MEASUREMENT="POSIX"
    LC_IDENTIFICATION="POSIX"
    LC_ALL=
    

    In spring boot jar file is build-in systemd script and before running spring boot is executed among others:

    # Source any config file
    configfile="$(basename "${jarfile%.*}.conf")"
    
    # Initialize CONF_FOLDER location defaulting to jarfolder
    [[ -z "$CONF_FOLDER" ]] && CONF_FOLDER="${jarfolder}"
    
    # shellcheck source=/dev/null
    [[ -r "${CONF_FOLDER}/${configfile}" ]] && source "${CONF_FOLDER}/${configfile}"
    
    0 讨论(0)
  • 2020-12-16 16:28

    The first line tells you that you should set system property 'file.encoding'. I think that's the problem. Please see: Setting the default Java character encoding?

    0 讨论(0)
提交回复
热议问题