Additional Request Completion Methods that support Futures
See original GitHub issueDescribe the feature
Adding more request completion methods (eg: Context#result(...)
that support futures as you don’t always need to specify a response body for a request, sometimes a Status Code of 401 Unauthorized is fine.
Additional context
Some of the methods that I am thinking of are as follows but please note that all the methods outlined assume that the generic type variable from the CompletableFuture is Nullable.
Context#status(int)
Context#status(CompletableFuture<Integer>)
Context#json(Any)
Context#json(CompletableFuture<Any>)
Context#result(String)
Context#result(CompleteableFuture<String>)
Context#result(byte[])
Context#result(CompleteableFuture<Byte[]>)
Context#(InputStream)
Context#result(CompleteableFuture<InputStream>)
One useful usecase would be if you are querying for a document from a database and then going to return that response (if any) as json as it makes for neater code:
Context#json(CompletableFuture.supplyAsync(() -> {
final Document document = this.mongo.find(Filters.eq("_id", ctx.pathParam("id", ObjectId.class).get())).first();
if (document == null) {
ctx.status(HttpStatus.NO_CONTENT_204);
return null; // Hello Javalin I don't have any Object for you to convert to Json using JavalinJson do not set the ContentType.JSON do not add a Response Body please.
}
Context#status(HttpStatus.OK_200);
return document; // Hello Javalin please convert this Object to Json using JavalinJson
}));
One thing to note about this example is serializing a Bson Document to json isn’t going to give your returned response as a valid response you would want. You actually have to tell Bson to write as a json string first, for example:
ctx -> {
ctx.status(HttpStatus.OK);
ctx.contentType(ContentType.JSON);
ctx.result(Document#toJson(JsonWriterSettings, Encoder));
}
This leads me onto the next thing; having a Context#json(String jsonString)
method that sets the Content Type to JSON and then sets the body to the jsonString value would also be useful. Sometimes I already have the jsonString due to Bson so I can’t use Context#json(String)
because that then serializes my jsonString AGAIN, causing issues. Also in case I haven’t mentioned this, Context#json(String)
does not support a null argument so I have to hack around all of this with:
Context.result(CompleteableFuture.supplyAsync(() -> {
final Document document = this.mongo.find(Filters.eq("_id", ctx.pathParam("id", ObjectId.class).get())).first();
if (document == null) {
ctx.status(HttpStatus.NO_CONTENT_204);
return null;
}
ctx.statusCode(HttpStatus.OK_200);
ctx.contentType(ContentType.JSON); // Notice how we specify this all of the time?
return this.gson.toJson(document); // Again, for Mongo you'd do Document#toJson(JsonWriterSettings, Encoder) instead of gson.
}));
Issue Analytics
- State:
- Created 2 years ago
- Comments:25 (25 by maintainers)
Top GitHub Comments
Merged a change here: https://github.com/tipsy/javalin/commit/b0a2e1abda4cd10a22960f2e5a1f0b31309851d4
It’s now:
And these assertions pass: