Support ResponseStatusException in WebFlux fn
This commit introduces support for the ResponseStatusException in the functional web framework. Issue: SPR-15344
This commit is contained in:
@@ -16,9 +16,11 @@
|
||||
|
||||
package org.springframework.web.reactive.function.server;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
@@ -28,20 +30,41 @@ import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.RequestEntity;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.client.ClientHttpResponse;
|
||||
import org.springframework.web.client.ResponseErrorHandler;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.web.reactive.function.BodyExtractors.*;
|
||||
import static org.springframework.web.reactive.function.BodyInserters.*;
|
||||
import static org.springframework.web.reactive.function.server.RequestPredicates.*;
|
||||
import static org.springframework.web.reactive.function.server.RouterFunctions.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.springframework.web.reactive.function.BodyExtractors.toMono;
|
||||
import static org.springframework.web.reactive.function.BodyInserters.fromPublisher;
|
||||
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
|
||||
import static org.springframework.web.reactive.function.server.RequestPredicates.POST;
|
||||
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class PublisherHandlerFunctionIntegrationTests extends AbstractRouterFunctionIntegrationTests {
|
||||
|
||||
private final RestTemplate restTemplate = new RestTemplate();
|
||||
private RestTemplate restTemplate;
|
||||
|
||||
@Before
|
||||
public void createRestTemplate() {
|
||||
restTemplate = new RestTemplate();
|
||||
restTemplate.setErrorHandler(new ResponseErrorHandler() {
|
||||
@Override
|
||||
public boolean hasError(ClientHttpResponse response) throws IOException {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleError(ClientHttpResponse response) throws IOException {
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
@@ -49,7 +72,9 @@ public class PublisherHandlerFunctionIntegrationTests extends AbstractRouterFunc
|
||||
PersonHandler personHandler = new PersonHandler();
|
||||
return route(GET("/mono"), personHandler::mono)
|
||||
.and(route(POST("/mono"), personHandler::postMono))
|
||||
.and(route(GET("/flux"), personHandler::flux));
|
||||
.and(route(GET("/flux"), personHandler::flux))
|
||||
.and(route(GET("/throwRSE"), personHandler::throwResponseStatusException))
|
||||
.and(route(GET("/returnRSE"), personHandler::returnResponseStatusException));
|
||||
}
|
||||
|
||||
|
||||
@@ -86,6 +111,19 @@ public class PublisherHandlerFunctionIntegrationTests extends AbstractRouterFunc
|
||||
assertEquals("Jack", result.getBody().getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void responseStatusException() {
|
||||
ResponseEntity<String> result =
|
||||
restTemplate.getForEntity("http://localhost:" + port + "/throwRSE", String.class);
|
||||
|
||||
assertEquals(HttpStatus.BAD_REQUEST, result.getStatusCode());
|
||||
|
||||
result = restTemplate.getForEntity("http://localhost:" + port + "/returnRSE", String.class);
|
||||
|
||||
assertEquals(HttpStatus.BAD_REQUEST, result.getStatusCode());
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static class PersonHandler {
|
||||
|
||||
@@ -105,6 +143,14 @@ public class PublisherHandlerFunctionIntegrationTests extends AbstractRouterFunc
|
||||
return ServerResponse.ok().body(
|
||||
fromPublisher(Flux.just(person1, person2), Person.class));
|
||||
}
|
||||
|
||||
public Mono<ServerResponse> throwResponseStatusException(ServerRequest request) {
|
||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Bad Request");
|
||||
}
|
||||
|
||||
public Mono<ServerResponse> returnResponseStatusException(ServerRequest request) {
|
||||
return Mono.error(new ResponseStatusException(HttpStatus.BAD_REQUEST, "Bad Request"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user