Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

WebFlux Annotations Tutorial

Introduction to WebFlux Annotations

Spring WebFlux is a reactive web framework that is part of the Spring Framework. It is designed to handle asynchronous and non-blocking web applications using the reactive programming paradigm. Annotations in WebFlux help define controllers and mapping of requests in a declarative way. This tutorial will cover the key annotations used in WebFlux, their purposes, and how to use them effectively.

@RestController Annotation

The @RestController annotation is a convenience annotation that combines @Controller and @ResponseBody. It indicates that the class is a controller where every method returns a domain object instead of a view.

@RestController
public class MyController {
    
    @GetMapping("/hello")
    public String sayHello() {
        return "Hello, World!";
    }
}

In the above example, a simple REST controller is created with a single GET endpoint that returns a greeting message.

@GetMapping, @PostMapping, @PutMapping, and @DeleteMapping Annotations

These annotations are specialized variants of @RequestMapping that are used to handle HTTP GET, POST, PUT, and DELETE requests, respectively. They make the code cleaner and easier to read.

@RestController
public class UserController {

    @GetMapping("/users")
    public Flux getAllUsers() {
        return userService.getAllUsers();
    }

    @PostMapping("/users")
    public Mono createUser(@RequestBody User user) {
        return userService.createUser(user);
    }
}

In this example, getAllUsers responds to GET requests and returns a reactive stream of users, while createUser handles POST requests to create a new user.

@RequestBody Annotation

The @RequestBody annotation is used to bind the HTTP request body to a Java object. It is commonly used in methods that handle POST and PUT requests.

@PostMapping("/users")
public Mono createUser(@RequestBody User user) {
    return userService.createUser(user);
}

Here, the createUser method takes a User object from the request body and processes it.

@PathVariable Annotation

The @PathVariable annotation is used to extract values from the URI path. It allows you to define dynamic segments in your URI.

@GetMapping("/users/{id}")
public Mono getUserById(@PathVariable String id) {
    return userService.getUserById(id);
}

In this example, the getUserById method retrieves a user based on the ID provided in the URI.

@RequestParam Annotation

The @RequestParam annotation is used to extract query parameters from the request URL. It can also provide default values.

@GetMapping("/users")
public Flux getUsersByAge(@RequestParam(defaultValue = "0") int age) {
    return userService.getUsersByAge(age);
}

The getUsersByAge method retrieves users filtered by age, with a default value of 0 if no age is specified.

Conclusion

Spring WebFlux offers a powerful set of annotations that simplify the development of reactive web applications. Understanding and effectively using these annotations allows developers to build scalable and maintainable applications. This tutorial covered the essential annotations and provided examples to illustrate their usage. For more advanced topics, consider exploring reactive programming concepts and how they integrate with WebFlux.