Skip to content

Commit 42b603c

Browse files
authored
Merge pull request #3858 from jooby-project/newdoc
documentation: find a way to organize/group/classify documentation be…
2 parents 37af7b2 + 4cd18a0 commit 42b603c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+3030
-4214
lines changed

docs/asciidoc/body.adoc

Lines changed: 48 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
=== Request Body
1+
==== Request Body
22

3-
Raw `request body` is available via javadoc:Context[body] method:
3+
The raw request body is available via the javadoc:Context[body] method:
44

55
.Java
66
[source,java,role="primary"]
77
----
88
{
99
post("/string", ctx -> {
1010
String body = ctx.body().value(); // <1>
11-
...
11+
// ...
1212
});
1313
1414
post("/bytes", ctx -> {
1515
byte[] body = ctx.body().bytes(); // <2>
16-
...
16+
// ...
1717
});
1818
1919
post("/stream", ctx -> {
2020
InputStream body = ctx.body().stream(); // <3>
21-
...
21+
// ...
2222
});
2323
}
2424
----
@@ -29,57 +29,52 @@ Raw `request body` is available via javadoc:Context[body] method:
2929
{
3030
post("/string") {
3131
val body = ctx.body().value() // <1>
32-
...
32+
// ...
3333
}
3434
3535
post("/bytes") {
3636
val body = ctx.body().bytes() // <2>
37-
...
37+
// ...
3838
}
3939
4040
post("/stream") {
4141
val body = ctx.body().stream() // <3>
42-
...
42+
// ...
4343
}
4444
}
4545
----
4646

47-
<1> `HTTP Body` as `String`
48-
<2> `HTTP Body` as `byte array`
49-
<3> `HTTP Body` as `InputStream`
47+
<1> Reads the HTTP body as a `String`.
48+
<2> Reads the HTTP body as a `byte array`.
49+
<3> Reads the HTTP body as an `InputStream`.
5050

51-
This gives us the `raw body`.
51+
===== Message Decoder
5252

53-
==== Message Decoder
54-
55-
Request body parsing is achieved using the javadoc:MessageDecoder[] functional interface.
53+
Request body parsing (converting the raw body into a specific object) is handled by the javadoc:MessageDecoder[] functional interface.
5654

5755
[source, java]
5856
----
5957
public interface MessageDecoder {
60-
6158
<T> T decode(Context ctx, Type type) throws Exception;
6259
}
6360
----
6461

65-
javadoc:MessageDecoder[] has a single `decode` method that takes two input arguments: `(context, type)`
66-
and returns a single result of the given type.
62+
The javadoc:MessageDecoder[] has a single `decode` method that takes the request context and the target type, returning the parsed result.
6763

