Skip to content

Commit bacfc3e

Browse files
authored
Merge pull request #12261 from hkhan/JAVA-8153-webclient-error-handling
[JAVA-8153] Code clean up for reactive error handling
2 parents c98f02a + 4188ab6 commit bacfc3e

15 files changed

Lines changed: 168 additions & 414 deletions

File tree

spring-reactive/src/main/java/com/baeldung/reactive/errorhandling/ErrorHandlingApplication.java

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,16 @@
33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
55
import org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration;
6-
import org.springframework.context.annotation.Bean;
7-
import org.springframework.security.config.web.server.ServerHttpSecurity;
8-
import org.springframework.security.web.server.SecurityWebFilterChain;
6+
import org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration;
7+
import org.springframework.boot.autoconfigure.security.reactive.ReactiveUserDetailsServiceAutoConfiguration;
98

10-
@SpringBootApplication(exclude = MongoReactiveAutoConfiguration.class)
9+
@SpringBootApplication(exclude = {
10+
MongoReactiveAutoConfiguration.class,
11+
ReactiveSecurityAutoConfiguration.class,
12+
ReactiveUserDetailsServiceAutoConfiguration.class })
1113
public class ErrorHandlingApplication {
1214

1315
public static void main(String[] args) {
1416
SpringApplication.run(ErrorHandlingApplication.class, args);
1517
}
16-
17-
@Bean
18-
public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
19-
http.authorizeExchange()
20-
.anyExchange()
21-
.permitAll();
22-
http.csrf().disable();
23-
return http.build();
24-
}
2518
}

spring-reactive/src/main/java/com/baeldung/reactive/errorhandling/GlobalErrorAttributes.java

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,44 +9,14 @@
99
import java.util.Map;
1010

