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,14 +26,14 @@ 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()
35
+ public HttpClient client() {
36
+ return HttpClient . builder()
39
37
.baseUrl(baseUrl)
40
38
.bodyAdapter(new JsonbBodyAdapter ())
41
39
// .bodyAdapter(new JacksonBodyAdapter(new ObjectMapper()))
@@ -47,7 +45,7 @@ Create a HttpClientContext with a baseUrl, Jackson or Gson based JSON
47
45
48
46
## Requests
49
47
50
- From HttpClientContext :
48
+ From HttpClient :
51
49
- Create a request
52
50
- Build the url via path(), matrixParam(), queryParam()
53
51
- Optionally set headers(), cookies() etc
@@ -78,15 +76,15 @@ From HttpClientContext:
78
76
79
77
#### Example GET as String
80
78
``` java
81
- HttpResponse<String > hres = clientContext . request()
79
+ HttpResponse<String > hres = client . request()
82
80
.path(" hello" )
83
81
. GET ()
84
82
.asString();
85
83
```
86
84
87
85
#### Example GET as JSON marshalling into a java class/dto
88
86
``` java
89
- CustomerDto customer = clientContext . request()
87
+ CustomerDto customer = client . request()
90
88
.path(" customers" ). path(42 )
91
89
. GET ()
92
90
.bean(CustomerDto . class);
@@ -98,7 +96,7 @@ CustomerDto customer = clientContext.request()
98
96
- In the example below hres is of type HttpResponse< ; String> ;
99
97
100
98
``` java
101
- clientContext . request()
99
+ client . request()
102
100
.path(" hello" )
103
101
. GET ()
104
102
.async(). asString() // CompletableFuture<HttpResponse<String>>
@@ -173,7 +171,7 @@ When sending body content we can use:
173
171
174
172
#### GET as String
175
173
``` java
176
- HttpResponse<String > hres = clientContext . request()
174
+ HttpResponse<String > hres = client . request()
177
175
.path(" hello" )
178
176
. GET ()
179
177
.asString();
@@ -185,7 +183,7 @@ HttpResponse<String> hres = clientContext.request()
185
183
- In the example below hres is of type HttpResponse< ; String> ;
186
184
187
185
``` java
188
- clientContext . request()
186
+ client . request()
189
187
.path(" hello" )
190
188
. GET ()
191
189
.async(). asString()
@@ -205,23 +203,23 @@ clientContext.request()
205
203
206
204
#### GET as json to single bean
207
205
``` java
208
- Customer customer = clientContext . request()
206
+ Customer customer = client . request()
209
207
.path(" customers" ). path(42 )
210
208
. GET ()
211
209
.bean(Customer . class);
212
210
```
213
211
214
212
#### GET as json to a list of beans
215
213
``` java
216
- List<Customer > list = clientContext . request()
214
+ List<Customer > list = client . request()
217
215
.path(" customers" )
218
216
. GET ()
219
217
.list(Customer . class);
220
218
```
221
219
222
220
#### GET as ` application/x-json-stream ` as a stream of beans
223
221
``` java
224
- Stream<Customer > stream = clientContext . request()
222
+ Stream<Customer > stream = client . request()
225
223
.path(" customers/all" )
226
224
. GET ()
227
225
.stream(Customer . class);
@@ -232,7 +230,7 @@ Stream<Customer> stream = clientContext.request()
232
230
``` java
233
231
Hello bean = new Hello (42 , " rob" , " other" );
234
232
235
- HttpResponse<Void > res = clientContext . request()
233
+ HttpResponse<Void > res = client . request()
236
234
.path(" hello" )
237
235
.body(bean)
238
236
. POST ()
@@ -248,7 +246,7 @@ Multiple calls to `path()` append with a `/`. This is make it easier to build a
248
246
programmatically and also build paths that include ` matrixParam() `
249
247
250
248
``` java
251
- HttpResponse<String > res = clientContext . request()
249
+ HttpResponse<String > res = client . request()
252
250
.path(" customers" )
253
251
.path(" 42" )
254
252
.path(" contacts" )
@@ -257,15 +255,15 @@ HttpResponse<String> res = clientContext.request()
257
255
258
256
// is the same as ...
259
257
260
- HttpResponse<String > res = clientContext . request()
258
+ HttpResponse<String > res = client . request()
261
259
.path(" customers/42/contacts" )
262
260
. GET ()
263
261
.asString();
264
262
```
265
263
266
264
#### MatrixParam
267
265
``` java
268
- HttpResponse<String > httpRes = clientContext . request()
266
+ HttpResponse<String > httpRes = client . request()
269
267
.path(" books" )
270
268
.matrixParam(" author" , " rob" )
271
269
.matrixParam(" country" , " nz" )
@@ -277,7 +275,7 @@ HttpResponse<String> httpRes = clientContext.request()
277
275
278
276
#### QueryParam
279
277
``` java
280
- List<Product > beans = clientContext . request()
278
+ List<Product > beans = client . request()
281
279
.path(" products" )
282
280
.queryParam(" sortBy" , " name" )
283
281
.queryParam(" maxCount" , " 100" )
@@ -287,7 +285,7 @@ List<Product> beans = clientContext.request()
287
285
288
286
#### FormParam
289
287
``` java
290
- HttpResponse<Void > res = clientContext . request()
288
+ HttpResponse<Void > res = client . request()
291
289
.path(" register/user" )
292
290
.formParam(" name" , " Bazz" )
293
291
.formParam(
" email" ,
" [email protected] " )
@@ -355,7 +353,7 @@ The `bean()`, `list()` and `stream()` responses throw a `HttpException` if the s
355
353
356
354
``` java
357
355
358
- clientContext . request()
356
+ client . request()
359
357
.path(" hello/world" )
360
358
. GET ()
361
359
.async(). asDiscarding()
@@ -374,7 +372,7 @@ clientContext.request()
374
372
### .async().asString() - HttpResponse< ; String> ;
375
373
376
374
``` java
377
- clientContext . request()
375
+ client . request()
378
376
.path(" hello/world" )
379
377
. GET ()
380
378
.async(). asString()
@@ -393,7 +391,7 @@ clientContext.request()
393
391
### .async().bean(HelloDto.class)
394
392
395
393
``` java
396
- clientContext . request()
394
+ client . request()
397
395
...
398
396
. POST (). async()
399
397
.bean(HelloDto . class)
@@ -420,7 +418,7 @@ clientContext.request()
420
418
The example below is a line subscriber processing response content line by line.
421
419
422
420
``` java
423
- CompletableFuture<HttpResponse<Void > > future = clientContext . request()
421
+ CompletableFuture<HttpResponse<Void > > future = client . request()
424
422
.path(" hello/lineStream" )
425
423
. GET (). async()
426
424
.handler(HttpResponse . BodyHandlers . fromLineSubscriber(new Flow .Subscriber<> () {
@@ -460,7 +458,7 @@ choose `async()` to execute the request asynchronously.
460
458
461
459
``` java
462
460
HttpCall<List<Customer > > call =
463
- clientContext . request()
461
+ client . request()
464
462
.path(" customers" )
465
463
. GET ()
466
464
.call(). list(Customer . class);
@@ -484,8 +482,8 @@ header ("Basic Auth").
484
482
##### Example
485
483
486
484
``` java
487
- HttpClientContext clientContext =
488
- HttpClientContext . builder()
485
+ HttpClient client =
486
+ HttpClient . builder()
489
487
.baseUrl(baseUrl)
490
488
...
491
489
.requestIntercept(new BasicAuthIntercept (" myUsername" , " myPassword" )) < ! -- HERE
@@ -496,7 +494,7 @@ HttpClientContext clientContext =
496
494
## AuthTokenProvider - Authorization Bearer token
497
495
498
496
For Authorization using ` Bearer ` tokens that are obtained and expire, implement ` AuthTokenProvider `
499
- and register that when building the HttpClientContext .
497
+ and register that when building the HttpClient .
500
498
501
499
### 1. Implement AuthTokenProvider
502
500
@@ -520,10 +518,10 @@ and register that when building the HttpClientContext.
520
518
}
521
519
```
522
520
523
- ### 2. Register with HttpClientContext
521
+ ### 2. Register with HttpClient
524
522
525
523
``` java
526
- HttpClientContext ctx = HttpClientContext . builder()
524
+ HttpClient client = HttpClient . builder()
527
525
.baseUrl(" https://foo" )
528
526
...
529
527
.authTokenProvider(new MyAuthTokenProvider ()) < ! -- HERE
@@ -532,7 +530,7 @@ and register that when building the HttpClientContext.
532
530
533
531
### 3. Token obtained and set automatically
534
532
535
- All requests using the HttpClientContext will automatically get
533
+ All requests using the HttpClient will automatically get
536
534
an ` Authorization ` header with ` Bearer ` token added. The token will be
537
535
obtained for initial request and then renewed when the token has expired.
538
536
0 commit comments