22
33Http server and client libraries and code generation.
44
5- ## http server
5+ ## Http Server
66
77A jax-rs style controllers with annotations (` @Path ` , ` @Get ` ...)
88that is lightweight by using source code generation (annotation processors)
@@ -11,8 +11,39 @@ to generate adapter code for Javalin and Helidon SE/Nima.
1111- Lightweight as in 65Kb library + generated source code
1212- Full use of Javalin or Helidon SE/Nima as desired
1313
14+ ## Add dependencies
1415
15- ## Define a Controller (Note that these APT processors works with both Java and Kotlin.)
16+ ``` xml
17+ <dependency >
18+ <groupId >io.avaje</groupId >
19+ <artifactId >avaje-http-api</artifactId >
20+ <version >${avaje.http.version}</version >
21+ </dependency >
22+ ```
23+ Add the generator module for your desired microframework as a annotation processor.
24+
25+ ``` xml
26+ <build >
27+ <plugins >
28+ <plugin >
29+ <groupId >org.apache.maven.plugins</groupId >
30+ <artifactId >maven-compiler-plugin</artifactId >
31+ <version >${maven-compiler-plugin.version}</version >
32+ <configuration >
33+ <annotationProcessorPaths >
34+ <path >
35+ <groupId >io.avaje</groupId >
36+ <artifactId >avaje-http-javalin-generator</artifactId >
37+ <version >${avaje.http.version}</version >
38+ </path >
39+ </annotationProcessorPaths >
40+ </configuration >
41+ </plugin >
42+ </plugins >
43+ </build >
44+ ```
45+
46+ ## Define a Controller (These APT processors work with both Java and Kotlin.)
1647``` java
1748package org.example.hello ;
1849
@@ -41,7 +72,6 @@ public class WidgetController {
4172
4273 record Widget (int id , String name ){};
4374}
44-
4575```
4676
4777## Usage with Javalin
@@ -95,59 +125,39 @@ WebServer.builder()
95125### (Javalin ) The generated WidgetController $Route . java is:
96126
97127```java
98- package org. example. hello;
99-
100- import static io.avaje.http.api. PathTypeConversion . * ;
101- import io.avaje.http.api. WebRoutes ;
102- import io.javalin.apibuilder. ApiBuilder ;
103- import javax.annotation. Generated ;
104- import javax.inject. Singleton ;
105- import org.example.hello. WidgetController ;
106-
107- @Generated (" io.avaje.javalin-generator" )
108- @Singleton
128+ @Generated (" avaje-javalin-generator" )
129+ @Component
109130public class WidgetController $Route implements WebRoutes {
110131
111- private final WidgetController controller;
132+ private final WidgetController controller;
112133
113- public WidgetController$route (WidgetController controller ) {
114- this . controller = controller;
115- }
134+ public WidgetController$Route (WidgetController controller ) {
135+ this . controller = controller;
136+ }
116137
117138 @Override
118139 public void registerRoutes () {
119140
120141 ApiBuilder . get(" /widgets/{id}" , ctx - > {
121- int id = asInt(ctx. pathParam(" id" ));
122- ctx. json(controller. getById(id));
123142 ctx. status(200 );
143+ var id = asInt(ctx. pathParam(" id" ));
144+ var result = controller. getById(id);
145+ ctx. json(result);
124146 });
125147
126148 ApiBuilder . get(" /widgets" , ctx - > {
127- ctx. json(controller. getAll());
128149 ctx. status(200 );
150+ var result = controller. getAll();
151+ ctx. json(result);
129152 });
130153
131154 }
155+
132156}
133157```
134158
135159### (Helidon SE ) The generated WidgetController $Route . java is:
136160```java
137- package org. example. hello;
138-
139- import static io.avaje.http.api. PathTypeConversion . * ;
140-
141- import io. avaje. http. api. * ;
142- import io.helidon.common.http. FormParams ;
143- import io.helidon.webserver. Handler ;
144- import io.helidon.webserver. Routing ;
145- import io.helidon.webserver. ServerRequest ;
146- import io.helidon.webserver. ServerResponse ;
147- import io.helidon.webserver. Service ;
148- import jakarta.inject. Singleton ;
149- import org.example.hello. WidgetController ;
150-
151161@Generated (" io.dinject.helidon-generator" )
152162@Singleton
153163public class WidgetController $Route implements Service {
@@ -180,19 +190,6 @@ public class WidgetController$Route implements Service {
180190### (Helidon Nima ) The generated WidgetController $Route . java is:
181191
182192```java
183- package org. example. hello;
184-
185- import static io.avaje.http.api. PathTypeConversion . * ;
186-
187- import io. avaje. http. api. * ;
188- import io.avaje.inject. Component ;
189- import io.helidon.nima.webserver.http. HttpRouting ;
190- import io.helidon.nima.webserver.http. HttpRules ;
191- import io.helidon.nima.webserver.http. HttpService ;
192- import io.helidon.nima.webserver.http. ServerRequest ;
193- import io.helidon.nima.webserver.http. ServerResponse ;
194- import org.example.hello. WidgetController ;
195-
196193@Generated (" avaje-helidon-nima-generator" )
197194@Component
198195public class WidgetController $Route implements HttpService {
@@ -224,22 +221,49 @@ public class WidgetController$Route implements HttpService {
224221}
225222```
226223
227- ### (Helidon Nima with Avaje - Jsonb ) The generated WidgetController $Route . java is:
224+ ## Generated sources ([Avaje - Jsonb ](https: // github.com/avaje/avaje-jsonb))
225+ If [Avaje - Jsonb ](https: // github.com/avaje/avaje-jsonb) is detected, http generators with support will use it for faster Json message processing.
228226
227+ ### (Javalin ) The generated WidgetController $Route . java is:
229228```java
230- package org. example. hello;
229+ @Generated (" avaje-javalin-generator" )
230+ @Component
231+ public class WidgetController $Route implements WebRoutes {
231232
232- import static io.avaje.http.api. PathTypeConversion . * ;
233+ private final WidgetController controller;
234+ private final JsonType<java.util.List<org.example.hello. WidgetController . Widget > > listWidgetJsonType;
235+ private final JsonType<org.example.hello. WidgetController . Widget > widgetJsonType;
236+
237+ public WidgetController$Route (WidgetController controller , Jsonb jsonB ) {
238+ this . controller = controller;
239+ this . listWidgetJsonType = jsonB. type(org.example.hello. WidgetController . Widget . class). list();
240+ this . widgetJsonType = jsonB. type(org.example.hello. WidgetController . Widget . class);
241+ }
242+
243+ @Override
244+ public void registerRoutes () {
245+
246+ ApiBuilder . get(" /widgets/{id}" , ctx - > {
247+ ctx. status(200 );
248+ var id = asInt(ctx. pathParam(" id" ));
249+ var result = controller. getById(id);
250+ widgetJsonType. toJson(result, ctx. contentType(" application/json" ). outputStream());
251+ });
233252
234- import io. avaje. http. api. * ;
235- import io.avaje.inject. Component ;
236- import io.helidon.nima.webserver.http. HttpRouting ;
237- import io.helidon.nima.webserver.http. HttpRules ;
238- import io.helidon.nima.webserver.http. HttpService ;
239- import io.helidon.nima.webserver.http. ServerRequest ;
240- import io.helidon.nima.webserver.http. ServerResponse ;
241- import org.example.hello. WidgetController ;
253+ ApiBuilder . get(" /widgets" , ctx - > {
254+ ctx. status(200 );
255+ var result = controller. getAll();
256+ listWidgetJsonType. toJson(result, ctx. contentType(" application/json" ). outputStream());
257+ });
258+
259+ }
242260
261+ }
262+ ```
263+
264+ ### (Helidon Nima ) The generated WidgetController $Route . java is:
265+
266+ ```java
243267@Generated (" avaje-helidon-nima-generator" )
244268@Component
245269public class WidgetController $Route implements HttpService {
0 commit comments