|
1 | 1 | # OpenFeature SDK for Java
|
2 | 2 |
|
3 |
| -This is an experimental Java SDK. The main motivation at the moment is to square away the API surface area to |
4 |
| -ensure the work we're doing in typescript can be replicated in a less flexible language. This may become the official |
5 |
| -Java SDK eventually, but now is not that time. |
| 3 | +[](https://www.repostatus.org/#wip) |
| 4 | +[](https://snyk.io/test/github/open-feature/java-sdk?targetFile=lib/build.gradle) |
| 5 | +[](https://github.com/open-feature/java-sdk/actions/workflows/merge.yml) |
| 6 | +[](https://codecov.io/gh/open-feature/java-sdk) |
| 7 | + |
| 8 | + |
| 9 | +This is the Java implementation of [OpenFeature](https://openfeature.dev), a vendor-agnostic abstraction library for evaluating feature flags. |
| 10 | + |
| 11 | +We support multiple data types for flags (numbers, strings, booleans, objects) as well as hooks, which can alter the lifecycle of a flag evaluation. |
| 12 | + |
| 13 | +This library is intended to be used in server-side contexts and has not been evaluated for use in mobile devices. |
| 14 | + |
| 15 | +## Usage |
| 16 | + |
| 17 | +While `Boolean` provides the simplest introduction, we offer a variety of flag types. |
| 18 | + |
| 19 | +```java |
| 20 | +class MyClass { |
| 21 | + public UI booleanExample() { |
| 22 | + // Should we render the redesign? Or the default webpage? |
| 23 | + if (client.getBooleanValue("redesign_enabled", false)) { |
| 24 | + return render_redesign(); |
| 25 | + } |
| 26 | + return render_normal(); |
| 27 | + } |
| 28 | + |
| 29 | + public Template stringExample() { |
| 30 | + // Get the template to load for the custom new homepage |
| 31 | + String template = client.getStringValue("homepage_template", "default-homepage.html") |
| 32 | + return render_template(template); |
| 33 | + } |
| 34 | + |
| 35 | + public List<Module> numberExample() { |
| 36 | + // How many modules should we be fetching? |
| 37 | + Integer count = client.getIntegerValue("module-fetch-count", 4); |
| 38 | + return fetch_modules(count); |
| 39 | + } |
| 40 | + |
| 41 | + public Module structureExample() { |
| 42 | + // This deserializes into the Module structure for you. |
| 43 | + Module heroModule = client.getObjectValue("hero-module", myExampleModule); |
| 44 | + return heroModule; |
| 45 | + } |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +## Requirements |
| 50 | +- Java 11+ |
| 51 | + |
| 52 | +## Installation |
| 53 | + |
| 54 | +### Add it to your build |
| 55 | + |
| 56 | +Maven: |
| 57 | +```xml |
| 58 | +<dependency> |
| 59 | + <groupId>dev.openfeature</groupId> |
| 60 | + <artifactId>javasdk</artifactId> |
| 61 | + <version>0.0.1-SNAPSHOT</version> |
| 62 | +</dependency> |
| 63 | +``` |
| 64 | + |
| 65 | +Gradle: |
| 66 | +```groovy |
| 67 | +dependencies { |
| 68 | + implementation 'dev.openfeature:javasdk:0.0.1-SNAPSHOT' |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +### Configure it |
| 73 | +To configure it, you'll need to add a provider to the global singleton `OpenFeatureAPI`. From there, you can generate a `Client` which is usable by your code. While you'll likely want a provider for your specific backend, we've provided a `NoOpProvider`, which simply returns the default passed in. |
| 74 | +```java |
| 75 | +class MyApp { |
| 76 | + public void example(){ |
| 77 | + OpenFeatureAPI api = OpenFeatureAPI.getInstance(); |
| 78 | + api.setProvider(new NoOpProvider()); |
| 79 | + Client client = api.getClient(); |
| 80 | + // Now use your `client` instance to evaluate some feature flags! |
| 81 | + } |
| 82 | +} |
| 83 | +``` |
| 84 | +## Contacting us |
| 85 | +We hold regular meetings which you can see [here](https://github.com/open-feature/community/#meetings-and-events). |
| 86 | + |
| 87 | +We are also present on the `#openfeature` channel in the [CNCF slack](https://slack.cncf.io/). |
| 88 | + |
| 89 | +## Contributors |
| 90 | + |
| 91 | +Thanks so much to our contributors. |
| 92 | + |
| 93 | +<a href="https://github.com/open-feature/java-sdk/graphs/contributors"> |
| 94 | + <img src="https://contrib.rocks/image?repo=open-feature/java-sdk" /> |
| 95 | +</a> |
| 96 | + |
| 97 | +Made with [contrib.rocks](https://contrib.rocks). |
0 commit comments