How to return 404 with Spring WebFlux (#6509)
* feat(response-status): return response http status * fix(style): improve code style * BAEL-2716: How to return 404 with Spring WebFlux * style: apply baeldung intellij formatter * Delete .gitignore * config: remove gitignore & remove unused maven plugin
This commit is contained in:
+64
@@ -0,0 +1,64 @@
|
||||
package com.baeldung.spring.responsestatus;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
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 reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
|
||||
|
||||
@RequestMapping("/statuses")
|
||||
@RestController
|
||||
public class ResponseStatusController {
|
||||
|
||||
@GetMapping(value = "/ok", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||
public Flux<String> ok() {
|
||||
return Flux.just("ok");
|
||||
}
|
||||
|
||||
@GetMapping(value = "/no-content", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||
@ResponseStatus(HttpStatus.NO_CONTENT)
|
||||
public Flux<String> noContent() {
|
||||
return Flux.empty();
|
||||
}
|
||||
|
||||
@GetMapping(value = "/accepted", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||
public Flux<String> accepted(ServerHttpResponse response) {
|
||||
response.setStatusCode(HttpStatus.ACCEPTED);
|
||||
return Flux.just("accepted");
|
||||
}
|
||||
|
||||
@GetMapping(value = "/bad-request")
|
||||
public Mono<String> badRequest() {
|
||||
return Mono.error(new IllegalArgumentException());
|
||||
}
|
||||
|
||||
@ResponseStatus(value = HttpStatus.BAD_REQUEST, reason = "Illegal arguments")
|
||||
@ExceptionHandler(IllegalArgumentException.class)
|
||||
public void illegalArgument() {
|
||||
|
||||
}
|
||||
|
||||
@GetMapping(value = "/unauthorized")
|
||||
public ResponseEntity<Mono<String>> unathorized() {
|
||||
return ResponseEntity
|
||||
.status(HttpStatus.UNAUTHORIZED)
|
||||
.header("X-Reason", "user-invalid")
|
||||
.body(Mono.just("unauthorized"));
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RouterFunction<ServerResponse> notFound() {
|
||||
return RouterFunctions.route(GET("/statuses/not-found"), request -> ServerResponse
|
||||
.notFound()
|
||||
.build());
|
||||
}
|
||||
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.spring.responsestatus;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringResponseStatusApp {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringResponseStatusApp.class, args);
|
||||
}
|
||||
}
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
package com.baeldung.spring.responsestatus;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
public class ResponseStatusControllerTests {
|
||||
|
||||
@Autowired
|
||||
private WebTestClient testClient;
|
||||
|
||||
@Test
|
||||
public void whenCallRest_thenStatusIsOk() {
|
||||
testClient.get()
|
||||
.uri("/statuses/ok")
|
||||
.exchange()
|
||||
.expectStatus()
|
||||
.isOk();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallRest_thenStatusIsNoContent() {
|
||||
testClient.get()
|
||||
.uri("/statuses/no-content")
|
||||
.exchange()
|
||||
.expectStatus()
|
||||
.isNoContent();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallRest_thenStatusIsAccepted() {
|
||||
testClient.get()
|
||||
.uri("/statuses/accepted")
|
||||
.exchange()
|
||||
.expectStatus()
|
||||
.isAccepted();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallRest_thenStatusIsBadRequest() {
|
||||
testClient.get()
|
||||
.uri("/statuses/bad-request")
|
||||
.exchange()
|
||||
.expectStatus()
|
||||
.isBadRequest();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallRest_thenStatusIsUnauthorized() {
|
||||
testClient.get()
|
||||
.uri("/statuses/unauthorized")
|
||||
.exchange()
|
||||
.expectStatus()
|
||||
.isUnauthorized();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallRest_thenStatusIsNotFound() {
|
||||
testClient.get()
|
||||
.uri("/statuses/not-found")
|
||||
.exchange()
|
||||
.expectStatus()
|
||||
.isNotFound();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user