---
title: "Java Exchange Rate API with Spring Boot"
description: "Build a typed Spring Boot client for live FX rates with RestClient, BigDecimal, environment-based authentication, and explicit freshness fields."
canonical: "https://exchangerate.dev/learn/java-spring-boot-exchange-rates"
---

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

# Exchange rates in Java and Spring Boot

Build a typed Spring Boot client for live FX rates with RestClient, BigDecimal, environment-based authentication, and explicit freshness fields.

By the Exchangerate team · Updated 20 Sep 2026 · 6 min read

Spring `RestClient` can map exchangerate.dev JSON directly into a Java record. Keep rates as `BigDecimal`, inject the API key from configuration, and retain `source`, `market_session`, and `data_updated_at` instead of reducing the response to one number.

- Use Spring `RestClient` for a synchronous typed client and let it reject 4xx and 5xx responses by default.
- Map rate values to `BigDecimal`; do not convert money through binary floating-point arithmetic.
- Load the key from `EXCHANGERATE_API_KEY`, never from a committed properties file.
- Store `data_updated_at` with the rate so downstream jobs can enforce their own freshness rule.

## Create a typed RestClient

The response record below keeps the rate map and the fields needed to interpret it. Jackson maps the snake-case JSON names through `@JsonProperty`, while Spring handles the HTTP request and JSON decoding.

```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);
  }
}
```

## Load the key from the environment

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

## Handle freshness and errors explicitly

`RestClient.retrieve()` throws on 4xx and 5xx responses by default, so a failed upstream call does not become a cached zero. Add bounded timeouts in the shared request factory, and retry only transient failures. A 401 needs a key fix (see the [quickstart](https://exchangerate.dev/docs/quickstart)); a sustained 429 needs less traffic or a higher limit.

**Keep BigDecimal through the calculation**

Parse rates and amounts as `BigDecimal`, multiply with an explicit rounding policy, and round only for the target currency at the display boundary. The API rate should remain at full precision.

- Reuse one configured RestClient across requests.
- Set connection and read timeouts on its request factory.
- Cache only successful responses.
- Persist source, marketSession, and dataUpdatedAt with the rate.

## Questions

### Should a Spring Boot application use RestClient or RestTemplate for exchange rates?

Use RestClient for new synchronous Spring applications. It has a fluent API, maps JSON into records, and throws on error responses through retrieve by default.

### Why should Java map exchange rates to BigDecimal?

BigDecimal avoids the binary representation artifacts of double when amounts are multiplied and rounded. Keep the rate at full precision and apply the target currency's rounding rule only to the final amount.

### Where should the exchangerate.dev API key be stored in Spring Boot?

Read it from an environment variable or secret manager through Spring configuration. Do not commit the key to application.yaml, source code, or test fixtures.

### What should the application do after a 429 response?

Do not cache it as rate data. Back off, reduce duplicate requests through caching, and respect the plan limit; the Free tier allows 10 requests per minute and 5,000 per month.

## Keep reading

- [Historical FX rates and time series in one call](https://exchangerate.dev/learn/historical-fx-time-series)
- [Reading source and market_session](https://exchangerate.dev/learn/reading-source-and-session)
- [Indicative vs executable FX rates](https://exchangerate.dev/learn/indicative-vs-executable-fx)
- [PHP Exchange Rate API with Laravel](https://exchangerate.dev/learn/php-laravel-exchange-rates)
- [Refresh exchange rates in Excel with Power Automate](https://exchangerate.dev/learn/power-automate-exchange-rates)
- [Get exchange rates in Swift with URLSession](https://exchangerate.dev/learn/swift-exchange-rates)

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