@@ -59,12 +59,16 @@ public class PostDataHandler implements RequestHandler {
5959
6060``` java
6161Response . empty(); // 204 No Content, no body
62- Response . status(404 ); // 404, no body
6362Response . status(200 ); // 200 OK, no body
6463Response . 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
6566Response . accepted(); // 202 Accepted, no body
6667Response . 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
6872Response . text(200 , " hello" ); // text/plain; UTF-8
6973Response . bytes(200 , pdf, " application/pdf" ); // pre-serialised bytes
7074Response . 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
238242package com.example.promotions ;
239243
240- import static java.net.HttpURLConnection.HTTP_NOT_FOUND ;
241- import static java.net.HttpURLConnection.HTTP_OK ;
242-
243244import com.retailsvc.http.OpenApiServer ;
244245import com.retailsvc.http.Request ;
245246import 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()
0 commit comments