Skip to content

Commit db00197

Browse files
authored
Merge pull request eugenp#4670 from Tonnix/master
Added BAEL-1867 project files under the Spring-5-Reactive Project
2 parents 692eafa + 1e69e96 commit db00197

15 files changed

Lines changed: 735 additions & 0 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
package com.baeldung.reactive.errorhandling;
3+
4+
import java.util.Map;
5+
import org.springframework.boot.web.reactive.error.DefaultErrorAttributes;
6+
import org.springframework.http.HttpStatus;
7+
import org.springframework.stereotype.Component;
8+
import org.springframework.web.reactive.function.server.ServerRequest;
9+
10+
@Component
11+
public class GlobalErrorAttributes extends DefaultErrorAttributes{
12+
13+
private HttpStatus status = HttpStatus.BAD_REQUEST;
14+
private String message = "please provide a name";
15+
16+
public GlobalErrorAttributes() {
17+
super(false);
18+
}
19+
20+
@Override
21+
public Map<String, Object> getErrorAttributes(ServerRequest request, boolean includeStackTrace) {
22+
Map<String, Object> map = super.getErrorAttributes(request, includeStackTrace);
23+
map.put("status", getStatus());
24+
map.put("message", getMessage());
25+
return map;
26+
}
27+
28+
/**
29+
* @return the status
30+
*/
31+
public HttpStatus getStatus() {
32+
return status;
33+
}
34+
35+
/**
36+
* @param status the status to set
37+
*/
38+
public void setStatus(HttpStatus status) {
39+
this.status = status;
40+
}
41+
42+
/**
43+
* @return the message
44+
*/
45+
public String getMessage() {
46+
return message;
47+
}
48+
49+
/**
50+
* @param message the message to set
51+
*/
52+
public void setMessage(String message) {
53+
this.message = message;
54+
}
55+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
package com.baeldung.reactive.errorhandling;
3+
4+
import java.util.Map;
5+
import org.springframework.boot.autoconfigure.web.ResourceProperties;
6+
import org.springframework.boot.autoconfigure.web.reactive.error.AbstractErrorWebExceptionHandler;
7+
import org.springframework.boot.web.reactive.error.ErrorAttributes;
8+
import org.springframework.context.ApplicationContext;
9+
import org.springframework.core.annotation.Order;
10+
import org.springframework.http.HttpStatus;
11+
import org.springframework.http.MediaType;
12+
import org.springframework.http.codec.ServerCodecConfigurer;
13+
import org.springframework.stereotype.Component;
14+
import org.springframework.web.reactive.function.BodyInserters;
15+
import org.springframework.web.reactive.function.server.RequestPredicates;
16+
import org.springframework.web.reactive.function.server.RouterFunction;
17+
import org.springframework.web.reactive.function.server.RouterFunctions;
18+
import org.springframework.web.reactive.function.server.ServerRequest;
19+
import org.springframework.web.reactive.function.server.ServerResponse;
20+
import reactor.core.publisher.Mono;
21+
22+
@Component
23+
@Order(-2)
24+
public class GlobalErrorWebExceptionHandler extends AbstractErrorWebExceptionHandler {
25+
26+
public GlobalErrorWebExceptionHandler(GlobalErrorAttributes g, ApplicationContext applicationContext,
27+
ServerCodecConfigurer serverCodecConfigurer) {
28+
super(g, new ResourceProperties(), applicationContext);
29+
super.setMessageWriters(serverCodecConfigurer.getWriters());
30+
super.setMessageReaders(serverCodecConfigurer.getReaders());
31+
}
32+
33+
@Override
34+
protected RouterFunction<ServerResponse> getRoutingFunction(final ErrorAttributes errorAttributes) {
35+
return RouterFunctions.route(RequestPredicates.all(), this::renderErrorResponse);
36+
}
37+
38+
private Mono<ServerResponse> renderErrorResponse(final ServerRequest request) {
39+
40+
final Map<String, Object> errorPropertiesMap = getErrorAttributes(request, false);
41+
42+
return ServerResponse.status(HttpStatus.BAD_REQUEST)
43+
.contentType(MediaType.APPLICATION_JSON_UTF8)
44+
.body(BodyInserters.fromObject(errorPropertiesMap));
45+
}
46+
47+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
package com.baeldung.reactive.errorhandling;
3+
4+
import org.springframework.http.HttpStatus;
5+
import org.springframework.web.server.ResponseStatusException;
6+
7+
public class NameRequiredException extends ResponseStatusException {
8+
9+
public NameRequiredException(HttpStatus status, String message, Throwable e) {
10+
super(status, message, e);
11+
}
12+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
package com.baeldung.reactive.errorhandling.handlers;
3+
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 Handler1 {
12+
13+
public Mono<ServerResponse> handleRequest1(ServerRequest request) {
14+
return sayHello(request).onErrorReturn("Hello, Stranger")
15+
.flatMap(s -> ServerResponse.ok()
16+
.contentType(MediaType.TEXT_PLAIN)
17+
.syncBody(s));
18+
}
19+
20+
private Mono<String> sayHello(ServerRequest request) {
21+
try {
22+
return Mono.just("Hello, " + request.queryParam("name")
23+
.get());
24+
} catch (Exception e) {
25+
return Mono.error(e);
26+
}
27+
}
28+
29+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
package com.baeldung.reactive.errorhandling.handlers;
3+
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 Handler2 {
12+
13+
public Mono<ServerResponse> handleRequest2(ServerRequest request) {
14+
return
15+
sayHello(request)
16+
.flatMap(s -> ServerResponse.ok()
17+
.contentType(MediaType.TEXT_PLAIN)
18+
.syncBody(s))
19+
.onErrorResume(e -> sayHelloFallback()
20+
.flatMap(s -> ServerResponse.ok()
21+
.contentType(MediaType.TEXT_PLAIN)
22+
.syncBody(s)));
23+
}
24+
25+
private Mono<String> sayHello(ServerRequest request) {
26+
try {
27+
return Mono.just("Hello, " + request.queryParam("name")
28+
.get());
29+
} catch (Exception e) {
30+
return Mono.error(e);
31+
}
32+
}
33+
34+
private Mono<String> sayHelloFallback() {
35+
return Mono.just("Hello, Stranger");
36+
}
37+
38+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
package com.baeldung.reactive.errorhandling.handlers;
3+
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 Handler3 {
12+
13+
public Mono<ServerResponse> handleRequest3(ServerRequest request) {
14+
return
15+
sayHello(request)
16+
.flatMap(s -> ServerResponse.ok()
17+
.contentType(MediaType.TEXT_PLAIN)
18+
.syncBody(s))
19+
.onErrorResume(e -> (Mono.just("Hi, I looked around for your name but found: " +
20+
e.getMessage())).flatMap(s -> ServerResponse.ok()
21+
.contentType(MediaType.TEXT_PLAIN)
22+
.syncBody(s)));
23+
}
24+
25+
private Mono<String> sayHello(ServerRequest request) {
26+
try {
27+
return Mono.just("Hello, " + request.queryParam("name")
28+
.get());
29+
} catch (Exception e) {
30+
return Mono.error(e);
31+
}
32+
}
33+
34+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
package com.baeldung.reactive.errorhandling.handlers;
3+
4+
import com.baeldung.reactive.errorhandling.NameRequiredException;
5+
import org.springframework.http.HttpStatus;
6+
import org.springframework.stereotype.Component;
7+
import org.springframework.web.reactive.function.server.ServerRequest;
8+
import org.springframework.web.reactive.function.server.ServerResponse;
9+
import reactor.core.publisher.Mono;
10+
11+
@Component
12+
public class Handler4 {
13+
14+
public Mono<ServerResponse> handleRequest4(ServerRequest request) {
15+
return ServerResponse.ok()
16+
.body(sayHello(request)
17+
.onErrorResume(e ->
18+
Mono.error(new NameRequiredException(
19+
HttpStatus.BAD_REQUEST, "please provide a name", e))), String.class);
20+
}
21+
22+
private Mono<String> sayHello(ServerRequest request) {
23+
try {
24+
return Mono.just("Hello, " + request.queryParam("name").get());
25+
} catch (Exception e) {
26+
return Mono.error(e);
27+
}
28+
}
29+
30+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
package com.baeldung.reactive.errorhandling.handlers;
3+
4+
import org.springframework.stereotype.Component;
5+
import org.springframework.web.reactive.function.server.ServerRequest;
6+
import org.springframework.web.reactive.function.server.ServerResponse;
7+
import reactor.core.publisher.Mono;
8+
9+
@Component
10+
public class Handler5 {
11+
12+
public Mono<ServerResponse> handleRequest5(ServerRequest request) {
13+
return ServerResponse.ok()
14+
.body(sayHello(request), String.class);
15+
16+
}
17+
18+
private Mono<String> sayHello(ServerRequest request) {
19+
return Mono.just("Hello, " + request.queryParam("name").get());
20+
}
21+
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
package com.baeldung.reactive.errorhandling.routers;
3+
4+
import com.baeldung.reactive.errorhandling.handlers.Handler1;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.http.MediaType;
7+
import org.springframework.stereotype.Component;
8+
import org.springframework.web.reactive.function.server.RequestPredicates;
9+
import org.springframework.web.reactive.function.server.RouterFunction;
10+
import org.springframework.web.reactive.function.server.RouterFunctions;
11+
import org.springframework.web.reactive.function.server.ServerResponse;
12+
13+
@Component
14+
public class Router1 {
15+
16+
@Bean
17+
public RouterFunction<ServerResponse> routeRequest1(Handler1 handler) {
18+
return RouterFunctions.route(RequestPredicates.GET("/api/endpoint1")
19+
.and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), handler::handleRequest1);
20+
}
21+
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
package com.baeldung.reactive.errorhandling.routers;
3+
4+
import com.baeldung.reactive.errorhandling.handlers.Handler2;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.http.MediaType;
7+
import org.springframework.stereotype.Component;
8+
import org.springframework.web.reactive.function.server.RequestPredicates;
9+
import org.springframework.web.reactive.function.server.RouterFunction;
10+
import org.springframework.web.reactive.function.server.RouterFunctions;
11+
import org.springframework.web.reactive.function.server.ServerResponse;
12+
13+
@Component
14+
public class Router2 {
15+
16+
@Bean
17+
public RouterFunction<ServerResponse> routeRequest2(Handler2 handler) {
18+
return RouterFunctions.route(RequestPredicates.GET("/api/endpoint2")
19+
.and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), handler::handleRequest2);
20+
}
21+
22+
}

0 commit comments

Comments
 (0)