Using Flagonic from Java

Flagonic has no Java SDK of its own — by design. You use the official OpenFeature Java SDK with its official OFREP provider. Nothing on your classpath is Flagonic-specific, so swapping flag vendors later is a config change, not a refactor.

Prerequisites

  • A running Flagonic server with a flag defined — see the overview & quickstart.
  • An SDK key (fgn_sdk_…) for your project + environment.
  • Java 11 or newer.

Install

<!-- Maven -->
<dependency>
  <groupId>dev.openfeature</groupId>
  <artifactId>sdk</artifactId>
</dependency>
<dependency>
  <groupId>dev.openfeature.contrib.providers</groupId>
  <artifactId>ofrep</artifactId>
</dependency>

Initialize once at startup

import dev.openfeature.sdk.*;
import dev.openfeature.contrib.providers.ofrep.OfrepProvider;
import dev.openfeature.contrib.providers.ofrep.OfrepProviderOptions;
import java.util.List;
import java.util.Map;

OfrepProviderOptions options = OfrepProviderOptions.builder()
    .baseUrl(System.getenv("FLAGONIC_URL"))  // e.g. https://flags.example.com
    .headers(Map.of("Authorization",
        List.of("Bearer " + System.getenv("FLAGONIC_SDK_KEY"))))
    .build();

OpenFeatureAPI api = OpenFeatureAPI.getInstance();
api.setProviderAndWait(OfrepProvider.constructProvider(options));

Client client = api.getClient("checkout-service");

Evaluate flags

The evaluation context carries the targeting key (a stable subject identifier — it keeps percentage rollouts sticky) plus any attributes your rules match on:

MutableContext ctx = new MutableContext(user.getId());
ctx.add("plan", user.getPlan());

if (client.getBooleanValue("new-checkout", false, ctx)) {
    newCheckout();
} else {
    legacyCheckout();
}

String  theme = client.getStringValue("banner-theme", "default", ctx);
Integer limit = client.getIntegerValue("rate-limit", 100, ctx);
Double  ratio = client.getDoubleValue("sampling-ratio", 0.1, ctx);
Value  config = client.getObjectValue("checkout-config", new Value(), ctx);

Each call takes a default that is returned if the flag is missing or Flagonic is unreachable — pick the value you'd want during an outage.

Inspect why a value was served

FlagEvaluationDetails<Boolean> details =
    client.getBooleanDetails("new-checkout", false, ctx);
log.info("value={} variant={} reason={}",
    details.getValue(), details.getVariant(), details.getReason());
// reasons: TARGETING_MATCH, SPLIT, DEFAULT, STATIC, DISABLED, ERROR

Switching vendors

The provider speaks standard OFREP: point baseUrl at any other OFREP-compatible backend — one line, no import changes.

Provider builders occasionally shift between releases — if something doesn't compile, check the java-sdk-contrib README for your installed version.