Skip to content

Commit

Permalink
Clarify and expand on SE server completion
Browse files Browse the repository at this point in the history
  • Loading branch information
tjquinno committed Oct 31, 2022
1 parent a636db1 commit 75bd507
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
4 changes: 3 additions & 1 deletion docs/includes/openapi/openapi-generator.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -505,11 +505,13 @@ The Helidon generators go a long way in helping you write your client or server.
// end::using-generated-code-intro[]
// tag::using-generated-code-server[]
// tag::using-generated-code-server-intro[]
=== Completing the Server
Recall earlier how the OpenAPI generator gathers operations into one or more "APIs" and generates either a class or an interface--your choice--for each API.
Recall from earlier how the OpenAPI generator gathers operations into one or more "APIs" and generates either a class or an interface--your choice--for each API.
You need to extend each generated API class or implement each generated API interface by writing your own classes.
Any input parameters to the endpoints are expressed as POJO model objects or Java types, as declared in the OpenAPI document. Your server code uses each of the input parameters to accomplish whatever business purpose that endpoint is responsible for, possibly returning a result as a POJO or Java type as indicated for that operation in the OpenAPI document.
// end::using-generated-code-server-intro[]
In some cases, you might need more control over the response sent to the client. In that case, specify the additional property `returnResponse=true` when you run the Helidon server generator. The return type for the generated methods is
ifdef::mp-flavor[]
Expand Down
55 changes: 54 additions & 1 deletion docs/se/openapi/openapi-generator.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,60 @@ include::{rootdir}/includes/pages.adoc[]
:helidon-client-xref: {webclient-page}
include::{gen-inc}[tag=preamble]
include::{gen-inc}[tags=intro;coords;config;usage;using-generated-code-intro;using-generated-code-server;using-generated-code-client-intro]
include::{gen-inc}[tags=intro;coords;config;usage;using-generated-code-intro;using-generated-code-server-intro]
The Helidon SE server generator also creates, for each API, a separate class containing handler methods for each endpoint.
Along with the `PetService` interface or abstract class which has methods such as `addPet` and `getPetById`, the tool generates `PetServiceImpl` with methods such as `handleAddPet` and `handleGetPetById`.
[source,java]
.Generated `PetService` abstract class
----
public abstract class PetService implements Service {
void addPet(ServerRequest request, ServerResponse response, Pet pet) {
// ...
}
abstract void handleAddPet(ServerRequest request, ServerResponse response, Pet pet);
void getPetById(ServerRequest request, ServerResponse response) {
// ...
}
abstract void handleGetPetById(ServerRequest request, ServerResponse response, Long petId);
}
----
[source,java]
.Generated skeleton `PetServiceImpl` class (which you extend)
----
public class PetServiceImpl extends PetService {
public void handleAddPet(ServerRequest request, ServerResponse response, Pet pet) {
response.status(HTTP_CODE_NOT_IMPLEMENTED.send());
}
public void handleGetPetById(ServerRequest request, ServerResponse response, Long petId) {
response.status(HTTP_CODE_NOT_IMPLEMENTED).send();
}
}
----
You write your own classes which extend `PetServiceImpl` and the other generated `xxxImpl` classes, overriding the `handle...` methods.
You have control over--and therefore responsibility for--preparing the response to be sent to the client, including the status, any response headers, and any returned entity.
Your overriding implementation of `handleGetPetById` might look like the following example.
[source,java]
.Example override of `handleGetPetById`
----
public void handleGetPetById(ServerRequest request, ServerResponse response, Long petId) {
Pet pet = locatePetInDatabase(petId);
if (pet == null) {
response.status(404).send();
}
response.send(pet); // Respnose status is 200 by default.
}
}
----
include::{gen-inc}[tag=using-generated-code-client-intro]
The generated Helidon SE client includes the class `ApiClient`. This class corresponds to
the Helidon link:{webclient-javadoc-base-url}/io/helidon/webclient/WebClient.html[`WebClient`] and represents the connection between your code and the remote server. The generator also creates one or more `Api` interfaces and corresponding implementation classes. The examples below use the `PetApi` interface and the `PetApiImpl` class.
Expand Down

0 comments on commit 75bd507

Please sign in to comment.