File tree Expand file tree Collapse file tree 5 files changed +79
-1
lines changed
aotSmokeTest/java/com/example/webclient
main/java/com/example/webclient Expand file tree Collapse file tree 5 files changed +79
-1
lines changed Original file line number Diff line number Diff line change @@ -31,4 +31,12 @@ void httpsWorks(AssertableOutput output) {
31
31
});
32
32
}
33
33
34
+ @ Test
35
+ void serviceWorks (AssertableOutput output ) {
36
+ Awaitility .await ().atMost (Duration .ofSeconds (30 )).untilAsserted (() -> {
37
+ assertThat (output ).hasSingleLineContaining (
38
+ "service: ExchangeDataDto{url='https://httpbin.org/anything', method='GET'}" );
39
+ });
40
+ }
41
+
34
42
}
Original file line number Diff line number Diff line change @@ -14,14 +14,18 @@ class CLR implements CommandLineRunner {
14
14
15
15
private final Builder webClientBuilder ;
16
16
17
- CLR (WebClient .Builder webClientBuilder ) {
17
+ private final DataService dataService ;
18
+
19
+ CLR (WebClient .Builder webClientBuilder , DataService dataService ) {
18
20
this .webClientBuilder = webClientBuilder ;
21
+ this .dataService = dataService ;
19
22
}
20
23
21
24
@ Override
22
25
public void run (String ... args ) {
23
26
http ();
24
27
https ();
28
+ service ();
25
29
}
26
30
27
31
private void http () {
@@ -50,4 +54,15 @@ private void https() {
50
54
}
51
55
}
52
56
57
+ private void service () {
58
+ try {
59
+ ExchangeDataDto dto = this .dataService .getData ();
60
+ System .out .printf ("service: %s%n" , dto );
61
+ }
62
+ catch (Exception ex ) {
63
+ System .out .println ("service failed:" );
64
+ ex .printStackTrace (System .out );
65
+ }
66
+ }
67
+
53
68
}
Original file line number Diff line number Diff line change
1
+ package com .example .webclient ;
2
+
3
+ import org .springframework .web .service .annotation .GetExchange ;
4
+
5
+ interface DataService {
6
+
7
+ @ GetExchange ("/anything" )
8
+ ExchangeDataDto getData ();
9
+
10
+ }
Original file line number Diff line number Diff line change
1
+ package com .example .webclient ;
2
+
3
+ public class ExchangeDataDto {
4
+
5
+ private String url ;
6
+
7
+ private String method ;
8
+
9
+ public ExchangeDataDto () {
10
+ }
11
+
12
+ public String getUrl () {
13
+ return url ;
14
+ }
15
+
16
+ public void setUrl (String url ) {
17
+ this .url = url ;
18
+ }
19
+
20
+ public String getMethod () {
21
+ return method ;
22
+ }
23
+
24
+ public void setMethod (String method ) {
25
+ this .method = method ;
26
+ }
27
+
28
+ @ Override
29
+ public String toString () {
30
+ return "ExchangeDataDto{" + "url='" + url + '\'' + ", method='" + method + '\'' + '}' ;
31
+ }
32
+
33
+ }
Original file line number Diff line number Diff line change 3
3
import org .springframework .boot .SpringApplication ;
4
4
import org .springframework .boot .WebApplicationType ;
5
5
import org .springframework .boot .autoconfigure .SpringBootApplication ;
6
+ import org .springframework .context .annotation .Bean ;
7
+ import org .springframework .web .reactive .function .client .WebClient ;
8
+ import org .springframework .web .reactive .function .client .support .WebClientAdapter ;
9
+ import org .springframework .web .service .invoker .HttpServiceProxyFactory ;
6
10
7
11
@ SpringBootApplication
8
12
public class WebClientApplication {
@@ -14,4 +18,12 @@ public static void main(String[] args) throws InterruptedException {
14
18
Thread .currentThread ().join (); // To be able to measure memory consumption
15
19
}
16
20
21
+ @ Bean
22
+ DataService dataService (WebClient .Builder webClientBuilder ) {
23
+ WebClient webClient = webClientBuilder .baseUrl ("https://httpbin.org/" ).build ();
24
+ HttpServiceProxyFactory factory = HttpServiceProxyFactory .builder (WebClientAdapter .forClient (webClient ))
25
+ .build ();
26
+ return factory .createClient (DataService .class );
27
+ }
28
+
17
29
}
You can’t perform that action at this time.
0 commit comments