|
| 1 | +## Example: Quarkus Server |
| 2 | + |
| 3 | +A Smithy-Java service running inside a Quarkus application via the |
| 4 | +[`quarkus-smithy` extension](../../quarkus-smithy/README.md). Smithy |
| 5 | +operations share Quarkus's HTTP port — no separate Smithy listener. |
| 6 | + |
| 7 | +### Run it |
| 8 | + |
| 9 | +This example is a standalone Gradle build (see "Why a standalone |
| 10 | +Gradle build" below). Drive it from the smithy-java root with the |
| 11 | +repo's wrapper: |
| 12 | + |
| 13 | +```console |
| 14 | +# from smithy-java/ — publishes the smithy-java jars to your local repo |
| 15 | +./gradlew publishToMavenLocal |
| 16 | + |
| 17 | +# still from smithy-java/ — runs the example's standalone build |
| 18 | +./gradlew --project-dir examples/quarkus-server quarkusDev |
| 19 | +``` |
| 20 | + |
| 21 | +The server listens on `http://localhost:8080`. Watch the boot log for |
| 22 | +the recorder's mount line (`Smithy mounted at /* with N service(s)`) |
| 23 | +and the server's own construction line (`Smithy server constructed |
| 24 | +with N service(s), M operation(s); protocols (precision order): […]`). |
| 25 | + |
| 26 | +Re-run `publishToMavenLocal` whenever you change smithy-java sources. |
| 27 | + |
| 28 | +### Curl the operations |
| 29 | + |
| 30 | +The CoffeeShop service declares `@restJson1 @rpcv2Cbor @rpcv2Json`, so |
| 31 | +each operation is reachable via any of three on-the-wire shapes. The |
| 32 | +server picks one per request in protocol-precision order |
| 33 | +(rpcv2Cbor → rpcv2Json → restJson1). |
| 34 | + |
| 35 | +#### restJson1 — HTTP routes via `@http(method, uri)` |
| 36 | + |
| 37 | +```console |
| 38 | +curl http://localhost:8080/menu |
| 39 | +curl -X PUT http://localhost:8080/order -H 'Content-Type: application/json' \ |
| 40 | + -d '{"coffeeType":"LATTE"}' |
| 41 | +curl http://localhost:8080/order/<id-from-PUT-response> |
| 42 | +``` |
| 43 | + |
| 44 | +#### rpcv2Cbor — `/service/CoffeeShop/operation/<Op>` + `smithy-protocol` header |
| 45 | + |
| 46 | +```console |
| 47 | +# GetMenu — empty input is the CBOR empty map (0xa0) |
| 48 | +printf '\xa0' > /tmp/empty.cbor |
| 49 | +curl -X POST http://localhost:8080/service/CoffeeShop/operation/GetMenu \ |
| 50 | + -H 'smithy-protocol: rpc-v2-cbor' -H 'content-type: application/cbor' \ |
| 51 | + --data-binary @/tmp/empty.cbor |
| 52 | + |
| 53 | +# CreateOrder — {"coffeeType":"LATTE"} |
| 54 | +python3 -c 'import sys; sys.stdout.buffer.write(bytes([0xa1, 0x6a]) + b"coffeeType" + bytes([0x65]) + b"LATTE")' \ |
| 55 | + > /tmp/createorder.cbor |
| 56 | +curl -X POST http://localhost:8080/service/CoffeeShop/operation/CreateOrder \ |
| 57 | + -H 'smithy-protocol: rpc-v2-cbor' -H 'content-type: application/cbor' \ |
| 58 | + --data-binary @/tmp/createorder.cbor |
| 59 | +``` |
| 60 | + |
| 61 | +The response body is CBOR; pipe through `xxd` or a CBOR diagnostic tool |
| 62 | +to read it. |
| 63 | + |
| 64 | +#### rpcv2Json — same URI scheme, JSON body |
| 65 | + |
| 66 | +```console |
| 67 | +curl -X POST http://localhost:8080/service/CoffeeShop/operation/GetMenu \ |
| 68 | + -H 'smithy-protocol: rpc-v2-json' -H 'content-type: application/json' -d '{}' |
| 69 | + |
| 70 | +curl -X POST http://localhost:8080/service/CoffeeShop/operation/CreateOrder \ |
| 71 | + -H 'smithy-protocol: rpc-v2-json' -H 'content-type: application/json' \ |
| 72 | + -d '{"coffeeType":"ESPRESSO"}' |
| 73 | + |
| 74 | +curl -X POST http://localhost:8080/service/CoffeeShop/operation/GetOrder \ |
| 75 | + -H 'smithy-protocol: rpc-v2-json' -H 'content-type: application/json' \ |
| 76 | + -d '{"id":"<id-from-CreateOrder>"}' |
| 77 | +``` |
| 78 | + |
| 79 | +#### Fall-through behavior |
| 80 | + |
| 81 | +The server distinguishes three outcomes per request, observable as |
| 82 | +distinct 404 shapes: |
| 83 | + |
| 84 | +```console |
| 85 | +# no-claim → ctx.next() → Quarkus default 404 (text/plain ~358B page) |
| 86 | +curl -i -X POST http://localhost:8080/service/CoffeeShop/operation/GetMenu |
| 87 | + |
| 88 | +# claim-and-reject → server 404 with empty body |
| 89 | +curl -i -X POST http://localhost:8080/service/CoffeeShop/operation/ \ |
| 90 | + -H 'smithy-protocol: rpc-v2-cbor' -H 'content-type: application/cbor' \ |
| 91 | + --data-binary @/tmp/empty.cbor |
| 92 | + |
| 93 | +# unrelated path → ctx.next() → Quarkus (or your own sibling handler) |
| 94 | +curl -i http://localhost:8080/q/notreal |
| 95 | +``` |
| 96 | + |
| 97 | +The empty-body 404 (claim-and-reject) vs the Quarkus default-page 404 |
| 98 | +(no-claim) is the observable signal for routing correctness. A request |
| 99 | +that *claimed* the rpcv2-cbor protocol but failed URI parsing is |
| 100 | +intercepted before `ctx.next()`, so a sibling handler can't |
| 101 | +misinterpret it. A request that no protocol claimed falls through, so |
| 102 | +Quarkus (or any other Vert.x handler on the same router) gets a chance |
| 103 | +to serve it. |
| 104 | + |
| 105 | +### Hot reload |
| 106 | + |
| 107 | +While `quarkusDev` is running: |
| 108 | + |
| 109 | +- Edit `CreateOrder.java` (e.g., change a status string), save → re-curl |
| 110 | + `PUT /order`. The response reflects the change without a restart. |
| 111 | +- Edit `src/main/smithy/coffee.smithy` (e.g., add a member), save → the |
| 112 | + `CodeGenProvider` regenerates the stub and the recorder removes the |
| 113 | + previous Vert.x route before installing the new one. |
| 114 | + |
| 115 | +### Path-prefix mode |
| 116 | + |
| 117 | +To put Smithy operations under `/api/smithy/...` (so REST endpoints can |
| 118 | +own the root), set in `src/main/resources/application.properties`: |
| 119 | + |
| 120 | +```properties |
| 121 | +quarkus.smithy.server.path-prefix=/api/smithy |
| 122 | +``` |
| 123 | + |
| 124 | +`@http(uri:"/menu")` then becomes reachable at `/api/smithy/menu`. Verify: |
| 125 | + |
| 126 | +```console |
| 127 | +curl -i http://localhost:8080/api/smithy/menu # 200 |
| 128 | +curl -i http://localhost:8080/menu # 404 |
| 129 | +``` |
| 130 | + |
| 131 | +In dev mode you can also live-edit this from the Dev UI Configuration |
| 132 | +tile — see below. |
| 133 | + |
| 134 | +### Packaged jar (prod profile) |
| 135 | + |
| 136 | +```console |
| 137 | +# from smithy-java/ |
| 138 | +./gradlew --project-dir examples/quarkus-server quarkusBuild |
| 139 | +java -jar examples/quarkus-server/build/quarkus-app/quarkus-run.jar |
| 140 | +``` |
| 141 | + |
| 142 | +Run the same curl probes against this — they should all 200, boot is |
| 143 | +sub-2s. |
| 144 | + |
| 145 | +### Dev UI |
| 146 | + |
| 147 | +While `quarkusDev` is running, open `http://localhost:8080/q/dev-ui`. |
| 148 | + |
| 149 | +There is no Smithy-specific Dev UI card today (none of the extension's |
| 150 | +build steps emit a `CardPageBuildItem`), so use the standard tiles: |
| 151 | + |
| 152 | +- **Endpoints** — confirms the Smithy server's catch-all route |
| 153 | + alongside Quarkus's own routes. |
| 154 | +- **Configuration** — search for `quarkus.smithy.server` to live-edit |
| 155 | + `path-prefix`, `workers`, and `shutdown-grace`. |
| 156 | +- **ArC** — confirms the `@Produces Service` bean is present and |
| 157 | + unremovable (the extension marks it via `UnremovableBeanBuildItem`). |
| 158 | +- **Build Steps** — confirms `SmithyProcessor` ran and which build |
| 159 | + items it produced. |
| 160 | +- **Continuous Testing** — press `r` in the dev terminal (or open the |
| 161 | + tile) to re-run tests on save. |
| 162 | + |
| 163 | +--- |
| 164 | + |
| 165 | +### How it's wired |
| 166 | + |
| 167 | +The user produces a `@Produces Service` bean (the generated `CoffeeShop` |
| 168 | +stub) and the extension mounts a `SmithyVertxServer` from the upstream |
| 169 | +`:server:server-vertx` module on Quarkus's main HTTP router: |
| 170 | + |
| 171 | +```java |
| 172 | +@ApplicationScoped |
| 173 | +public class CoffeeShopServerConfig { |
| 174 | + |
| 175 | + @Produces |
| 176 | + @Singleton |
| 177 | + Service coffeeShop() { |
| 178 | + return CoffeeShop.builder() |
| 179 | + .addCreateOrderOperation(new CreateOrder()) |
| 180 | + .addGetMenuOperation(new GetMenu()) |
| 181 | + .addGetOrderOperation(new GetOrder()) |
| 182 | + .build(); |
| 183 | + } |
| 184 | +} |
| 185 | +``` |
| 186 | + |
| 187 | +#### Project layout |
| 188 | + |
| 189 | +``` |
| 190 | +. |
| 191 | +├── build.gradle.kts ← apply io.quarkus, depend on quarkus-smithy |
| 192 | +├── settings.gradle.kts |
| 193 | +├── gradle.properties |
| 194 | +├── smithy-build.json ← project root, configures java-codegen |
| 195 | +├── src/main/smithy/ ← .smithy models (Quarkus convention) |
| 196 | +│ ├── coffee.smithy |
| 197 | +│ ├── main.smithy |
| 198 | +│ └── order.smithy |
| 199 | +├── src/main/java/.../CoffeeShopServerConfig.java |
| 200 | +├── src/main/java/.../CreateOrder.java |
| 201 | +├── src/main/java/.../GetMenu.java |
| 202 | +├── src/main/java/.../GetOrder.java |
| 203 | +└── src/main/resources/application.properties |
| 204 | +``` |
| 205 | + |
| 206 | +No `afterEvaluate { ... srcDir(...) }` wiring. No |
| 207 | +`compileJava.dependsOn(smithyBuild)`. The `quarkus-smithy` extension's |
| 208 | +`CodeGenProvider` runs as part of `quarkusGenerateCode`, generates Java |
| 209 | +sources directly into Quarkus's |
| 210 | +`build/classes/java/quarkus-generated-sources/smithy/` output directory, |
| 211 | +and `compileJava` picks them up automatically. |
| 212 | + |
| 213 | +#### Why a standalone Gradle build |
| 214 | + |
| 215 | +This example is intentionally not included in `smithy-java`'s root |
| 216 | +`settings.gradle.kts`. Quarkus dev-mode workspace discovery would |
| 217 | +otherwise substitute sibling smithy-java projects' raw `build/classes` |
| 218 | +directories for their published jars — bypassing |
| 219 | +`:codecs:json-codec`'s shadowJar (which relocates Jackson 3) and |
| 220 | +splitting the classloader graph in ways that break the |
| 221 | +`SchemaExtensionProvider` SPI lookup. Running standalone, against the |
| 222 | +locally-published jars, makes the example behave exactly the way a |
| 223 | +real customer's project would. |
| 224 | + |
| 225 | +### Running the extension's tests |
| 226 | + |
| 227 | +These live in the parent smithy-java build, not in this example: |
| 228 | + |
| 229 | +```console |
| 230 | +# from smithy-java/ |
| 231 | +./gradlew :server:server-vertx:test # Smithy Vert.x server tests |
| 232 | +./gradlew :quarkus-smithy-integration-tests:test # extension integ |
| 233 | +./gradlew :aws:server:aws-server-restjson:integ # protocol integ |
| 234 | +``` |
0 commit comments