---
title: "อัตราแลกเปลี่ยนใน Java และ Spring Boot"
description: "สร้าง client ของ Spring Boot ที่มี type ด้วย RestClient, BigDecimal และ key จาก environment เพื่อดึงอัตราแลกเปลี่ยนจาก exchangerate.dev ฝั่งเซิร์ฟเวอร์"
canonical: "https://exchangerate.dev/th/learn/java-spring-boot-exchange-rates"
---

> Index of every exchangerate.dev page and doc for AI readers: [llms.txt](https://exchangerate.dev/llms.txt)

# อัตราแลกเปลี่ยนใน Java และ Spring Boot

สร้าง client ของ Spring Boot ที่มี type ด้วย RestClient, BigDecimal และ key จาก environment เพื่อดึงอัตราแลกเปลี่ยนจาก exchangerate.dev ฝั่งเซิร์ฟเวอร์

17 ก.ค. 2569 · อ่าน 6 นาที

Spring `RestClient` แปลง JSON เป็น record ได้ เก็บอัตราเป็น `BigDecimal` ฉีด key จาก configuration และรักษา `source`, `market_session`, `data_updated_at`

- ใช้ Spring `RestClient`
- ใช้ `BigDecimal`
- อ่าน key จาก `EXCHANGERATE_API_KEY`
- เก็บ `data_updated_at`

## สร้าง RestClient แบบ typed

record เก็บแผนที่อัตราและข้อมูลที่ใช้ตีความ โดย Jackson map snake_case ผ่าน `@JsonProperty`

```javaexchangerateclientjava title="java · ExchangeRateClient.java"
package com.example.fx;

import com.fasterxml.jackson.annotation.JsonProperty;
import java.math.BigDecimal;
import java.time.Instant;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestClient;

record LatestRates(
    String base,
    String source,
    @JsonProperty("market_session") String marketSession,
    @JsonProperty("data_updated_at") Instant dataUpdatedAt,
    Map<String, BigDecimal> rates
) {}

@Component
public final class ExchangeRateClient {
  private final RestClient client;

  public ExchangeRateClient(
      RestClient.Builder builder,
      @Value("${exchangerate.api-key}") String apiKey
  ) {
    this.client = builder
        .baseUrl("https://api.exchangerate.dev/v1")
        .defaultHeader(HttpHeaders.AUTHORIZATION, "Bearer " + apiKey)
        .build();
  }

  public LatestRates latest(String base) {
    return client.get()
        .uri("/latest/{base}?symbols=EUR,GBP", base)
        .retrieve()
        .body(LatestRates.class);
  }
}
```

## โหลด key จาก environment

```yamlapplicationyaml title="yaml · application.yaml"
exchangerate:
  api-key: ${EXCHANGERATE_API_KEY}
```

## จัดการความใหม่และข้อผิดพลาดให้ชัดเจน

`RestClient.retrieve()` โยนข้อผิดพลาดเมื่อเป็น 4xx หรือ 5xx ตั้ง timeout และ retry เฉพาะความผิดพลาดชั่วคราว

**ใช้ BigDecimal ตลอดการคำนวณ**

รักษาความละเอียดของอัตราและปัดเฉพาะยอดสุดท้าย

- ใช้ client ร่วมกัน
- ตั้ง timeout
- แคชเฉพาะผลสำเร็จ
- เก็บแหล่งที่มาและเวลา

## คำถาม

### RestClient หรือ RestTemplate

แอป Spring แบบ synchronous ใหม่ควรใช้ RestClient

### ทำไมใช้ BigDecimal

เพื่อเลี่ยงความคลาดเคลื่อนของ double

### เก็บ key ที่ไหน

อ่านจาก environment หรือ secret manager

### ทำอย่างไรเมื่อ 429

อย่าแคชเป็นข้อมูล และเคารพ Free 10 คำขอต่อนาทีและ 5,000 ต่อเดือน

## อ่านต่อ

- [อัตราแลกเปลี่ยนย้อนหลังและข้อมูลอนุกรมเวลา](https://exchangerate.dev/th/learn/historical-fx-time-series)

Source: https://exchangerate.dev/th/learn/java-spring-boot-exchange-rates