68-
.JSON example:
64+
.JSON Decoder Example:
6965
[source, java, role="primary"]
7066
----
7167
{
7268
FavoriteJson lib = new FavoriteJson(); // <1>
7369
74-
decoder(MediaType.json, (ctx, type) -> { // <2>
75-
70+
decoder(MediaType.json, (ctx, type) -> { // <2>
7671
byte[] body = ctx.body().bytes(); // <3>
77-
7872
return lib.fromJson(body, type); // <4>
7973
});
8074
8175
post("/", ctx -> {
8276
MyObject myObject = ctx.body(MyObject.class); // <5>
77+
return myObject;
8378
});
8479
}
8580
----
@@ -90,40 +85,37 @@ and returns a single result of the given type.
9085
{
9186
val lib = FavoriteJson() // <1>
9287
93-
decoder(MediaType.json) { ctx, type -> // <2>
94-
88+
decoder(MediaType.json) { ctx, type -> // <2>
9589
val body = ctx.body().bytes() // <3>
96-
9790
lib.fromJson(body, type) // <4>
9891
}
9992
10093
post("/") {
10194
val myObject = ctx.body<MyObject>() // <5>
95+
myObject
10296
}
10397
}
10498
----
10599

106-
<1> Choose your favorite `json` library
107-
<2> Check if the `Content-Type` header matches `application/json`
108-
<3> Read the body as `byte[]`
109-
<4> Parse the `body` and use the requested type
110-
<5> Route handler now call the `body(Type)` function to trigger the decoder function
100+
<1> Initialize your favorite JSON library.
101+
<2> Register the decoder to trigger when the `Content-Type` header matches `application/json`.
102+
<3> Read the raw body as a `byte[]`.
103+
<4> Parse the payload into the requested type.
104+
<5> Inside the route, calling `ctx.body(Type)` automatically triggers the registered decoder.
111105

112-
=== Response Body
106+
==== Response Body
113107

114-
Response body is generated from `handler` function:
108+
The response body is generated by the route handler.
115109

116-
.Response body
110+
.Response Body Example
117111
[source, java,role="primary"]
118112
----
119113
{
120114
get("/", ctx -> {
121-
ctx.setResponseCode(200); // <1>
122-
115+
ctx.setResponseCode(200); // <1>
123116
ctx.setResponseType(MediaType.text); // <2>
124-
125117
ctx.setResponseHeader("Date", new Date()); // <3>
126-
118+
127119
return "Response"; // <4>
128120
});
129121
}
@@ -135,56 +127,46 @@ Response body is generated from `handler` function:
135127
{
136128
get("/") {
137129
ctx.responseCode = 200 // <1>
138-
139130
ctx.responseType = MediaType.text // <2>
140-
141131
ctx.setResponseHeader("Date", Date()) // <3>
142-
132+
143133
"Response" // <4>
144134
}
145135
}
146136
----
147137

148-
<1> Set `status code` to `OK(200)`. This is the default `status code`
149-
<2> Set `content-type` to `text/plain`. This is the default `content-type`
150-
<3> Set the `date` header
151-
<4> Send a `Response` string to the client
138+
<1> Set the status code to `200 OK` (this is the default).
139+
<2> Set the `Content-Type` to `text/plain` (this is the default for strings).
140+
<3> Set a custom response header.
141+
<4> Return the response body to the client.
152142

153-
==== Message Encoder
143+
===== Message Encoder
154144

155-
Response encoding is achieved using the javadoc:MessageEncoder[] functional interface.
145+
Response encoding (converting an object into a raw HTTP response) is handled by the javadoc:MessageEncoder[] functional interface.
156146

157147
[source, java]
158148
----
159149
public interface MessageEncoder {
160-
161150
Output encode(@NonNull Context ctx, @NonNull Object value) throws Exception;
162151
}
163152
----
164153

165-
MessageEncoder has a single `encode` method that accepts two input arguments: `(context, value)` and
166-
produces a result.
154+
The javadoc:MessageEncoder[] has a single `encode` method that accepts the context and the value returned by the handler, producing an output. (Internally, javadoc:output.Output[] works like a `java.nio.ByteBuffer` for performance reasons).
167155

168-
The javadoc:output.Output[] works like `java.nio.ByteBuffer` and it is used internally
169-
for performance reason.
170-
171-
.JSON example:
156+
.JSON Encoder Example:
172157
[source, java, role="primary"]
173158
----
174159
{
175160
FavoriteJson lib = new FavoriteJson(); // <1>
176161
177-
encoder(MediaType.json, (ctx, result) -> { // <2>
178-
162+
encoder(MediaType.json, (ctx, result) -> { // <2>
179163
String json = lib.toJson(result); // <3>
180-
181164
ctx.setDefaultResponseType(MediaType.json); // <4>
182-
183165
return json; // <5>
184166
});
185167
186168
get("/item", ctx -> {
187-
MyObject myObject = ...;
169+
MyObject myObject = new MyObject();
188170
return myObject; // <6>
189171
});
190172
}
@@ -196,25 +178,22 @@ for performance reason.
196178
{
197179
val lib = FavoriteJson() // <1>
198180
199-
encoder(MediaType.json) { ctx, result -> // <2>
200-
181+
encoder(MediaType.json) { ctx, result -> // <2>
201182
val json = lib.toJson(result) // <3>
202-
203183
ctx.defaultResponseType = MediaType.json // <4>
204-
205184
json // <5>
206185
}
207186
208187
get("/item") {
209-
val myObject = ...;
188+
val myObject = MyObject()
210189
myObject // <6>
211190
}
212191
}
213192
----
214193

215-
<1> Choose your favorite `json` library
216-
<2> Check if the `Accept` header matches `application/json`
217-
<3> Convert `result` to `JSON`
218-
<4> Set default `Content-Type` to `application/json`
219-
<5> Produces JSON response
220-
<6> Route handler returns a user defined type
194+
<1> Initialize your favorite JSON library.
195+
<2> Register the encoder to trigger when the client's `Accept` header matches `application/json`.
196+
<3> Convert the route's result into JSON.
197+
<4> Set the `Content-Type` header to `application/json`.
198+
<5> Return the encoded JSON payload.
199+
<6> The route handler returns a user-defined POJO, which is automatically intercepted and encoded.

0 commit comments

Comments
 (0)