Skip to content

Commit

Permalink
Improve Reactive Routes documentation for ending the response
Browse files Browse the repository at this point in the history
  • Loading branch information
loicmathieu committed Nov 19, 2020
1 parent 78117f2 commit da98cb5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docs/src/main/asciidoc/reactive-routes.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ A route method must be a non-private non-static method of a CDI bean.
If the annotated method returns `void` then it has to accept at least one argument - see the supported types below.
If the annotated method does not return `void` then the arguments are optional.

NOTE: Methods that return `void` must __end__ the response or the HTTP request to this route will never end.
Some methods of `RoutingExchange` do it for you, others not and you must call the `end()` method of the response by yourself, please refer to its JavaDoc for more information.

A route method can accept arguments of the following types:

* `io.vertx.ext.web.RoutingContext`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@
*/
public interface RoutingExchange {

/**
* @return the underlying Vert.x routing context.
*/
RoutingContext context();

/**
* @return the HTTP request object
*/
default HttpServerRequest request() {
return context().request();
}
Expand All @@ -29,30 +35,56 @@ default Optional<String> getParam(String paramName) {

/**
*
* @param paramName
* @param headerName
* @return the first header value with the specified name
* @see HttpServerRequest#getHeader(CharSequence)
*/
default Optional<String> getHeader(CharSequence headerName) {
return Optional.ofNullable(request().getHeader(headerName));
}

/**
* @return the HTTP response object
*/
default HttpServerResponse response() {
return context().response();
}

/**
* Set the response status code to 200 and return the response.
* You must call <code>HttpServerResponse.end()</code> afterwards to end the response.
*
* @return the HTTP response object
*/
default HttpServerResponse ok() {
return response().setStatusCode(200);
}

/**
* Set the response status code to 200, write a chunk of data to the response then ends it.
*
* @param chunk
*/
default void ok(String chunk) {
ok().end(chunk);
}

/**
* Set the response status code to 200 and return the response.

This comment has been minimized.

Copy link
@Eng-Fouad

Eng-Fouad Aug 4, 2021

Contributor

typo in comment: status code should be 500 not 200

@loicmathieu @gsmet

This comment has been minimized.

Copy link
@gsmet

gsmet Aug 4, 2021

Member

Good catch. Care to provide a PR?

This comment has been minimized.

Copy link
@Eng-Fouad

Eng-Fouad Aug 4, 2021

Contributor
* You must call <code>HttpServerResponse.end()</code> afterwards to end the response.
*
* @return the HTTP response object
*/
default HttpServerResponse serverError() {
return response().setStatusCode(500);
}

/**
* Set the response status code to 200 and return the response.

This comment has been minimized.

Copy link
@Eng-Fouad

Eng-Fouad Aug 4, 2021

Contributor

typo in comment: status code should be 404 not 200

@loicmathieu @gsmet

* You must call <code>HttpServerResponse.end()</code> afterwards to end the response.
*
* @return the HTTP response object
*/
default HttpServerResponse notFound() {
return response().setStatusCode(404);
}
Expand Down

0 comments on commit da98cb5

Please sign in to comment.