2
2
[ ![ Maven Central] ( https://img.shields.io/maven-central/v/io.avaje/avaje-http-client.svg?label=Maven%20Central )] ( https://mvnrepository.com/artifact/io.avaje/avaje-http-client )
3
3
[ ![ License] ( https://img.shields.io/badge/License-Apache%202.0-blue.svg )] ( https://github.com/avaje/avaje-http-client/blob/master/LICENSE )
4
4
5
- # avaje-http-client
6
-
7
- Documentation at [ avaje.io/http-client] ( https://avaje.io/http-client/ )
5
+ # [ Avaje-HTTP-Client] ( https://avaje.io/http-client/ )
8
6
9
7
A lightweight wrapper to the [ JDK 11+ Java Http Client] ( http://openjdk.java.net/groups/net/httpclient/intro.html )
10
8
@@ -28,26 +26,28 @@ A lightweight wrapper to the [JDK 11+ Java Http Client](http://openjdk.java.net/
28
26
</dependency >
29
27
```
30
28
31
- ### Create HttpClientContext
29
+ ### Create HttpClient
32
30
33
- Create a HttpClientContext with a baseUrl, Jackson or Gson based JSON
31
+ Create a HttpClient with a baseUrl, Jackson or Gson based JSON
34
32
body adapter, logger.
35
33
36
34
``` java
37
- public HttpClientContext client() {
38
- return HttpClientContext . builder()
39
- .baseUrl(baseUrl)
40
- .bodyAdapter(new JsonbBodyAdapter ())
41
- // .bodyAdapter(new JacksonBodyAdapter(new ObjectMapper()))
42
- // .bodyAdapter(new GsonBodyAdapter(new Gson()))
43
- .build();
44
- }
45
-
35
+ HttpClient client = HttpClient . builder()
36
+ .baseUrl(baseUrl)
37
+ .bodyAdapter(new JsonbBodyAdapter ())
38
+ // .bodyAdapter(new JacksonBodyAdapter(new ObjectMapper()))
39
+ // .bodyAdapter(new GsonBodyAdapter(new Gson()))
40
+ .build();
41
+
42
+ HttpResponse<String > hres = client. request()
43
+ .path(" hello" )
44
+ . GET ()
45
+ .asString();
46
46
```
47
47
48
48
## Requests
49
49
50
- From HttpClientContext :
50
+ From HttpClient :
51
51
- Create a request
52
52
- Build the url via path(), matrixParam(), queryParam()
53
53
- Optionally set headers(), cookies() etc
@@ -78,18 +78,43 @@ From HttpClientContext:
78
78
79
79
#### Example GET as String
80
80
``` java
81
- HttpResponse<String > hres = clientContext. request()
81
+ HttpClient client = HttpClient . builder()
82
+ .baseUrl(baseUrl)
83
+ .build();
84
+
85
+ HttpResponse<String > hres = client. request()
82
86
.path(" hello" )
83
87
. GET ()
84
88
.asString();
85
89
```
86
90
87
91
#### Example GET as JSON marshalling into a java class/dto
88
92
``` java
89
- CustomerDto customer = clientContext. request()
93
+ HttpResponse<CustomerDto > customer = client. request()
94
+ .path(" customers" ). path(42 )
95
+ . GET ()
96
+ .as(CustomerDto . class);
97
+
98
+ // just get the bean without HttpResponse
99
+ CustomerDto customer = client. request()
90
100
.path(" customers" ). path(42 )
91
101
. GET ()
92
102
.bean(CustomerDto . class);
103
+
104
+ // get a List
105
+ HttpResponse<List<CustomerDto > > customers = client. request()
106
+ .path(" customers" )
107
+ .queryParam(" active" , " true" )
108
+ . GET ()
109
+ .asList(CustomerDto . class);
110
+
111
+
112
+ // get a Stream - `application/x-json-stream`
113
+ HttpResponse<List<CustomerDto > > customers = client. request()
114
+ .path(" customers/stream" )
115
+ . GET ()
116
+ .asStream(CustomerDto . class);
117
+
93
118
```
94
119
95
120
#### Example Async GET as String
@@ -98,7 +123,7 @@ CustomerDto customer = clientContext.request()
98
123
- In the example below hres is of type HttpResponse< ; String> ;
99
124
100
125
``` java
101
- clientContext . request()
126
+ client . request()
102
127
.path(" hello" )
103
128
. GET ()
104
129
.async(). asString() // CompletableFuture<HttpResponse<String>>
@@ -124,6 +149,9 @@ Overview of response types for sync calls.
124
149
<tr ><td ><b >sync processing</b ></td ><td >  ; </td ></tr >
125
150
<tr ><td >asVoid</td ><td >HttpResponse< ; Void> ; </td ></tr >
126
151
<tr ><td >asString</td ><td >HttpResponse< ; String> ; </td ></tr >
152
+ <tr ><td >as< ; E> </td ><td >HttpResponse< ; E> ; </td ></tr >
153
+ <tr ><td >asList< ; E> </td ><td >HttpResponse< ; List< ; E> ;> ; </td ></tr >
154
+ <tr ><td >asStream< ; E> </td ><td >HttpResponse< ; Stream< ; E> ;> ; </td ></tr >
127
155
<tr ><td >bean< ; E> </td ><td >E</td ></tr >
128
156
<tr ><td >list< ; E> </td ><td >List< ; E> ; </td ></tr >
129
157
<tr ><td >stream< ; E> </td ><td >Stream< ; E> ; </td ></tr >
@@ -132,6 +160,9 @@ Overview of response types for sync calls.
132
160
<tr ><td ><b >async processing</b ></td ><td >  ; </td ></tr >
133
161
<tr ><td >asVoid</td ><td >CompletableFuture< ; HttpResponse< ; Void> ;> ; </td ></tr >
134
162
<tr ><td >asString</td ><td >CompletableFuture< ; HttpResponse< ; String> ;> ; </td ></tr >
163
+ <tr ><td >as< ; E> </td ><td >CompletableFuture< ; HttpResponse< ; E> ;> ; </td ></tr >
164
+ <tr ><td >asList< ; E> </td ><td >CompletableFuture< ; HttpResponse< ; List< ; E> ;> ;> ; </td ></tr >
165
+ <tr ><td >asStream< ; E> </td ><td >CompletableFuture< ; HttpResponse< ; Stream< ; E> ;> ;> ; </td ></tr >
135
166
<tr ><td >bean< ; E> </td ><td >CompletableFuture< ; E> ; </td ></tr >
136
167
<tr ><td >list< ; E> </td ><td >CompletableFuture< ; List< ; E> ;> ; </td ></tr >
137
168
<tr ><td >stream< ; E> </td ><td >CompletableFuture< ; Stream< ; E> ;> ; </td ></tr >
@@ -173,7 +204,7 @@ When sending body content we can use:
173
204
174
205
#### GET as String
175
206
``` java
176
- HttpResponse<String > hres = clientContext . request()
207
+ HttpResponse<String > hres = client . request()
177
208
.path(" hello" )
178
209
. GET ()
179
210
.asString();
@@ -185,7 +216,7 @@ HttpResponse<String> hres = clientContext.request()
185
216
- In the example below hres is of type HttpResponse< ; String> ;
186
217
187
218
``` java
188
- clientContext . request()
219
+ client . request()
189
220
.path(" hello" )
190
221
. GET ()
191
222
.async(). asString()
@@ -205,23 +236,38 @@ clientContext.request()
205
236
206
237
#### GET as json to single bean
207
238
``` java
208
- Customer customer = clientContext. request()
239
+ HttpResponse<Customer > customer = client. request()
240
+ .path(" customers" ). path(42 )
241
+ . GET ()
242
+ .as(Customer . class);
243
+
244
+ Customer customer = client. request()
209
245
.path(" customers" ). path(42 )
210
246
. GET ()
211
247
.bean(Customer . class);
212
248
```
213
249
214
250
#### GET as json to a list of beans
215
251
``` java
216
- List<Customer > list = clientContext. request()
252
+ HttpResponse<List<Customer > > list = client. request()
253
+ .path(" customers" )
254
+ . GET ()
255
+ .asList(Customer . class);
256
+
257
+ List<Customer > list = client. request()
217
258
.path(" customers" )
218
259
. GET ()
219
260
.list(Customer . class);
220
261
```
221
262
222
263
#### GET as ` application/x-json-stream ` as a stream of beans
223
264
``` java
224
- Stream<Customer > stream = clientContext. request()
265
+ HttpResponse<Stream<Customer > > stream = client. request()
266
+ .path(" customers/all" )
267
+ . GET ()
268
+ .asStream(Customer . class);
269
+
270
+ Stream<Customer > stream = client. request()
225
271
.path(" customers/all" )
226
272
. GET ()
227
273
.stream(Customer . class);
@@ -232,7 +278,7 @@ Stream<Customer> stream = clientContext.request()
232
278
``` java
233
279
Hello bean = new Hello (42 , " rob" , " other" );
234
280
235
- HttpResponse<Void > res = clientContext . request()
281
+ HttpResponse<Void > res = client . request()
236
282
.path(" hello" )
237
283
.body(bean)
238
284
. POST ()
@@ -248,7 +294,7 @@ Multiple calls to `path()` append with a `/`. This is make it easier to build a
248
294
programmatically and also build paths that include ` matrixParam() `
249
295
250
296
``` java
251
- HttpResponse<String > res = clientContext . request()
297
+ HttpResponse<String > res = client . request()
252
298
.path(" customers" )
253
299
.path(" 42" )
254
300
.path(" contacts" )
@@ -257,15 +303,15 @@ HttpResponse<String> res = clientContext.request()
257
303
258
304
// is the same as ...
259
305
260
- HttpResponse<String > res = clientContext . request()
306
+ HttpResponse<String > res = client . request()
261
307
.path(" customers/42/contacts" )
262
308
. GET ()
263
309
.asString();
264
310
```
265
311
266
312
#### MatrixParam
267
313
``` java
268
- HttpResponse<String > httpRes = clientContext . request()
314
+ HttpResponse<String > httpRes = client . request()
269
315
.path(" books" )
270
316
.matrixParam(" author" , " rob" )
271
317
.matrixParam(" country" , " nz" )
@@ -277,7 +323,7 @@ HttpResponse<String> httpRes = clientContext.request()
277
323
278
324
#### QueryParam
279
325
``` java
280
- List<Product > beans = clientContext . request()
326
+ List<Product > beans = client . request()
281
327
.path(" products" )
282
328
.queryParam(" sortBy" , " name" )
283
329
.queryParam(" maxCount" , " 100" )
@@ -287,7 +333,7 @@ List<Product> beans = clientContext.request()
287
333
288
334
#### FormParam
289
335
``` java
290
- HttpResponse<Void > res = clientContext . request()
336
+ HttpResponse<Void > res = client . request()
291
337
.path(" register/user" )
292
338
.formParam(" name" , " Bazz" )
293
339
.formParam(
" email" ,
" [email protected] " )
@@ -355,7 +401,7 @@ The `bean()`, `list()` and `stream()` responses throw a `HttpException` if the s
355
401
356
402
``` java
357
403
358
- clientContext . request()
404
+ client . request()
359
405
.path(" hello/world" )
360
406
. GET ()
361
407
.async(). asDiscarding()
@@ -374,7 +420,7 @@ clientContext.request()
374
420
### .async().asString() - HttpResponse< ; String> ;
375
421
376
422
``` java
377
- clientContext . request()
423
+ client . request()
378
424
.path(" hello/world" )
379
425
. GET ()
380
426
.async(). asString()
@@ -393,7 +439,7 @@ clientContext.request()
393
439
### .async().bean(HelloDto.class)
394
440
395
441
``` java
396
- clientContext . request()
442
+ client . request()
397
443
...
398
444
. POST (). async()
399
445
.bean(HelloDto . class)
@@ -420,7 +466,7 @@ clientContext.request()
420
466
The example below is a line subscriber processing response content line by line.
421
467
422
468
``` java
423
- CompletableFuture<HttpResponse<Void > > future = clientContext . request()
469
+ CompletableFuture<HttpResponse<Void > > future = client . request()
424
470
.path(" hello/lineStream" )
425
471
. GET (). async()
426
472
.handler(HttpResponse . BodyHandlers . fromLineSubscriber(new Flow .Subscriber<> () {
@@ -460,7 +506,7 @@ choose `async()` to execute the request asynchronously.
460
506
461
507
``` java
462
508
HttpCall<List<Customer > > call =
463
- clientContext . request()
509
+ client . request()
464
510
.path(" customers" )
465
511
. GET ()
466
512
.call(). list(Customer . class);
@@ -484,8 +530,8 @@ header ("Basic Auth").
484
530
##### Example
485
531
486
532
``` java
487
- HttpClientContext clientContext =
488
- HttpClientContext . builder()
533
+ HttpClient client =
534
+ HttpClient . builder()
489
535
.baseUrl(baseUrl)
490
536
...
491
537
.requestIntercept(new BasicAuthIntercept (" myUsername" , " myPassword" )) < ! -- HERE
@@ -496,7 +542,7 @@ HttpClientContext clientContext =
496
542
## AuthTokenProvider - Authorization Bearer token
497
543
498
544
For Authorization using ` Bearer ` tokens that are obtained and expire, implement ` AuthTokenProvider `
499
- and register that when building the HttpClientContext .
545
+ and register that when building the HttpClient .
500
546
501
547
### 1. Implement AuthTokenProvider
502
548
@@ -520,10 +566,10 @@ and register that when building the HttpClientContext.
520
566
}
521
567
```
522
568
523
- ### 2. Register with HttpClientContext
569
+ ### 2. Register with HttpClient
524
570
525
571
``` java
526
- HttpClientContext ctx = HttpClientContext . builder()
572
+ HttpClient client = HttpClient . builder()
527
573
.baseUrl(" https://foo" )
528
574
...
529
575
.authTokenProvider(new MyAuthTokenProvider ()) < ! -- HERE
@@ -532,7 +578,7 @@ and register that when building the HttpClientContext.
532
578
533
579
### 3. Token obtained and set automatically
534
580
535
- All requests using the HttpClientContext will automatically get
581
+ All requests using the HttpClient will automatically get
536
582
an ` Authorization ` header with ` Bearer ` token added. The token will be
537
583
obtained for initial request and then renewed when the token has expired.
538
584
0 commit comments