Java Spring Boot Feature Flag Examples
In our Java Spring Boot feature flag tutorial, we implemented a simple feature flag that could be turned on and off. In the real world, many feature flag use cases have more nuance than this. This document will walk you through some common examples of using feature flags in Java Spring Boot with some of those more advanced use cases in mind.
We built multiple features into Unleash, an open-source feature flag platform, to address the complexities of releasing code and managing feature flags along the way. This tutorial will explore the following:
- Gradual Rollouts for Java Spring Boot Apps
- Canary Deployments in Java
- Server-side A/B Testing in Java Spring Boot
- Feature Flag Analytics and Reporting in Java
- Application Metrics & Monitoring
- Feature Flag Audit Logs in Java
- Flag Automation & Workflow Integration for Java Apps
Gradual Rollouts for Java Spring Boot Apps
It is common to use feature flags to roll out changes to a percentage of users, and we can use Unleash to do a gradual rollout with a Java-based app.
In our Spring Boot tutorial, the flag controls the release of a new service implementation on a product page. To further customize that, you can modify the basic setup to adjust the percentage of users who experience this feature with a gradual rollout.
Navigate to the Gradual Rollout page of a flag in Unleash by clicking "Edit strategy".
Adjust the percentage of users to 50% or whichever value you choose, and refresh your app in the browser to see if your user has the new feature experience. This might take 30 seconds to propagate.
You can achieve this same result using our API:
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create("{\n \"name\": \"flexibleRollout\",\n \"title\": \"",\n \"disabled\": false,\n \"sortOrder\": 9999,\n \"constraints\": [\n {\n \"values\": [\n \"1\",\n \"2\"\n ],\n \"inverted\": false,\n \"operator\": \"IN\",\n \"contextName\": \"appName\",\n \"caseInsensitive\": false\n }\n ],\n \"variants\": [\n {\n \"name\": \"blue_group\",\n \"weight\": 0,\n \"weightType\": \"fix\",\n \"stickiness\": \"custom.context.field\",\n \"payload\": {\n \"type\": \"json\",\n \"value\": \"{\\\"color\\\": \\\"red\\\"}\"\n }\n }\n ],\n \"parameters\": {\n \"groupId\": \"some_new\",\n \"rollout\": \"25\",\n \"stickiness\": \"sessionId\"\n },\n \"segments\": [\n 1,\n 2\n ]\n}", mediaType);
Request request = new Request.Builder()
.url("<your-unleash-url>/api/admin/projects/:projectId/features/:featureName/environments/:environment/strategies")
.method("POST", body)
.addHeader("Content-Type", "application/json")
.addHeader("Accept", "application/json")
.addHeader("Authorization", "<API_KEY_VALUE>")
.build();
Response response = client.newCall(request).execute();
Learn more about gradual rollouts in our docs. Also, learn more about our API for creating a new strategy for your flag.