1111
@Component
12-
public class GlobalErrorAttributes extends DefaultErrorAttributes{
13-
14-
private HttpStatus status = HttpStatus.BAD_REQUEST;
15-
private String message = "please provide a name";
12+
public class GlobalErrorAttributes extends DefaultErrorAttributes {
1613

1714
@Override
1815
public Map<String, Object> getErrorAttributes(ServerRequest request, ErrorAttributeOptions options) {
1916
Map<String, Object> map = super.getErrorAttributes(request, options);
20-
map.put("status", getStatus());
21-
map.put("message", getMessage());
17+
map.put("status", HttpStatus.BAD_REQUEST);
18+
map.put("message", "please provide a name");
2219
return map;
2320
}
2421

25-
/**
26-
* @return the status
27-
*/
28-
public HttpStatus getStatus() {
29-
return status;
30-
}
31-
32-
/**
33-
* @param status the status to set
34-
*/
35-
public void setStatus(HttpStatus status) {
36-
this.status = status;
37-
}
38-
39-
/**
40-
* @return the message
41-
*/
42-
public String getMessage() {
43-
return message;
44-
}
45-
46-
/**
47-
* @param message the message to set
48-
*/
49-
public void setMessage(String message) {
50-
this.message = message;
51-
}
5222
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.baeldung.reactive.errorhandling;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.http.MediaType;
5+
import org.springframework.stereotype.Component;
6+
import org.springframework.web.reactive.function.server.ServerRequest;
7+
import org.springframework.web.reactive.function.server.ServerResponse;
8+
import reactor.core.publisher.Mono;
9+
10+
@Component
11+
public class Handler {
12+
13+
public Mono<ServerResponse> handleWithErrorReturn(ServerRequest request) {
14+
return sayHello(request)
15+
.onErrorReturn("Hello, Stranger")
16+
.flatMap(s -> ServerResponse.ok()
17+
.contentType(MediaType.TEXT_PLAIN)
18+
.bodyValue(s));
19+
}
20+
21+
public Mono<ServerResponse> handleWithErrorResumeAndDynamicFallback(ServerRequest request) {
22+
return sayHello(request)
23+
.flatMap(s -> ServerResponse.ok()
24+
.contentType(MediaType.TEXT_PLAIN)
25+
.bodyValue(s))
26+
.onErrorResume(e -> (Mono.just("Hi, I looked around for your name but found: " + e.getMessage()))
27+
.flatMap(s -> ServerResponse.ok()
28+
.contentType(MediaType.TEXT_PLAIN)
29+
.bodyValue(s)));
30+
}
31+
32+
public Mono<ServerResponse> handleWithErrorResumeAndFallbackMethod(ServerRequest request) {
33+
return sayHello(request)
34+
.flatMap(s -> ServerResponse.ok()
35+
.contentType(MediaType.TEXT_PLAIN)
36+
.bodyValue(s))
37+
.onErrorResume(e -> sayHelloFallback()
38+
.flatMap(s -> ServerResponse.ok()
39+
.contentType(MediaType.TEXT_PLAIN)
40+
.bodyValue(s)));
41+
}
42+
43+
public Mono<ServerResponse> handleWithErrorResumeAndCustomException(ServerRequest request) {
44+
return ServerResponse.ok()
45+
.body(sayHello(request)
46+
.onErrorResume(e -> Mono.error(new NameRequiredException(
47+
HttpStatus.BAD_REQUEST,
48+
"please provide a name", e))), String.class);
49+
}
50+
51+
public Mono<ServerResponse> handleWithGlobalErrorHandler(ServerRequest request) {
52+
return ServerResponse.ok()
53+
.body(sayHello(request), String.class);
54+
}
55+
56+
private Mono<String> sayHello(ServerRequest request) {
57+
try {
58+
return Mono.just("Hello, " + request.queryParam("name").get());
59+
} catch (Exception e) {
60+
return Mono.error(e);
61+
}
62+
}
63+
64+
private Mono<String> sayHelloFallback() {
65+
return Mono.just("Hello, Stranger");
66+
}
67+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.reactive.errorhandling;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.stereotype.Component;
5+
import org.springframework.web.reactive.function.server.RouterFunction;
6+
import org.springframework.web.reactive.function.server.RouterFunctions;
7+
import org.springframework.web.reactive.function.server.ServerResponse;
8+
9+
import static org.springframework.http.MediaType.TEXT_PLAIN;
10+
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
11+
import static org.springframework.web.reactive.function.server.RequestPredicates.accept;
12+
13+
@Component
14+
public class Router {
15+
16+
@Bean
17+
public RouterFunction<ServerResponse> routes(Handler handler) {
18+
return RouterFunctions
19+
.route(GET("/api/endpoint1").and(accept(TEXT_PLAIN)), handler::handleWithErrorReturn)
20+
.andRoute(GET("/api/endpoint2").and(accept(TEXT_PLAIN)), handler::handleWithErrorResumeAndFallbackMethod)
21+
.andRoute(GET("/api/endpoint3").and(accept(TEXT_PLAIN)), handler::handleWithErrorResumeAndDynamicFallback)
22+
.andRoute(GET("/api/endpoint4").and(accept(TEXT_PLAIN)), handler::handleWithErrorResumeAndCustomException)
23+
.andRoute(GET("/api/endpoint5").and(accept(TEXT_PLAIN)), handler::handleWithGlobalErrorHandler);
24+
}
25+
26+
}

spring-reactive/src/main/java/com/baeldung/reactive/errorhandling/handlers/Handler1.java

Lines changed: 0 additions & 28 deletions
This file was deleted.

spring-reactive/src/main/java/com/baeldung/reactive/errorhandling/handlers/Handler2.java

Lines changed: 0 additions & 37 deletions
This file was deleted.

spring-reactive/src/main/java/com/baeldung/reactive/errorhandling/handlers/Handler3.java

Lines changed: 0 additions & 33 deletions
This file was deleted.

spring-reactive/src/main/java/com/baeldung/reactive/errorhandling/handlers/Handler4.java

Lines changed: 0 additions & 29 deletions
This file was deleted.

spring-reactive/src/main/java/com/baeldung/reactive/errorhandling/handlers/Handler5.java

Lines changed: 0 additions & 21 deletions
This file was deleted.

spring-reactive/src/main/java/com/baeldung/reactive/errorhandling/routers/Router1.java

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)