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>
inserver.xml
. References: HTTP Connector, AJP Connector. - Change all your pages to include charset name using -
<meta charset="UTF-8" />
. - Used
CharsetEncodingFilter
inweb.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.