Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions demo/spring-boot/README.md
Original file line number Diff line number Diff line change
@@ -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 <http://localhost:8080>. 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.
59 changes: 59 additions & 0 deletions demo/spring-boot/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<!-- Standalone consumer build: intentionally NOT part of the library reactor. -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>4.1.1</version>
<relativePath />
</parent>

<groupId>com.example</groupId>
<artifactId>problem-details-demo</artifactId>
<version>0.1.0</version>
<packaging>jar</packaging>

<name>problem-details-demo</name>
<description>Demo Spring Boot app consuming problem-details-java from Maven Central.</description>

<properties>
<java.version>17</java.version>
<problem-details.version>0.1.0</problem-details.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.github.othmaneataallah</groupId>
<artifactId>problem-details-core</artifactId>
<version>${problem-details.version}</version>
</dependency>
<dependency>
<groupId>io.github.othmaneataallah</groupId>
<artifactId>problem-details-registry</artifactId>
<version>${problem-details.version}</version>
</dependency>
<dependency>
<groupId>io.github.othmaneataallah</groupId>
<artifactId>problem-details-spring</artifactId>
<version>${problem-details.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -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);
}
}
104 changes: 104 additions & 0 deletions demo/spring-boot/src/main/java/com/example/demo/DemoController.java
Original file line number Diff line number Diff line change
@@ -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<String, String> 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<String, String> validate(@RequestParam(defaultValue = "30") int age) {
if (age >= 0) {
return Map.of("message", "valid");
}
ProblemType type = registry.lookup(VALIDATION_ERROR).orElseThrow();
Map<String, Object> 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<Map<String, Object>> types() {
return registry.registeredTypes().stream()
.map(
type -> {
Map<String, Object> 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();
}
}
33 changes: 33 additions & 0 deletions demo/spring-boot/src/main/java/com/example/demo/DemoProblems.java
Original file line number Diff line number Diff line change
@@ -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<Integer> 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;
}
}
1 change: 1 addition & 0 deletions demo/spring-boot/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
server.port=8080
64 changes: 64 additions & 0 deletions demo/spring-boot/verify.sh
Original file line number Diff line number Diff line change
@@ -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."
Loading