|
| 1 | +# spring-boot-reactive-default-timezone |
| 2 | + |
| 3 | +> ตัวอย่างการเขียน Spring-boot Reactive Default Time Zone |
| 4 | +
|
| 5 | +# Prerequisites |
| 6 | + |
| 7 | +- มีความเข้าใจเรื่อง Time Zone |
| 8 | + - ถ้าไม่เข้าใจสามารถเรียนรู้ได้จาก [สรุปสั้น ๆ เรื่องเขตเวลา (Time Zone) และไขข้อสงสัยว่าทำไมเวลาประเทศไทยถึงเป็น UTC+7](https://www.jittagornp.me/blog/short-summary-of-time-zone/) |
| 9 | + - ถ้าใครใช้ Server เป็น Ubuntu สามารถดูตัวอย่างการ Set Time Zone ได้จาก [ตั้งค่า Time Zone สำหรับ Ubuntu 20.04 LTS](https://www.jittagornp.me/blog/set-timezone-for-ubuntu20.04/) |
| 10 | + |
| 11 | +# 1. เพิ่ม Dependencies และ Plugins |
| 12 | + |
| 13 | +pom.xml |
| 14 | +``` xml |
| 15 | +... |
| 16 | +<parent> |
| 17 | + <groupId>org.springframework.boot</groupId> |
| 18 | + <artifactId>spring-boot-starter-parent</artifactId> |
| 19 | + <version>2.3.2.RELEASE</version> |
| 20 | +</parent> |
| 21 | + |
| 22 | +<dependencies> |
| 23 | + <dependency> |
| 24 | + <groupId>org.springframework.boot</groupId> |
| 25 | + <artifactId>spring-boot-starter-webflux</artifactId> |
| 26 | + </dependency> |
| 27 | + |
| 28 | + <dependency> |
| 29 | + <groupId>org.projectlombok</groupId> |
| 30 | + <artifactId>lombok</artifactId> |
| 31 | + <scope>provided</scope> |
| 32 | + </dependency> |
| 33 | +</dependencies> |
| 34 | + |
| 35 | +<build> |
| 36 | + <plugins> |
| 37 | + <plugin> |
| 38 | + <groupId>org.springframework.boot</groupId> |
| 39 | + <artifactId>spring-boot-maven-plugin</artifactId> |
| 40 | + <executions> |
| 41 | + <execution> |
| 42 | + <id>build-info</id> |
| 43 | + <goals> |
| 44 | + <goal>build-info</goal> |
| 45 | + </goals> |
| 46 | + <configuration> |
| 47 | + <additionalProperties> |
| 48 | + <java.version>${java.version}</java.version> |
| 49 | + </additionalProperties> |
| 50 | + </configuration> |
| 51 | + </execution> |
| 52 | + </executions> |
| 53 | + </plugin> |
| 54 | + </plugins> |
| 55 | +</build> |
| 56 | +... |
| 57 | +``` |
| 58 | + |
| 59 | +# 2. เขียน Main Class |
| 60 | + |
| 61 | +``` java |
| 62 | +@SpringBootApplication |
| 63 | +@ComponentScan(basePackages = {"me.jittagornp"}) |
| 64 | +public class AppStarter { |
| 65 | + |
| 66 | + public static void main(String[] args) { |
| 67 | + SpringApplication.run(AppStarter.class, args); |
| 68 | + } |
| 69 | + |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +# 3. Config Default Time Zone |
| 74 | + |
| 75 | +สร้าง Java Class Config ขึ้นมา แล้วกำหนด Time Zone ตามที่ต้องการ |
| 76 | + |
| 77 | +```java |
| 78 | +@Slf4j |
| 79 | +@Configuration |
| 80 | +public class TimeZoneConfig { |
| 81 | + |
| 82 | + @PostConstruct |
| 83 | + public void setDefaultTimeZone(){ |
| 84 | + |
| 85 | + TimeZone.setDefault(TimeZone.getTimeZone("Asia/Bangkok")); |
| 86 | + |
| 87 | + log.info("set Default Time Zone => {}", TimeZone.getDefault().getID()); |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +**คำอธิบาย** |
| 95 | + |
| 96 | +- โดยปกติ `TimeZone.getDefault()` จะทำการอ่านค่า Time Zone มาจากเครื่อง |
| 97 | + - ถ้าเครื่องเรา Default Time Zone เป็น `UTC` แล้ว `TimeZone.getDefault()` ก็จะได้ `UTC` เช่นเดียวกัน |
| 98 | + - ถ้าเครื่องเรา Default Time Zone เป็น `Asia/Bangkok` แล้ว `TimeZone.getDefault()` ก็จะได้ `Asia/Bangkok` เช่นเดียวกัน |
| 99 | + - เพราะฉะนั้น ต้องระวังเวลาเอา App ไป Deploy ให้เช็คเสมอว่าเครื่องนั้น Default Time Zone เป็นอะไร |
| 100 | +- เพื่อป้องกันข้อผิดพลาดอีกชั้น เราสามารถ Set Time Zone App เองได้โดยการใช้ `TimeZone.setDefault(...)` |
| 101 | +- กำหนดให้ Java (8+ DateTime) ใช้ Default Time Zone เป็น `Asia/Bangkok` หรือ `UTC+7` |
| 102 | + |
| 103 | +# 4. เขียน Controller |
| 104 | +``` java |
| 105 | +@RestController |
| 106 | +@RequestMapping("/time-zones") |
| 107 | +public class TimeZoneController { |
| 108 | + |
| 109 | + @GetMapping |
| 110 | + public Flux<TimeZone> getAvailableTimeZones(){ |
| 111 | + return Flux.fromIterable(Arrays.asList(TimeZone.getAvailableIDs())) |
| 112 | + .map(TimeZone::getTimeZone); |
| 113 | + } |
| 114 | + |
| 115 | + @GetMapping("/default") |
| 116 | + public Mono<TimeZone> getDefault() { |
| 117 | + return Mono.just(TimeZone.getDefault()); |
| 118 | + } |
| 119 | + |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +# 5. Build Code |
| 124 | +cd ไปที่ root ของ project จากนั้น |
| 125 | +``` sh |
| 126 | +$ mvn clean package |
| 127 | +``` |
| 128 | + |
| 129 | +# 6. Run |
| 130 | +``` sh |
| 131 | +$ mvn spring-boot:run |
| 132 | +``` |
| 133 | + |
| 134 | + |
| 135 | + |
| 136 | +# 7. เข้าใช้งาน |
| 137 | + |
| 138 | +เปิด browser แล้วเข้า [http://localhost:8080/times-zones](http://localhost:8080/times-zones) |
| 139 | + |
| 140 | + |
| 141 | + |
| 142 | +Default Time Zone |
| 143 | + |
| 144 | + |
0 commit comments