|
1 | 1 | # customer-service-client |
2 | 2 |
|
| 3 | +[](https://openjdk.org/projects/jdk/21/) |
| 4 | +[](https://spring.io/projects/spring-boot) |
| 5 | +[](https://openapi-generator.tech/) |
| 6 | +[](../LICENSE) |
| 7 | + |
3 | 8 | Generated Java client for the **customer-service**, showcasing **type-safe generic responses** with OpenAPI + a |
4 | 9 | custom Mustache template (wrapping payloads in a reusable `ServiceClientResponse<T>`). |
5 | 10 |
|
@@ -72,12 +77,117 @@ Each is a **thin shell** extending `ServiceClientResponse<PayloadType>`. |
72 | 77 |
|
73 | 78 | --- |
74 | 79 |
|
| 80 | +## 🚫 Not a Published Library (Re-generate in your project) |
| 81 | + |
| 82 | +This module is a **reference demo**, not a published library. |
| 83 | +To apply the same approach in your own project: |
| 84 | + |
| 85 | +1. Generate your own OpenAPI spec (`/v3/api-docs.yaml`). |
| 86 | +2. Copy the two Mustache templates (`api_wrapper.mustache`, `model.mustache`) into your project. |
| 87 | +3. Run OpenAPI Generator with your spec + templates → you’ll get type-safe wrappers. |
| 88 | + |
| 89 | +> ⚠️ **Do not add `customer-service-client` as a Maven/Gradle dependency in your project.** |
| 90 | +> Instead, re-generate your own client using **your service’s OpenAPI spec** and the provided Mustache templates. |
| 91 | +
|
| 92 | +--- |
| 93 | + |
| 94 | +## 📦 Prerequisites |
| 95 | + |
| 96 | +Before generating or using the client, make sure you have: |
| 97 | + |
| 98 | +* **Java 21** or newer |
| 99 | +* **Maven 3.9+** (or Gradle 8+ if you adapt the build) |
| 100 | +* A running instance of the `customer-service` exposing its OpenAPI spec |
| 101 | + |
| 102 | +--- |
| 103 | + |
| 104 | +## 🎯 Scope & Non-Goals |
| 105 | + |
| 106 | +This module focuses on **generics-aware client generation** only. Specifically, it demonstrates: |
| 107 | + |
| 108 | +* Marking wrapper schemas via OpenAPI **vendor extensions** (e.g., `x-api-wrapper`, `x-api-wrapper-datatype`) |
| 109 | +* A tiny **Mustache overlay** that emits **thin wrapper classes** extending a reusable `ServiceClientResponse<T>` |
| 110 | +* How those wrappers enable **compile-time type safety** in consumer code (see the adapter example) |
| 111 | + |
| 112 | +**Out of scope (non-goals):** |
| 113 | + |
| 114 | +* Runtime concerns such as error handling strategies for non-2xx responses, retries, logging, metrics, circuit-breaking |
| 115 | +* Business validation, pagination conventions, or API design guidelines |
| 116 | +* Authentication/authorization configuration |
| 117 | +* Packaging and publishing this client as a reusable library |
| 118 | + |
| 119 | +If you need those capabilities, add them in your host application or platform code. This repo is intentionally minimal |
| 120 | +to keep the focus on the **wrapper generation pattern**. |
| 121 | + |
| 122 | +--- |
| 123 | + |
| 124 | +## 🔄 How thin wrappers are produced (end-to-end flow) |
| 125 | + |
| 126 | +``` |
| 127 | +Controller returns `ServiceResponse<T>` |
| 128 | + │ |
| 129 | + ▼ |
| 130 | +Springdoc `OpenApiCustomizer` discovers `T` and marks wrapper schemas |
| 131 | +(vendor extensions: `x-api-wrapper: true`, `x-api-wrapper-datatype: <T>`) |
| 132 | + │ |
| 133 | + ▼ |
| 134 | +OpenAPI spec (YAML/JSON) contains `ServiceResponse{T}` schemas with vendor extensions |
| 135 | + │ |
| 136 | + ▼ |
| 137 | +OpenAPI Generator runs with a tiny Mustache overlay |
| 138 | +(`api_wrapper.mustache`, `model.mustache`) |
| 139 | + │ |
| 140 | + ▼ |
| 141 | +Generated Java classes: |
| 142 | +- Thin wrappers like `ServiceResponseCustomerCreateResponse` |
| 143 | + extending `ServiceClientResponse<CustomerCreateResponse>` |
| 144 | +- No duplicated envelope fields |
| 145 | + │ |
| 146 | + ▼ |
| 147 | +Consumer code (e.g., adapter) gets compile-time type safety |
| 148 | +``` |
| 149 | + |
| 150 | +**Key files** |
| 151 | + |
| 152 | +* Templates: `src/main/resources/openapi-templates/api_wrapper.mustache`, `.../model.mustache` |
| 153 | +* Generated output: `target/generated-sources/openapi/src/gen/java` |
| 154 | +* Packages (from `pom.xml`): `apiPackage`, `modelPackage`, `invokerPackage` |
| 155 | + |
| 156 | +--- |
| 157 | + |
| 158 | +## 🧰 Troubleshooting (quick) |
| 159 | + |
| 160 | +* **No thin wrappers generated?** |
| 161 | + Check your spec contains vendor extensions on wrapper schemas (look for `x-api-wrapper: true`). |
| 162 | + Also verify the generator uses your overlay via `<templateDirectory>`. |
| 163 | + |
| 164 | +* **Wrong packages or missing classes?** |
| 165 | + Ensure `apiPackage`, `modelPackage`, and `invokerPackage` in the plugin configuration match what you expect. |
| 166 | + Delete `target/` and re-run: `mvn clean install`. |
| 167 | + |
| 168 | +* **Spec is stale?** |
| 169 | + Re-pull it: |
| 170 | + |
| 171 | + ```bash |
| 172 | + curl -s http://localhost:8084/customer-service/v3/api-docs.yaml \ |
| 173 | + -o src/main/resources/customer-api-docs.yaml |
| 174 | + mvn -q clean install |
| 175 | + ``` |
| 176 | + |
| 177 | +* **Validation/annotations not found at runtime?** |
| 178 | + Some dependencies (e.g., `spring-web`, `jakarta.*`) are marked **provided**. |
| 179 | + Your host app must supply them on the classpath. |
| 180 | + |
| 181 | +* **Base URL not applied?** |
| 182 | + If you use the Spring configuration, set `customer.api.base-url` correctly and ensure the `RestClient` bean is |
| 183 | + created. |
| 184 | + |
| 185 | +--- |
| 186 | + |
75 | 187 | ## 🧩 Using the Client |
76 | 188 |
|
77 | 189 | ### Option A — Spring Configuration (recommended) |
78 | 190 |
|
79 | | -Include this module as a dependency and configure the base URL: |
80 | | - |
81 | 191 | ```java |
82 | 192 |
|
83 | 193 | @Configuration |
@@ -339,3 +449,11 @@ This module is **reference-oriented**. If you want to publish it as a reusable l |
339 | 449 | * remove `provided` scopes and pin minimal runtime deps, |
340 | 450 | * add a semantic version and release process (e.g., GitHub Release + `mvn deploy` to Maven Central), |
341 | 451 | * keep the Mustache overlay in-repo for transparent builds. |
| 452 | + |
| 453 | +--- |
| 454 | + |
| 455 | +## 📦 Related Module |
| 456 | + |
| 457 | +This client is generated from the OpenAPI spec exposed by: |
| 458 | + |
| 459 | +* [customer-service](../customer-service/README.md) — Sample Spring Boot microservice (API producer). |
0 commit comments