Skip to content

Commit 9f18236

Browse files
committed
feat: Add Response.created/notFound/notImplemented factories
1 parent 10a05eb commit 9f18236

2 files changed

Lines changed: 38 additions & 9 deletions

File tree

README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,16 @@ public class PostDataHandler implements RequestHandler {
5959

6060
``` java
6161
Response.empty(); // 204 No Content, no body
62-
Response.status(404); // 404, no body
6362
Response.status(200); // 200 OK, no body
6463
Response.ok(Map.of("id", "42")); // 200 OK, JSON body via TypeMapper
64+
Response.created(newResource); // 201 Created, JSON body
65+
Response.created(newResource, "/things/42"); // 201 Created + Location header
6566
Response.accepted(); // 202 Accepted, no body
6667
Response.accepted(Map.of("jobId", "job-42")); // 202 Accepted, JSON body
67-
Response.of(201, newResource); // any status, JSON body
68+
Response.notFound(); // 404 Not Found, no body
69+
Response.notFound(problemDetail); // 404 Not Found, JSON body
70+
Response.notImplemented(); // 501 Not Implemented, no body
71+
Response.of(409, conflictDetail); // any status, JSON body
6872
Response.text(200, "hello"); // text/plain; UTF-8
6973
Response.bytes(200, pdf, "application/pdf"); // pre-serialised bytes
7074
Response.stream(200, "application/octet-stream", // chunked streaming
@@ -224,8 +228,8 @@ public class GetPromotionHandler implements RequestHandler {
224228
String tenant = TENANT_ID.get();
225229
return promotionService
226230
.find(tenant, id)
227-
.map(p -> Response.of(HTTP_OK, p))
228-
.orElse(Response.status(HTTP_NOT_FOUND));
231+
.<Response>map(Response::ok)
232+
.orElseGet(Response::notFound);
229233
}
230234
}
231235
```
@@ -237,9 +241,6 @@ Gson on the classpath for request/response JSON, SnakeYAML on the classpath for
237241
``` java
238242
package com.example.promotions;
239243

240-
import static java.net.HttpURLConnection.HTTP_NOT_FOUND;
241-
import static java.net.HttpURLConnection.HTTP_OK;
242-
243244
import com.retailsvc.http.OpenApiServer;
244245
import com.retailsvc.http.Request;
245246
import com.retailsvc.http.RequestHandler;
@@ -261,8 +262,8 @@ public final class App {
261262
RequestHandler getPromotion = req -> {
262263
String id = req.pathParam("id");
263264
return PromotionService.find(TENANT.get(), id) // uses bound tenant
264-
.<Response>map(p -> Response.of(HTTP_OK, p)) // 200 + JSON via Gson
265-
.orElseGet(() -> Response.status(HTTP_NOT_FOUND)); // 404, no body
265+
.<Response>map(Response::ok) // 200 + JSON via Gson
266+
.orElseGet(Response::notFound); // 404, no body
266267
};
267268

268269
OpenApiServer.builder()

src/main/java/com/retailsvc/http/Response.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,19 @@ public static Response ok(Object body) {
4848
return new Response(200, body, null, Map.of());
4949
}
5050

51+
/** {@code 201 Created} with {@code body} serialised as JSON. */
52+
public static Response created(Object body) {
53+
return new Response(201, body, null, Map.of());
54+
}
55+
56+
/**
57+
* {@code 201 Created} with {@code body} as JSON and a {@code Location} header — the canonical
58+
* shape for a POST that creates a new resource.
59+
*/
60+
public static Response created(Object body, String location) {
61+
return new Response(201, body, null, Map.of("Location", location));
62+
}
63+
5164
/** {@code 202 Accepted} with no body. Use for fire-and-forget async work. */
5265
public static Response accepted() {
5366
return new Response(202, null, null, Map.of());
@@ -58,6 +71,21 @@ public static Response accepted(Object body) {
5871
return new Response(202, body, null, Map.of());
5972
}
6073

74+
/** {@code 404 Not Found} with no body. */
75+
public static Response notFound() {
76+
return new Response(404, null, null, Map.of());
77+
}
78+
79+
/** {@code 404 Not Found} with {@code body} serialised as JSON (e.g. a ProblemDetail). */
80+
public static Response notFound(Object body) {
81+
return new Response(404, body, null, Map.of());
82+
}
83+
84+
/** {@code 501 Not Implemented} with no body. */
85+
public static Response notImplemented() {
86+
return new Response(501, null, null, Map.of());
87+
}
88+
6189
/** {@code status} with {@code body} serialised by the content-type's {@link TypeMapper}. */
6290
public static Response of(int status, Object body) {
6391
return new Response(status, body, null, Map.of());

0 commit comments

Comments
 (0)