Skip to content

Commit acda440

Browse files
author
ardizioa
committed
Reactive web client and exception handling for rest requests
1 parent 9d6186a commit acda440

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<java.version>1.8</java.version>
2525
<spring-boot-admin.version>2.0.0</spring-boot-admin.version>
2626
<dse-driver.version>1.1.2</dse-driver.version>
27+
<reactor.kafka.version>1.0.0.RELEASE</reactor.kafka.version>
2728
</properties>
2829

2930
<dependencies>
@@ -45,6 +46,11 @@
4546
<version>${dse-driver.version}</version>
4647
</dependency>
4748

49+
<dependency>
50+
<groupId>io.projectreactor.kafka</groupId>
51+
<artifactId>reactor-kafka</artifactId>
52+
<version>${reactor.kafka.version}</version>
53+
</dependency>
4854

4955
<dependency>
5056
<groupId>org.springframework.boot</groupId>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.aardizio.client;
2+
3+
4+
import com.aardizio.errors.*;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
import org.springframework.web.reactive.function.client.ClientResponse;
8+
9+
import reactor.core.publisher.Mono;
10+
/**
11+
* Generic error handler for rest requests.
12+
* @author Alessandro Pio Ardizio
13+
*/
14+
public class RestClientErrorHandler {
15+
16+
private static final Logger log = LoggerFactory.getLogger(RestClientExample.class);
17+
18+
public static Mono<Throwable> handle(ClientResponse cr){
19+
log.error(cr.toString());
20+
// TODO do exception mapping here
21+
return Mono.just(new RestClientException(cr.statusCode().toString() , "Message" ,cr.body(null)));
22+
}
23+
24+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.aardizio.client;
2+
3+
import com.aardizio.model.Hotels;
4+
import org.springframework.http.HttpStatus;
5+
import org.springframework.http.MediaType;
6+
import org.springframework.web.reactive.function.client.WebClient;
7+
8+
import reactor.core.publisher.Flux;
9+
10+
/**
11+
* Example web client to rest calls.
12+
* @author Alessandro Pio Ardizio
13+
*
14+
*/
15+
public class RestClientExample {
16+
17+
public Flux<Hotels> send() {
18+
WebClient client = WebClient.create("http://microservice");
19+
Flux<Hotels> hotels = client.get()
20+
.uri("/hotels")
21+
.accept(MediaType.APPLICATION_JSON)
22+
.retrieve()
23+
.onStatus(HttpStatus::is4xxClientError, RestClientErrorHandler::handle)
24+
.onStatus(HttpStatus::is5xxServerError, RestClientErrorHandler::handle)
25+
.bodyToFlux(Hotels.class);
26+
return hotels;
27+
}
28+
29+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.aardizio.errors;
2+
3+
import java.io.Serializable;
4+
5+
public class RestClientException extends Exception implements Serializable{
6+
7+
private static final long serialVersionUID = 1L ;
8+
9+
private String code;
10+
private String infos;
11+
12+
public RestClientException(String code ,String message , String infos){
13+
super(message);
14+
this.code = code;
15+
this.infos = infos;
16+
}
17+
18+
public String getCode(){
19+
return this.code;
20+
}
21+
22+
public String getInfos(){
23+
return this.infos;
24+
}
25+
}

0 commit comments

Comments
 (0)