Open In App

How to Return 404 with Spring WebFlux?

The Spring Reactive allows developers to build Asynchronous, Non-Blocking, and Event-Driven Web Applications. When compared with the Spring MVC framework the Spring Reactive Web framework provides more functionality. And we can easily manage the web applications through this Spring Reactive Framework. If we want to use this framework, we need to integrate Spring Web flux in our dependencies File.

In this article, we will see how to Return a 404 error status code with Spring WebFlux.



The 404 came while no data was found on the server, The HTTP protocol returns this error code as output. The 404 error is returned by the HTTP protocol when a server cannot find the requested resource.

Prerequisites:

To understand how a flux works in reactive streams, below we have listed some topics:



Implementation Steps to Return 404 Error Status Code with Spring WebFlux

Below is the implementation to get a 404 error status code with Spring WebFlux.

Step 1: Add Dependencies

To access the Spring Reactive, we need add below Dependencies in our Spring Boot project.

dependencies {
// Spring WebFlux
implementation 'org.springframework.boot:spring-boot-starter-webflux'
// Reactor Core
implementation 'io.projectreactor:reactor-core'
// Spring Boot Starter Test (if needed)
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

Step 2: Java Class Creation

After creation of Spring stater project with above mentioned dependencies, we will create one Java class in project main package. This class is worked as RestController for handling API end points. For Creating RestController we need use @RestController annotation in Spring WebFlux.




package com.reactive.app;
  
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
  
import reactor.core.publisher.Mono;
  
@RestController
public class RestControllerClass {
  
    @GetMapping("/api/resource")
    public Mono<ResponseEntity<String>> getResource(@RequestParam(required = false) String id) {
        // Check if resource exists
        if (id == null || id.isEmpty() || resourceNotFound(id)) {
            // Return 404 response
            return Mono.just(ResponseEntity.notFound().build());
        }
  
        // If resource exists, return it
        return Mono.just(ResponseEntity.ok("Resource with ID " + id));
    }
  
    // Dummy method to check if resource exists
    private boolean resourceNotFound(String id) {
        return true;
    }
}

Explanation of the Code:

Step 3: getResource API

Here, we have created one API with HTTP GET method by using @GetMapping annotation. Then we have used Mono Publisher. This publisher takes ResponseEntity as return type and it returns a publisher in the form of ResponseEntity with HTTP status code. This ResponseEntity take class as String to handle User ID.




@GetMapping("/api/resource")
    public Mono<ResponseEntity<String>> getResource(@RequestParam(required = false) String id) {
        // Check if resource exists
        if (id == null || id.isEmpty() || resourceNotFound(id)) {
            // Return 404 response
            return Mono.just(ResponseEntity.notFound().build());
        }
  
        // If resource exists, return it
        return Mono.just(ResponseEntity.ok("Resource with ID " + id));
    }

Step 4: resourceNotFound method

Here, we have created one boolean method to handle user id. This method takes user id as argument through method signature. Then we return the true value in any case. This method is called in API endpoint, we can observe this in the API code.

private boolean resourceNotFound(String id) {
return true;
}

Output:

After running this project as Spring Boot API, we have tested this API end point using PostMan tool. After successfully tested API endpoint, we got expected output that is Return 404 error.

Below is the output image for your reference.


Article Tags :