2
2
3
3
Http server and client libraries and code generation.
4
4
5
- ## http server
5
+ ## Http Server
6
6
7
7
A jax-rs style controllers with annotations (` @Path ` , ` @Get ` ...)
8
8
that is lightweight by using source code generation (annotation processors)
@@ -11,8 +11,39 @@ to generate adapter code for Javalin and Helidon SE/Nima.
11
11
- Lightweight as in 65Kb library + generated source code
12
12
- Full use of Javalin or Helidon SE/Nima as desired
13
13
14
+ ## Add dependencies
14
15
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.)
16
47
``` java
17
48
package org.example.hello ;
18
49
@@ -41,7 +72,6 @@ public class WidgetController {
41
72
42
73
record Widget (int id , String name ){};
43
74
}
44
-
45
75
```
46
76
47
77
## Usage with Javalin
@@ -95,59 +125,39 @@ WebServer.builder()
95
125
### (Javalin ) The generated WidgetController $Route . java is:
96
126
97
127
```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
109
130
public class WidgetController $Route implements WebRoutes {
110
131
111
- private final WidgetController controller;
132
+ private final WidgetController controller;
112
133
113
- public WidgetController$route (WidgetController controller ) {
114
- this . controller = controller;
115
- }
134
+ public WidgetController$Route (WidgetController controller ) {
135
+ this . controller = controller;
136
+ }
116
137
117
138
@Override
118
139
public void registerRoutes () {
119
140
120
141
ApiBuilder . get(" /widgets/{id}" , ctx - > {
121
- int id = asInt(ctx. pathParam(" id" ));
122
- ctx. json(controller. getById(id));
123
142
ctx. status(200 );
143
+ var id = asInt(ctx. pathParam(" id" ));
144
+ var result = controller. getById(id);
145
+ ctx. json(result);
124
146
});
125
147
126
148
ApiBuilder . get(" /widgets" , ctx - > {
127
- ctx. json(controller. getAll());
128
149
ctx. status(200 );
150
+ var result = controller. getAll();
151
+ ctx. json(result);
129
152
});
130
153
131
154
}
155
+
132
156
}
133
157
```
134
158
135
159
### (Helidon SE ) The generated WidgetController $Route . java is:
136
160
```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
-
151
161
@Generated (" io.dinject.helidon-generator" )
152
162
@Singleton
153
163
public class WidgetController $Route implements Service {
@@ -180,19 +190,6 @@ public class WidgetController$Route implements Service {
180
190
### (Helidon Nima ) The generated WidgetController $Route . java is:
181
191
182
192
```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
-
196
193
@Generated (" avaje-helidon-nima-generator" )
197
194
@Component
198
195
public class WidgetController $Route implements HttpService {
@@ -224,22 +221,49 @@ public class WidgetController$Route implements HttpService {
224
221
}
225
222
```
226
223
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.
228
226
227
+ ### (Javalin ) The generated WidgetController $Route . java is:
229
228
```java
230
- package org. example. hello;
229
+ @Generated (" avaje-javalin-generator" )
230
+ @Component
231
+ public class WidgetController $Route implements WebRoutes {
231
232
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
+ });
233
252
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
+ }
242
260
261
+ }
262
+ ```
263
+
264
+ ### (Helidon Nima ) The generated WidgetController $Route . java is:
265
+
266
+ ```java
243
267
@Generated (" avaje-helidon-nima-generator" )
244
268
@Component
245
269
public class WidgetController $Route implements HttpService {
0 commit comments