2018-12-10

Spring controller submit chinese character into DB via JPA.






Web page chinese character to tomcat via Spring



Solved by solution below:
https://stackoverflow.com/questions/4318664/save-chinese-characters-with-spring-mvc-java





<filter>
    <filter-name>charsetFilter</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>
</filter>

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






Tomcat Java to MySql

SHOW VARIABLES LIKE "%version%";


mysql> SHOW VARIABLES LIKE "%version%";
+-------------------------+------------------------------+
| Variable_name           | Value                        |
+-------------------------+------------------------------+
| innodb_version          | 5.7.23                       |
| protocol_version        | 10                           |
| slave_type_conversions  |                              |
| tls_version             | TLSv1,TLSv1.1                |
| version                 | 5.7.23                       |
| version_comment         | MySQL Community Server (GPL) |
| version_compile_machine | x86_64                       |
| version_compile_os      | Linux                        |
+-------------------------+------------------------------+
8 rows in set (0.00 sec)




Normally with this would be enough to solve the encoding

https://stackoverflow.com/questions/4782077/execute-mysql-query-contains-chinese-character-using-java-program

Try connecting with following switch
 String url = "jdbc:mysql://host/database?characterSetResults=UTF-8&characterEncoding=UTF-8&useUnicode=yes";




But I'm using JPA

https://stackoverflow.com/questions/18163328/jpa-utf-8-characters-not-persisted
Solved by solution below:
use the character encoding in the property of persistence.xml file
<property name="javax.persistence.jdbc.url" 
   value="jdbc:mysql://localhost:3306/blogdatabase?useUnicode=yes&amp;characterEncoding=UTF-8"/>































No comments:

Google Referrals