[英]UTF-8 character encoding is not working on AWS Elasticbeanstalk Tomcat8 server for spring application
UTF-8 character encoding is not working for my spring-based application after moving the server to AWS Elasticbeanstalk Tomcat 8. UTF-8 在将服务器移动到 AWS Elasticbeanstalk Tomcat 后,字符编码不适用于我基于 spring 的应用程序 8.
I have referred steps from this link .我已经参考了此链接中的步骤。
And below things, I have tried:在下面的事情中,我尝试过:
URIEncoding="UTF-8"
on your <Connector>
in server.xml
.server.xml
的<Connector>
上设置URIEncoding="UTF-8"
。 References: HTTP Connector, AJP Connector.<meta charset="UTF-8" />
.<meta charset="UTF-8" />
更改所有页面以包含字符集名称。CharsetEncodingFilter
in web.xml
.CharsetEncodingFilter
中使用了web.xml
。-Dfile.encoding=UTF8
and -Djavax.servlet.request.encoding=UTF8
-Dfile.encoding=UTF8
和-Djavax.servlet.request.encoding=UTF8
For AWS Elastibeanstalk tomcat JVM arguments,对于 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.) In
web.xml
(这个过滤配置没变,老服务器也一样)
<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
在
.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
在
.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.注意:字符编码问题在移动到 AWS Elastibeanstalk Tomcat8 Linux 服务器后出现。
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 应用以上更改后,如果它不起作用,请将数据库的uft8字符集更改为utf8mb4
and refer the below url 并参考以下网址
Trouble with UTF-8 characters; UTF-8字符有问题; what I see is not what I stored
我看到的不是我存储的
https://medium.com/@adamhooper/in-mysql-never-use-utf8-use-utf8mb4-11761243e434 https://medium.com/@adamhooper/in-mysql-never-use-utf8-use-utf8mb4-11761243e434
I was able to resolve this by adding the following to my application.properties
resource我能够通过将以下内容添加到我的
application.properties
资源来解决这个问题
server.servlet.encoding.charset=UTF-8
server.servlet.encoding.force=true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.