0

UTF-8 character encoding is not working for my spring-based application after moving the server to AWS Elasticbeanstalk Tomcat 8.

I have referred steps from this link.

And below things, I have tried:

  • Set URIEncoding="UTF-8" on your <Connector> in server.xml. References: HTTP Connector, AJP Connector.
  • Change all your pages to include charset name using - <meta charset="UTF-8" /> .
  • Used CharsetEncodingFilter in web.xml.
  • JVM Options to tomcat -Dfile.encoding=UTF8 and -Djavax.servlet.request.encoding=UTF8

For AWS Elastibeanstalk tomcat JVM arguments,

{
            "Namespace": "aws:elasticbeanstalk:container:tomcat:jvmoptions",
            "OptionName": "JVM Options",
            "Value": "-Dfile.encoding=UTF8 -Djavax.servlet.request.encoding=UTF-8"
}

In web.xml (This filter configuration is not changed. It was same in the old server as well.)

<filter>
        <filter-name>characterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
</filter>

<filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
</filter-mapping>

In .ebextensions/server.xml

<Server port="8005" shutdown="SHUTDOWN">
        ...
        <Service name="Catalina">
                ...
                <Connector port="8080" protocol="HTTP/1.1"
                   URIEncoding="UTF-8"
                   redirectPort="8443" />

                <Connector port="8009" protocol="AJP/1.3"
                   URIEncoding="UTF-8"
                   redirectPort="8443" />
                ...
        </Service>
        ...
</Server>

In .ebextensions/httpd.conf

AddDefaultCharset utf-8

Ideally, it should work with these possible changes but I am getting junk characters for special characters for languages like Russian, Bulgarian, etc.

As a work-around as of now, I am using the following snippet wherever required in the application:

public String convertToUTF(String value) {
        return StringUtils.isEmpty(value) ? value
                : new String(value.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
}

Please suggest where I am missing any configuration here. Thanks in advance.

Note: Character encoding issue is coming after moving to AWS Elastibeanstalk Tomcat8 Linux Server.

Jacks
  • 51
  • 1
  • 1
  • 6

2 Answers2

0

Hi do like this in the controller class.

@RequestMapping(value = "/add", method = RequestMethod.POST,produces = "application/json; charset=utf-8")

public @ResponseBody DeclareExamMessage addComplaint(HttpServletRequest request) {
 try {

request.setCharacterEncoding("UTF-8");
//map the request object and pass it to sava method 
}

 }

After applying above changes if it is not working change your database uft8 character set to utf8mb4

and refer the below url

Trouble with UTF-8 characters; what I see is not what I stored

https://medium.com/@adamhooper/in-mysql-never-use-utf8-use-utf8mb4-11761243e434

iragond
  • 60
  • 6
  • Thank you for your suggestion. I have already had this DB handling for `utf8mb4` and set the character encoding for the request is handled by `CharacterEncodingFilter`. As I said, the application was working fine earlier, it is not working after migrating server to AWS. Please find the note at the end of issue description. – Jacks Jul 25 '19 at 15:19
  • which is the environment do you have on AWS Linux or windows if you have Linux Make sure to set the client and server character set as well. I have the following in my MySQL configuration file (/etc/my.cnf): [client] default-character-set = utf8mb4 [mysql] default-character-set = utf8mb4 [mysqld] character-set-client-handshake = FALSE character-set-server = utf8mb4 collation-server = utf8mb4_unicode_ci Refer link- https://mathiasbynens.be/notes/mysql-utf8mb4 – iragond Jul 26 '19 at 08:03
0

I was able to resolve this by adding the following to my application.properties resource

server.servlet.encoding.charset=UTF-8
server.servlet.encoding.force=true