Developing Java Applications with Unicode Data

Author: Shazin Sadakath


Recently we got to work with a Japanese client and had to develop the backend and REST API to work with Japanese Characters. Following are some of the lessons we learnt during that Software Development work.

Infrastructure timezone must be changed

Since we were using AWS IaaS (AWS Linux) we followed the instruction available at AWS Documentation to change the timezone. So please following your Infrastructure documentation to set up the timezone.

Database must be created with Unicode support and Timezone must be properly set

Creating MySQL Database with UTF8 Encoding

CREATE DATABASE  DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

This is important as data will be stored eventually on the database. So please check your Database Server manual on how to create a Database with UTF8 Encoding.

Changing MySQL Database Server timezone

Changing the configuration file for MySQL and adding following;

default-time-zone = '+09:00'

The setting is from + or - no of hours from GMT time. So please check your Database Server manual on how to set the timezone. After which Database server must be restarted.

Connecting to Database with Unicode support

Setting JDBC MySQL Connection URL Query Strings

jdbc:mysql://localhost/?useUnicode=yes&characterEncoding=UTF-8&useTimezone=true&serverTimezone=Asia/Tokyo

In MySQL we used useUnicode, characterEncoding, useTimezone, serverTimezone to create a JDBC Connection which uses Unicode encoding and Japanese Timezone. So please your JDBC Driver based on Database server to configure the connection if required.

JSON Serialization using Jackson with Timezone

Setting up JSON ObjectMapper to use the system default timezone

Following Jackson2ObjectMapperBuilderCustomizer must be configured to set the timezone.

    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jacksonObjectMapperCustomization() {
        return jacksonObjectMapperBuilder ->
                jacksonObjectMapperBuilder.timeZone(TimeZone.getDefault());
    }

Conclusion

Finally testing is required make sure everything works well together.



Tags: Unicode UTF8 Java MySQL Japanese
Views: 103
Register for more exciting articles

Comments

Please login or register to post a comment.


There are currently no comments.