Merge pull request #12261 from hkhan/JAVA-8153-webclient-error-handling

[JAVA-8153] Code clean up for reactive error handling
This commit is contained in:
kwoyke 2022-05-26 10:52:32 +02:00 committed by GitHub
commit bacfc3e283
15 changed files with 195 additions and 441 deletions

View File

@ -3,23 +3,16 @@ package com.baeldung.reactive.errorhandling;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.web.server.SecurityWebFilterChain;
import org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration;
import org.springframework.boot.autoconfigure.security.reactive.ReactiveUserDetailsServiceAutoConfiguration;
@SpringBootApplication(exclude = MongoReactiveAutoConfiguration.class)
@SpringBootApplication(exclude = {
MongoReactiveAutoConfiguration.class,
ReactiveSecurityAutoConfiguration.class,
ReactiveUserDetailsServiceAutoConfiguration.class })
public class ErrorHandlingApplication {
public static void main(String[] args) {
SpringApplication.run(ErrorHandlingApplication.class, args);
}
@Bean
public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
http.authorizeExchange()
.anyExchange()
.permitAll();
http.csrf().disable();
return http.build();
}
}

View File

@ -9,44 +9,14 @@ import org.springframework.web.reactive.function.server.ServerRequest;
import java.util.Map;
@Component
public class GlobalErrorAttributes extends DefaultErrorAttributes{
private HttpStatus status = HttpStatus.BAD_REQUEST;
private String message = "please provide a name";
public class GlobalErrorAttributes extends DefaultErrorAttributes {
@Override
public Map<String, Object> getErrorAttributes(ServerRequest request, ErrorAttributeOptions options) {
Map<String, Object> map = super.getErrorAttributes(request, options);
map.put("status", getStatus());
map.put("message", getMessage());
map.put("status", HttpStatus.BAD_REQUEST);
map.put("message", "please provide a name");
return map;
}
/**
* @return the status
*/
public HttpStatus getStatus() {
return status;
}
/**
* @param status the status to set
*/
public void setStatus(HttpStatus status) {
this.status = status;
}
/**
* @return the message
*/
public String getMessage() {
return message;
}
/**
* @param message the message to set
*/
public void setMessage(String message) {
this.message = message;
}
}

View File

@ -0,0 +1,67 @@
package com.baeldung.reactive.errorhandling;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
@Component
public class Handler {
public Mono<ServerResponse> handleWithErrorReturn(ServerRequest request) {
return sayHello(request)
.onErrorReturn("Hello, Stranger")
.flatMap(s -> ServerResponse.ok()
.contentType(MediaType.TEXT_PLAIN)
.bodyValue(s));
}
public Mono<ServerResponse> handleWithErrorResumeAndDynamicFallback(ServerRequest request) {
return sayHello(request)
.flatMap(s -> ServerResponse.ok()
.contentType(MediaType.TEXT_PLAIN)
.bodyValue(s))
.onErrorResume(e -> (Mono.just("Hi, I looked around for your name but found: " + e.getMessage()))
.flatMap(s -> ServerResponse.ok()
.contentType(MediaType.TEXT_PLAIN)
.bodyValue(s)));
}
public Mono<ServerResponse> handleWithErrorResumeAndFallbackMethod(ServerRequest request) {
return sayHello(request)
.flatMap(s -> ServerResponse.ok()
.contentType(MediaType.TEXT_PLAIN)
.bodyValue(s))
.onErrorResume(e -> sayHelloFallback()
.flatMap(s -> ServerResponse.ok()
.contentType(MediaType.TEXT_PLAIN)
.bodyValue(s)));
}
public Mono<ServerResponse> handleWithErrorResumeAndCustomException(ServerRequest request) {
return ServerResponse.ok()
.body(sayHello(request)
.onErrorResume(e -> Mono.error(new NameRequiredException(
HttpStatus.BAD_REQUEST,
"please provide a name", e))), String.class);
}
public Mono<ServerResponse> handleWithGlobalErrorHandler(ServerRequest request) {
return ServerResponse.ok()
.body(sayHello(request), String.class);
}
private Mono<String> sayHello(ServerRequest request) {
try {
return Mono.just("Hello, " + request.queryParam("name").get());
} catch (Exception e) {
return Mono.error(e);
}
}
private Mono<String> sayHelloFallback() {
return Mono.just("Hello, Stranger");
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.reactive.errorhandling;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
import static org.springframework.http.MediaType.TEXT_PLAIN;
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RequestPredicates.accept;
@Component
public class Router {
@Bean
public RouterFunction<ServerResponse> routes(Handler handler) {
return RouterFunctions
.route(GET("/api/endpoint1").and(accept(TEXT_PLAIN)), handler::handleWithErrorReturn)
.andRoute(GET("/api/endpoint2").and(accept(TEXT_PLAIN)), handler::handleWithErrorResumeAndFallbackMethod)
.andRoute(GET("/api/endpoint3").and(accept(TEXT_PLAIN)), handler::handleWithErrorResumeAndDynamicFallback)
.andRoute(GET("/api/endpoint4").and(accept(TEXT_PLAIN)), handler::handleWithErrorResumeAndCustomException)
.andRoute(GET("/api/endpoint5").and(accept(TEXT_PLAIN)), handler::handleWithGlobalErrorHandler);
}
}

View File

@ -1,28 +0,0 @@
package com.baeldung.reactive.errorhandling.handlers;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
@Component
public class Handler1 {
public Mono<ServerResponse> handleRequest1(ServerRequest request) {
return sayHello(request).onErrorReturn("Hello, Stranger")
.flatMap(s -> ServerResponse.ok()
.contentType(MediaType.TEXT_PLAIN)
.bodyValue(s));
}
private Mono<String> sayHello(ServerRequest request) {
try {
return Mono.just("Hello, " + request.queryParam("name")
.get());
} catch (Exception e) {
return Mono.error(e);
}
}
}

View File

@ -1,37 +0,0 @@
package com.baeldung.reactive.errorhandling.handlers;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
@Component
public class Handler2 {
public Mono<ServerResponse> handleRequest2(ServerRequest request) {
return
sayHello(request)
.flatMap(s -> ServerResponse.ok()
.contentType(MediaType.TEXT_PLAIN)
.bodyValue(s))
.onErrorResume(e -> sayHelloFallback()
.flatMap(s -> ServerResponse.ok()
.contentType(MediaType.TEXT_PLAIN)
.bodyValue(s)));
}
private Mono<String> sayHello(ServerRequest request) {
try {
return Mono.just("Hello, " + request.queryParam("name")
.get());
} catch (Exception e) {
return Mono.error(e);
}
}
private Mono<String> sayHelloFallback() {
return Mono.just("Hello, Stranger");
}
}

View File

@ -1,33 +0,0 @@
package com.baeldung.reactive.errorhandling.handlers;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
@Component
public class Handler3 {
public Mono<ServerResponse> handleRequest3(ServerRequest request) {
return
sayHello(request)
.flatMap(s -> ServerResponse.ok()
.contentType(MediaType.TEXT_PLAIN)
.bodyValue(s))
.onErrorResume(e -> (Mono.just("Hi, I looked around for your name but found: " +
e.getMessage())).flatMap(s -> ServerResponse.ok()
.contentType(MediaType.TEXT_PLAIN)
.bodyValue(s)));
}
private Mono<String> sayHello(ServerRequest request) {
try {
return Mono.just("Hello, " + request.queryParam("name")
.get());
} catch (Exception e) {
return Mono.error(e);
}
}
}

View File

@ -1,29 +0,0 @@
package com.baeldung.reactive.errorhandling.handlers;
import com.baeldung.reactive.errorhandling.NameRequiredException;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
@Component
public class Handler4 {
public Mono<ServerResponse> handleRequest4(ServerRequest request) {
return ServerResponse.ok()
.body(sayHello(request)
.onErrorResume(e ->
Mono.error(new NameRequiredException(
HttpStatus.BAD_REQUEST, "please provide a name", e))), String.class);
}
private Mono<String> sayHello(ServerRequest request) {
try {
return Mono.just("Hello, " + request.queryParam("name").get());
} catch (Exception e) {
return Mono.error(e);
}
}
}

View File

@ -1,21 +0,0 @@
package com.baeldung.reactive.errorhandling.handlers;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
@Component
public class Handler5 {
public Mono<ServerResponse> handleRequest5(ServerRequest request) {
return ServerResponse.ok()
.body(sayHello(request), String.class);
}
private Mono<String> sayHello(ServerRequest request) {
return Mono.just("Hello, " + request.queryParam("name").get());
}
}

View File

@ -1,21 +0,0 @@
package com.baeldung.reactive.errorhandling.routers;
import com.baeldung.reactive.errorhandling.handlers.Handler1;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
@Component
public class Router1 {
@Bean
public RouterFunction<ServerResponse> routeRequest1(Handler1 handler) {
return RouterFunctions.route(RequestPredicates.GET("/api/endpoint1")
.and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), handler::handleRequest1);
}
}

View File

@ -1,21 +0,0 @@
package com.baeldung.reactive.errorhandling.routers;
import com.baeldung.reactive.errorhandling.handlers.Handler2;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
@Component
public class Router2 {
@Bean
public RouterFunction<ServerResponse> routeRequest2(Handler2 handler) {
return RouterFunctions.route(RequestPredicates.GET("/api/endpoint2")
.and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), handler::handleRequest2);
}
}

View File

@ -1,21 +0,0 @@
package com.baeldung.reactive.errorhandling.routers;
import com.baeldung.reactive.errorhandling.handlers.Handler3;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
@Component
public class Router3 {
@Bean
public RouterFunction<ServerResponse> routeRequest3(Handler3 handler) {
return RouterFunctions.route(RequestPredicates.GET("/api/endpoint3")
.and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), handler::handleRequest3);
}
}

