1
- # avaje-http
1
+ # [ Avaje-HTTP] ( https://avaje.io/http/ )
2
+ [ ![ Build] ( https://github.com/avaje/avaje-http/actions/workflows/build.yml/badge.svg )] ( https://github.com/avaje/avaje-http/actions/workflows/build.yml )
3
+ <img src =" https://img.shields.io/maven-central/v/io.avaje/avaje-http-api.svg?label=Maven%20Central " >
4
+ [ ![ License] ( https://img.shields.io/badge/License-Apache%202.0-blue.svg )] ( https://github.com/avaje/avaje-inject/blob/master/LICENSE )
2
5
3
6
HTTP server and client libraries via code generation.
4
7
5
- Documentation at [ avaje.io/http] ( https://avaje.io/http/ )
6
- ## Http Server
8
+ ## HTTP Server
7
9
8
10
A jax-rs style controllers with annotations (` @Path ` , ` @Get ` ...)
9
11
that is lightweight by using source code generation (annotation processors)
@@ -13,37 +15,59 @@ to generate adapter code for Javalin and Helidon SE/Nima.
13
15
- Full use of Javalin or Helidon SE/Nima as desired
14
16
15
17
## Add dependencies
16
-
17
18
``` xml
19
+ <dependency >
20
+ <groupId >io.avaje</groupId >
21
+ <artifactId >avaje-inject</artifactId >
22
+ <version >${avaje-inject.version}</version >
23
+ </dependency >
18
24
<dependency >
19
25
<groupId >io.avaje</groupId >
20
26
<artifactId >avaje-http-api</artifactId >
21
27
<version >${avaje.http.version}</version >
22
28
</dependency >
23
29
```
24
- Add the generator module for your desired microframework as a annotation processor.
30
+ #### Add the generator module for your desired microframework as a annotation processor.
25
31
26
32
``` xml
27
- <build >
28
- <plugins >
29
- <plugin >
30
- <groupId >org.apache.maven.plugins</groupId >
31
- <artifactId >maven-compiler-plugin</artifactId >
32
- <version >${maven-compiler-plugin.version}</version >
33
- <configuration >
34
- <annotationProcessorPaths >
35
- <path >
36
- <groupId >io.avaje</groupId >
37
- <artifactId >avaje-http-javalin-generator</artifactId >
38
- <version >${avaje.http.version}</version >
39
- </path >
40
- </annotationProcessorPaths >
41
- </configuration >
42
- </plugin >
43
- </plugins >
44
- </build >
33
+ <!-- Annotation processors -->
34
+ <dependency >
35
+ <groupId >io.avaje</groupId >
36
+ <artifactId >avaje-inject-generator</artifactId >
37
+ <version >${avaje-inject.version}</version >
38
+ <scope >provided</scope >
39
+ </dependency >
40
+ <dependency >
41
+ <groupId >io.avaje</groupId >
42
+ <artifactId >avaje-http-javalin-generator</artifactId >
43
+ <version >${avaje-http.version}</version >
44
+ <scope >provided</scope >
45
+ </dependency >
46
+ ```
47
+ If there are other annotation processors and they are specified via <i >maven-compiler-plugin</i > then we add avaje-http-generator there instead.
48
+ ``` xml
49
+ <plugin >
50
+ <groupId >org.apache.maven.plugins</groupId >
51
+ <artifactId >maven-compiler-plugin</artifactId >
52
+ <configuration >
53
+ <annotationProcessorPaths > <!-- All annotation processors specified here -->
54
+ <path >
55
+ <groupId >io.avaje</groupId >
56
+ <artifactId >avaje-inject-generator</artifactId >
57
+ <version >${avaje-inject.version}</version >
58
+ </path >
59
+ <path >
60
+ <groupId >io.avaje</groupId >
61
+ <artifactId >avaje-http-javalin-generator</artifactId >
62
+ <version >${avaje-http.version}</version >
63
+ </path >
64
+ <path >
65
+ ... other annotation processor ...
66
+ </path >
67
+ </annotationProcessorPaths >
68
+ </configuration >
69
+ </plugin >
45
70
```
46
-
47
71
## Define a Controller (These APT processors work with both Java and Kotlin.)
48
72
``` java
49
73
package org.example.hello ;
@@ -60,7 +84,7 @@ public class WidgetController {
60
84
public WidgetController (HelloComponent hello ) {
61
85
this . hello = hello;
62
86
}
63
-
87
+
64
88
@Get (" /{id}" )
65
89
Widget getById (int id ) {
66
90
return new Widget (id, " you got it" + hello. hello());
@@ -75,26 +99,31 @@ public class WidgetController {
75
99
}
76
100
```
77
101
78
- ## Usage with Javalin
102
+ ## Usage
103
+ The annotation processor will generate controller adapters that can register routes to Javalin/Helidon. The natural way to use the generated adapters is to get a DI library to find and wire them. This is what the below examples do and they use [ Avaje-Inject] ( https://avaje.io/inject/ ) to do this.
104
+
105
+ Note that there isn't a requirement to use Avaje for dependency injection. Any DI library that can find and wire the generated @Singleton beans can be used. You can even use Dagger2 or Guice to wire the controllers if you so desire.
106
+
107
+ ### Usage with Javalin
79
108
80
109
The annotation processor will generate controller classes implementing the WebRoutes interface, which means we can
81
110
get all the WebRoutes and register them with Javalin using:
82
111
83
112
``` java
84
- var routes = BeanScope . builder(). build(). list(WebRoutes . class);
113
+ var routes = BeanScope . builder(). build(). list(WebRoutes . class);
85
114
86
115
Javalin . create()
87
116
.routes(() - > routes. forEach(WebRoutes :: registerRoutes))
88
117
.start();
89
118
```
90
119
91
- ## Usage with Helidon SE
120
+ ### Usage with Helidon SE
92
121
93
122
The annotation processor will generate controller classes implementing the Helidon Service interface, which we can use
94
123
get all the Services and register them with Helidon ` RoutingBuilder ` .
95
124
96
125
``` java
97
- var routes = BeanScope . builder(). build(). list(Service . class);
126
+ var routes = BeanScope . builder(). build(). list(Service . class);
98
127
var routingBuilder = Routing . builder(). register(routes. stream(). toArray(Service []:: new ));
99
128
WebServer . builder()
100
129
.addMediaSupport(JacksonSupport . create())
@@ -103,13 +132,13 @@ WebServer.builder()
103
132
.start();
104
133
```
105
134
106
- ## Usage with Helidon Nima
135
+ ### Usage with Helidon Nima
107
136
108
137
The annotation processor will generate controller classes implementing the Helidon HttpService interface, which we can use
109
138
get all the services and register them with the Helidon `HttpRouting `.
110
139
111
140
```java
112
- var routes = BeanScope . builder(). build(). list(HttpService . class);
141
+ var routes = BeanScope . builder(). build(). list(HttpService . class);
113
142
final var builder = HttpRouting . builder();
114
143
115
144
for (final HttpService httpService : routes) {
@@ -232,13 +261,13 @@ If [Avaje-Jsonb](https://github.com/avaje/avaje-jsonb) is detected, http generat
232
261
public class WidgetController $Route implements WebRoutes {
233
262
234
263
private final WidgetController controller;
235
- private final JsonType<java.util. List<org.example.hello . WidgetController . Widget > > listWidgetJsonType;
236
- private final JsonType<org.example.hello . WidgetController . Widget > widgetJsonType;
264
+ private final JsonType<List<Widget > > listWidgetJsonType;
265
+ private final JsonType<Widget > widgetJsonType;
237
266
238
267
public WidgetController$Route (WidgetController controller , Jsonb jsonB ) {
239
268
this . controller = controller;
240
- this . listWidgetJsonType = jsonB. type(org.example.hello . WidgetController . Widget . class). list();
241
- this . widgetJsonType = jsonB. type(org.example.hello . WidgetController . Widget . class);
269
+ this . listWidgetJsonType = jsonB. type(Widget . class). list();
270
+ this . widgetJsonType = jsonB. type(Widget . class);
242
271
}
243
272
244
273
@Override
@@ -271,13 +300,13 @@ public class WidgetController$Route implements HttpService {
271
300
272
301
273
302
private final WidgetController controller;
274
- private final JsonType<org.example.hello . WidgetController . Widget > widgetJsonType;
275
- private final JsonType<java.util. List<org.example.hello . WidgetController . Widget > > listWidgetJsonType;
303
+ private final JsonType<Widget > widgetJsonType;
304
+ private final JsonType<List<Widget > > listWidgetJsonType;
276
305
277
306
public WidgetController$Route (WidgetController controller , Jsonb jsonB ) {
278
307
this . controller = controller;
279
- this . widgetJsonType = jsonB. type(org.example.hello . WidgetController . Widget . class);
280
- this . listWidgetJsonType = jsonB. type(org.example.hello . WidgetController . Widget . class). list();
308
+ this . widgetJsonType = jsonB. type(Widget . class);
309
+ this . listWidgetJsonType = jsonB. type(Widget . class). list();
281
310
}
282
311
283
312
@Override
0 commit comments