diff --git a/demo/spring-boot/README.md b/demo/spring-boot/README.md new file mode 100644 index 0000000..05960b4 --- /dev/null +++ b/demo/spring-boot/README.md @@ -0,0 +1,32 @@ +# Spring Boot demo + +A runnable consumer of `problem-details-java` **as released on Maven Central** (version pinned in `pom.xml` via `problem-details.version`). It lives outside the library's Maven reactor on purpose: it builds and runs exactly like a user project. + +## Run it + +```sh +cd demo/spring-boot +./../../mvnw spring-boot:run +``` + +The app starts on . Endpoints, one per case: + +| Endpoint | Case | +|-----------------------|---------------------------------------------------| +| `GET /demo/ok` | Control case, no problem | +| `GET /demo/credit` | Full problem + typed extension (403) | +| `GET /demo/missing` | No explicit instance → defaulted from path (404) | +| `GET /demo/boom` | No status member → 500 fallback | +| `GET /demo/validate?age=-5` | Validation-style `errors` extension (422) | +| `GET /demo/validate?age=30` | Valid input (200) | +| `GET /demo/types` | The registry itself, as JSON | + +## Verify it + +With the app running, in another shell: + +```sh +./verify.sh +``` + +The script curls every endpoint and asserts HTTP status, `application/problem+json` content type, and key body fields. It exits non-zero on the first mismatch. diff --git a/demo/spring-boot/pom.xml b/demo/spring-boot/pom.xml new file mode 100644 index 0000000..cdf9026 --- /dev/null +++ b/demo/spring-boot/pom.xml @@ -0,0 +1,59 @@ + + + 4.0.0 + + + + org.springframework.boot + spring-boot-starter-parent + 4.1.1 + + + + com.example + problem-details-demo + 0.1.0 + jar + + problem-details-demo + Demo Spring Boot app consuming problem-details-java from Maven Central. + + + 17 + 0.1.0 + + + + + org.springframework.boot + spring-boot-starter-web + + + io.github.othmaneataallah + problem-details-core + ${problem-details.version} + + + io.github.othmaneataallah + problem-details-registry + ${problem-details.version} + + + io.github.othmaneataallah + problem-details-spring + ${problem-details.version} + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/demo/spring-boot/src/main/java/com/example/demo/DemoApplication.java b/demo/spring-boot/src/main/java/com/example/demo/DemoApplication.java new file mode 100644 index 0000000..330f3b4 --- /dev/null +++ b/demo/spring-boot/src/main/java/com/example/demo/DemoApplication.java @@ -0,0 +1,16 @@ +package com.example.demo; + +import io.github.othmaneataallah.problemdetails.spring.ProblemDetailAdvice; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Import; + +/** Demo entry point. The advice is registered explicitly via {@code Import}. */ +@SpringBootApplication +@Import(ProblemDetailAdvice.class) +public class DemoApplication { + + public static void main(String[] args) { + SpringApplication.run(DemoApplication.class, args); + } +} diff --git a/demo/spring-boot/src/main/java/com/example/demo/DemoController.java b/demo/spring-boot/src/main/java/com/example/demo/DemoController.java new file mode 100644 index 0000000..ae542a2 --- /dev/null +++ b/demo/spring-boot/src/main/java/com/example/demo/DemoController.java @@ -0,0 +1,104 @@ +package com.example.demo; + +import static com.example.demo.DemoProblems.BALANCE; +import static com.example.demo.DemoProblems.OUT_OF_CREDIT; +import static com.example.demo.DemoProblems.VALIDATION_ERROR; + +import io.github.othmaneataallah.problemdetails.core.ProblemDetail; +import io.github.othmaneataallah.problemdetails.core.ProblemDetailException; +import io.github.othmaneataallah.problemdetails.core.ProblemDetailKey; +import io.github.othmaneataallah.problemdetails.registry.ProblemType; +import io.github.othmaneataallah.problemdetails.registry.ProblemTypeRegistry; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +/** One endpoint per case, so every behavior is curl-verifiable (see verify.sh). */ +@RestController +@RequestMapping("/demo") +public class DemoController { + + private final ProblemTypeRegistry registry; + + public DemoController(ProblemTypeRegistry registry) { + this.registry = registry; + } + + /** Control case: no problem involved. */ + @GetMapping("/ok") + public Map ok() { + return Map.of("message", "ok"); + } + + /** Full problem with a typed extension, built from the registry entry. */ + @GetMapping("/credit") + public String credit() { + ProblemType type = registry.lookup(OUT_OF_CREDIT).orElseThrow(); + throw new ProblemDetailException( + ProblemDetail.builder() + .type(type.getType()) + .title(type.getTitle()) + .status(type.getRecommendedStatus()) + .detail("Your current balance is 30, but that costs 50.") + .instance("https://example.com/instances/1") + .extension(BALANCE, 30) + .build()); + } + + /** No explicit instance: Spring defaults it from the request path. */ + @GetMapping("/missing") + public String missing() { + throw new ProblemDetailException( + ProblemDetail.builder() + .title("No such account.") + .status(404) + .detail("Account 12345 does not exist.") + .build()); + } + + /** No status member: the integrations answer 500. */ + @GetMapping("/boom") + public String boom() { + throw new ProblemDetailException(ProblemDetail.builder().title("Boom.").build()); + } + + /** Validation-style extension: a list of per-field errors. */ + @GetMapping("/validate") + public Map validate(@RequestParam(defaultValue = "30") int age) { + if (age >= 0) { + return Map.of("message", "valid"); + } + ProblemType type = registry.lookup(VALIDATION_ERROR).orElseThrow(); + Map error = new LinkedHashMap<>(); + error.put("detail", "must be a positive integer"); + error.put("pointer", "#/age"); + throw new ProblemDetailException( + ProblemDetail.builder() + .type(type.getType()) + .title(type.getTitle()) + .status(type.getRecommendedStatus()) + .extension(ProblemDetailKey.of("errors", Object.class), List.of(error)) + .build()); + } + + /** Exposes the registry itself, ordered by type URI. */ + @GetMapping("/types") + public List> types() { + return registry.registeredTypes().stream() + .map( + type -> { + Map view = new LinkedHashMap<>(); + view.put("type", type.getType().toString()); + view.put("title", type.getTitle()); + if (type.getRecommendedStatus() != null) { + view.put("status", type.getRecommendedStatus()); + } + return view; + }) + .toList(); + } +} diff --git a/demo/spring-boot/src/main/java/com/example/demo/DemoProblems.java b/demo/spring-boot/src/main/java/com/example/demo/DemoProblems.java new file mode 100644 index 0000000..ca18a8d --- /dev/null +++ b/demo/spring-boot/src/main/java/com/example/demo/DemoProblems.java @@ -0,0 +1,33 @@ +package com.example.demo; + +import io.github.othmaneataallah.problemdetails.core.ProblemDetailKey; +import io.github.othmaneataallah.problemdetails.registry.ProblemType; +import io.github.othmaneataallah.problemdetails.registry.ProblemTypeRegistry; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** Shared problem types and extension keys for the demo endpoints. */ +@Configuration +public class DemoProblems { + + static final ProblemDetailKey BALANCE = ProblemDetailKey.of("balance", Integer.class); + + static final String OUT_OF_CREDIT = "https://example.com/probs/out-of-credit"; + static final String VALIDATION_ERROR = "https://example.net/validation-error"; + + /** Registry modelling an API that reuses documented problem types. */ + @Bean + ProblemTypeRegistry problemTypeRegistry() { + ProblemTypeRegistry registry = new ProblemTypeRegistry(); + registry.register( + ProblemType.of( + OUT_OF_CREDIT, + "You do not have enough credit.", + 403, + "https://example.com/docs/probs")); + registry.register( + ProblemType.of( + VALIDATION_ERROR, "Your request is not valid.", 422, "https://example.net/docs")); + return registry; + } +} diff --git a/demo/spring-boot/src/main/resources/application.properties b/demo/spring-boot/src/main/resources/application.properties new file mode 100644 index 0000000..4c00e40 --- /dev/null +++ b/demo/spring-boot/src/main/resources/application.properties @@ -0,0 +1 @@ +server.port=8080 diff --git a/demo/spring-boot/verify.sh b/demo/spring-boot/verify.sh new file mode 100755 index 0000000..fe69352 --- /dev/null +++ b/demo/spring-boot/verify.sh @@ -0,0 +1,64 @@ +#!/usr/bin/env bash +# Verifies the demo app endpoint by endpoint. The app must be running +# (see README.md). Exits non-zero on the first mismatch. +set -u + +BASE="${BASE_URL:-http://localhost:8080}" +BODY="$(mktemp)" +trap 'rm -f "$BODY"' EXIT + +fail() { + echo "FAIL: $1" + exit 1 +} + +echo "Waiting for $BASE ..." +for _ in $(seq 1 30); do + curl -sf -o /dev/null "$BASE/demo/ok" 2>/dev/null && break + sleep 1 +done +curl -sf -o /dev/null "$BASE/demo/ok" 2>/dev/null || fail "app is not reachable at $BASE" + +check() { # name path expected_status [expected content-type fragment] [expected body fragments...] + local name="$1" path="$2" expected_status="$3" + local expected_type="$4" + shift 4 + local status + status="$(curl -s -o "$BODY" -w "%{http_code}" "$BASE$path")" + [ "$status" = "$expected_status" ] \ + || fail "$name: status $status, expected $expected_status ($(cat "$BODY"))" + if [ -n "$expected_type" ]; then + curl -s -o /dev/null -D - "$BASE$path" \ + | grep -qiF "content-type: $expected_type" \ + || fail "$name: missing content type $expected_type" + fi + for fragment in "$@"; do + grep -qF "$fragment" "$BODY" \ + || fail "$name: body lacks '$fragment' ($(cat "$BODY"))" + done + echo "ok: $name" +} + +JSON="application/problem+json" + +check "control case" /demo/ok 200 "" '"message":"ok"' +check "full problem" /demo/credit 403 "$JSON" \ + '"type":"https://example.com/probs/out-of-credit"' \ + '"status":403' \ + '"balance":30' \ + '"instance":"https://example.com/instances/1"' +check "instance from path" /demo/missing 404 "$JSON" \ + '"status":404' \ + '"instance":"/demo/missing"' +check "statusless fallback" /demo/boom 500 "$JSON" \ + '"title":"Boom."' +check "validation errors" "/demo/validate?age=-5" 422 "$JSON" \ + '"type":"https://example.net/validation-error"' \ + '"status":422' \ + '"pointer":"#/age"' +check "valid input" "/demo/validate?age=30" 200 "" '"message":"valid"' +check "registry listing" /demo/types 200 "" \ + '"type":"about:blank"' \ + '"type":"https://example.com/probs/out-of-credit"' + +echo "All demo checks passed."