forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
How to return 404 with Spring WebFlux (eugenp#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
- Loading branch information
Showing
4 changed files
with
216 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.baeldung</groupId> | ||
<artifactId>spring-5-webflux</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<name>spring-5-webflux</name> | ||
|
||
<url>http://www.baeldung.com</url> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
<spring-boot.version>2.0.2.RELEASE</spring-boot.version> | ||
</properties> | ||
|
||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-dependencies</artifactId> | ||
<version>${spring-boot.version}</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-webflux</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.8.0</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<version>2.22.1</version> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
64 changes: 64 additions & 0 deletions
64
...-5-webflux/src/main/java/com/baeldung/spring/responsestatus/ResponseStatusController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 changes: 12 additions & 0 deletions
12
...g-5-webflux/src/main/java/com/baeldung/spring/responsestatus/SpringResponseStatusApp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 changes: 70 additions & 0 deletions
70
...bflux/src/test/java/com/baeldung/spring/responsestatus/ResponseStatusControllerTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |