Blocking is a feature of classic servlet-based web frameworks like Spring MVC. Introduced in Spring 5, Spring WebFlux is a reactive framework that operates on servers like Netty and is completely non-blocking.
Two programming paradigms are supported by Spring WebFlux. Annotations (Aspect Oriented Programming) and WebFlux.fn (Functional Programming).
"Spring WebFlux includes WebFlux.fn, a lightweight functional programming model in which functions are used to route and handle requests and contracts are designed for immutability. It is an alternative to the annotation-based programming model but otherwise runs on the same Reactive Core foundation." Spring | Functional Endpoints
As the title describe, this is a simple Songs API build using Spring, Docker and MongoDB, the endpoints are Functional Endpoints and will have the traditional ControllerAdvice as Exception handler.
Talking XML these are the project dependencies:
First, let's setup the docker compose file /compose.yaml
of the project (it should generated by spring via the docker support starter).
With that set, let's create the Song class:
The SongRepository interface will be referring to the Song class in its DB ops:
Now, it's time for the Song Router, it will be responsible for router the incoming requests for the /songs ressource:
As you noticed the request are redirected to the SongHandler for a certain logic to be performed.
The SongsHandler will act as Service as well, will perform a business logic and communicate with the SongRepository for operations with the database.
As previously states, will be using the same old ControllerAdvice as Exception handler with two custom Exceptions as the following:
With all that been set, let's make use of our endpoint using Postman:
Sorry not a big fan of Madonna tbh :|
With that said, our functional songs endpoint will be good to go for further improvements and new features. This is simple, in real industrial projects, I can assure you it can get complicated with more layers, for "getting started" purposes I avoided the use of advanced concepts such as validation, DTO, etc.
You can find the full source code here
Blossoming Intelligence: How to Run Spring AI Locally with Ollama
In this short article, we'll look at how easy it is to create a chat bot backend powered by Spring and Olama using the llama 3 model.
Sat, 11th May 2024
Read MoreGetting started with native java apps with GraalVM
Native Image is a technology to ahead-of-time compile Java code to a standalone executable, called a native image. This executable includes the application classes, classes from its dependencies, runtime library classes, and statically linked native code from JDK.
Wed, 10th April 2024
Read MoreNextJs meets Redux: A simple user PoC
Redux is a powerful state management library primarily used in JavaScript applications, particularly those built with frameworks like React. At its core, Redux provides a predictable state container for managing the state of an application in a more organised and centralised manner. It operates on a unidirectional data flow model, which helps in maintaining the consistency of application state and facilitates easier debugging and testing.
Thu, 15th February 2024
Read MoreMonitor Spring reactive microservices with Prometheus and Grafana: a how-to guide
Micro-services monitoring is a crucial aspect of managing modern, complex software architectures. Unlike traditional monolithic applications, micro-services break down functionality into smaller, independent services that can be developed, deployed, and scaled independently.
Fri, 27th October 2023
Read MoreHands on Reactive Spring with Redis Cache and Docker support
The concept of reactive programming enables more responsive and scalable programmes by handling asynchronous data streams.
Mon, 28th August 2023
Read More