View File

@ -1,21 +0,0 @@
package com.baeldung.reactive.errorhandling.routers;
import com.baeldung.reactive.errorhandling.handlers.Handler4;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
@Component
public class Router4 {
@Bean
public RouterFunction<ServerResponse> routeRequest4(Handler4 handler) {
return RouterFunctions.route(RequestPredicates.GET("/api/endpoint4")
.and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), handler::handleRequest4);
}
}

View File

@ -1,21 +0,0 @@
package com.baeldung.reactive.errorhandling.routers;
import com.baeldung.reactive.errorhandling.handlers.Handler5;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
@Component
public class Router5 {
@Bean
public RouterFunction<ServerResponse> routeRequest5(Handler5 handler) {
return RouterFunctions.route(RequestPredicates.GET("/api/endpoint5")
.and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), handler::handleRequest5);
}
}

View File

@ -7,19 +7,13 @@ import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWeb
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;
import java.io.IOException;
import static org.junit.Assert.assertEquals;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@DirtiesContext
@WithMockUser
@AutoConfigureWebTestClient(timeout = "10000")
public class ErrorHandlingIntegrationTest {
@ -27,150 +21,107 @@ public class ErrorHandlingIntegrationTest {
private WebTestClient webTestClient;
@Test
public void givenErrorReturn_whenUsernamePresent_thenOk() throws IOException {
String s = webTestClient.get()
.uri("/api/endpoint1?name={username}", "Tony")
.accept(MediaType.TEXT_PLAIN)
.exchange()
.returnResult(String.class)
.getResponseBody()
.blockFirst();
assertEquals("Hello, Tony", s);
}
@Test
public void givenErrorReturn_whenNoUsername_thenOk() throws IOException {
String s = webTestClient.get()
.uri("/api/endpoint1")
.accept(MediaType.TEXT_PLAIN)
.exchange()
.returnResult(String.class)
.getResponseBody()
.blockFirst();
assertEquals("Hello, Stranger", s);
}
@Test
public void givenResumeFallback_whenUsernamePresent_thenOk() throws IOException {
String s = webTestClient.get()
.uri("/api/endpoint2?name={username}", "Tony")
.accept(MediaType.TEXT_PLAIN)
.exchange()
.returnResult(String.class)
.getResponseBody()
.blockFirst();
assertEquals("Hello, Tony", s);
}
@Test
public void givenResumeFallback_whenNoUsername_thenOk() throws IOException {
String s = webTestClient.get()
.uri("/api/endpoint2")
.accept(MediaType.TEXT_PLAIN)
.exchange()
.returnResult(String.class)
.getResponseBody()
.blockFirst();
assertEquals("Hello, Stranger", s);
}
@Test
public void givenResumeDynamicValue_whenUsernamePresent_thenOk() throws IOException {
String s = webTestClient.get()
.uri("/api/endpoint3?name={username}", "Tony")
.accept(MediaType.TEXT_PLAIN)
.exchange()
.returnResult(String.class)
.getResponseBody()
.blockFirst();
assertEquals("Hello, Tony", s);
}
@Test
public void givenResumeDynamicValue_whenNoUsername_thenOk() throws IOException {
String s = webTestClient.get()
.uri("/api/endpoint3")
.accept(MediaType.TEXT_PLAIN)
.exchange()
.returnResult(String.class)
.getResponseBody()
.blockFirst();
assertEquals("Hi, I looked around for your name but found: No value present", s);
}
@Test
public void givenResumeRethrow_whenUsernamePresent_thenOk() throws IOException {
String s = webTestClient.get()
.uri("/api/endpoint4?name={username}", "Tony")
.accept(MediaType.TEXT_PLAIN)
.exchange()
.returnResult(String.class)
.getResponseBody()
.blockFirst();
assertEquals("Hello, Tony", s);
}
@Test
public void givenResumeRethrow_whenNoUsername_thenOk() throws IOException {
public void givenErrorReturn_whenUsernamePresent_thenOk() {
webTestClient.get()
.uri("/api/endpoint4")
.accept(MediaType.TEXT_PLAIN)
.exchange()
.expectStatus()
.isBadRequest()
.expectHeader()
.contentType(MediaType.APPLICATION_JSON)
.expectBody()
.jsonPath("$.message")
.isNotEmpty()
.jsonPath("$.message")
.isEqualTo("please provide a name");
.uri("/api/endpoint1?name={username}", "Tony")
.accept(MediaType.TEXT_PLAIN)
.exchange()
.expectBody(String.class).isEqualTo("Hello, Tony");
}
@Test
public void givenGlobalErrorHandling_whenUsernamePresent_thenOk() throws IOException {
public void givenErrorReturn_whenNoUsername_thenOk() {
String s = webTestClient.get()
.uri("/api/endpoint5?name={username}", "Tony")
.accept(MediaType.TEXT_PLAIN)
.exchange()
.returnResult(String.class)
.getResponseBody()
.blockFirst();
assertEquals("Hello, Tony", s);
}
@Test
public void givenGlobalErrorHandling_whenNoUsername_thenOk() throws IOException {
webTestClient.get()
.uri("/api/endpoint5")
.accept(MediaType.TEXT_PLAIN)
.exchange()
.expectStatus()
.isBadRequest()
.expectHeader()
.contentType(MediaType.APPLICATION_JSON)
.expectBody()
.jsonPath("$.message")
.isNotEmpty()
.jsonPath("$.message")
.isEqualTo("please provide a name");
.uri("/api/endpoint1")
.accept(MediaType.TEXT_PLAIN)
.exchange()
.expectBody(String.class).isEqualTo("Hello, Stranger");
}
@Test
public void givenResumeFallback_whenUsernamePresent_thenOk() {
webTestClient.get()
.uri("/api/endpoint2?name={username}", "Tony")
.accept(MediaType.TEXT_PLAIN)
.exchange()
.expectBody(String.class).isEqualTo("Hello, Tony");
}
@Test
public void givenResumeFallback_whenNoUsername_thenOk() {
webTestClient.get()
.uri("/api/endpoint2")
.accept(MediaType.TEXT_PLAIN)
.exchange()
.expectBody(String.class).isEqualTo("Hello, Stranger");
}
@Test
public void givenResumeDynamicValue_whenUsernamePresent_thenOk() {
webTestClient.get()
.uri("/api/endpoint3?name={username}", "Tony")
.accept(MediaType.TEXT_PLAIN)
.exchange()
.expectBody(String.class).isEqualTo("Hello, Tony");
}
@Test
public void givenResumeDynamicValue_whenNoUsername_thenOk() {
webTestClient.get()
.uri("/api/endpoint3")
.accept(MediaType.TEXT_PLAIN)
.exchange()
.expectBody(String.class).isEqualTo("Hi, I looked around for your name but found: No value present");
}
@Test
public void givenResumeRethrow_whenUsernamePresent_thenOk() {
webTestClient.get()
.uri("/api/endpoint4?name={username}", "Tony")
.accept(MediaType.TEXT_PLAIN)
.exchange()
.expectBody(String.class).isEqualTo("Hello, Tony");
}
@Test
public void givenResumeRethrow_whenNoUsername_thenOk() {
webTestClient.get()
.uri("/api/endpoint4")
.accept(MediaType.TEXT_PLAIN)
.exchange()
.expectStatus().isBadRequest()
.expectHeader().contentType(MediaType.APPLICATION_JSON)
.expectBody().jsonPath("$.message").isEqualTo("please provide a name");
}
@Test
public void givenGlobalErrorHandling_whenUsernamePresent_thenOk() {
webTestClient.get()
.uri("/api/endpoint5?name={username}", "Tony")
.accept(MediaType.TEXT_PLAIN)
.exchange()
.expectBody(String.class).isEqualTo("Hello, Tony");
}
@Test
public void givenGlobalErrorHandling_whenNoUsername_thenOk() {
webTestClient.get()
.uri("/api/endpoint5")
.accept(MediaType.TEXT_PLAIN)
.exchange()
.expectStatus().isBadRequest()
.expectHeader().contentType(MediaType.APPLICATION_JSON)
.expectBody().jsonPath("$.message").isEqualTo("please provide a name");
}
}