NoSQL databases represent a new challenge when compared to relational databases. Combined with Reactive Programming Paradigm implemented by Spring Webflux fully supporting Reactive Streams is a game changer on how to take advantage of using asynchronous stream processing with non-blocking back pressure, dealing with a document-based database, offering significant gains of performance for those applications that must support thousands of simultaneous requests.
- Overview
- Requirements
- Project Structure
- Howto Build and Run
- Screenshot
- Links
- Built with
- Code Snippet
- Continued development
- Useful resources
- Author
- Portfolio
This API offers reactive endpoints to return Posts and Comments associated to a User according to this the JSON schema:
{
_id: ObjectId('67b0edf01a432d53fce1d509'),
moment: ISODate('2025-02-13T14:50:00.000Z'),
title: 'Business Trip',
body: 'Travel to Sampa',
author: {
name: 'Maria Brown'
},
comments: [
{
text: 'Have a nice and safe trip',
moment: ISODate('2025-02-13T14:55:00.000Z'),
author: {
name: 'Alex Green'
}
},
{
text: 'Enjoy it!',
moment: ISODate('2025-02-13T14:59:00.000Z'),
author: {
name: 'Bob Grey'
}
}
],
_class: 'br.dev.ferreiras.mongodb.models.entities.Post'
}
The API has been coded using Java 21, Spring Boot 3.4.2, MongoDB 4.4.2, Maven, Javadoc, Reactive Spring MongoDB,
Spring Webflux and Docker.
- docs
- javadocs
- src
- main
- java
- br.dev.ferreiras.mongodb
- config
- controller
- handlers
- dto
- model
- entities
- dto
- embedded
- mapper
- repository
- services
- exceptions
- br.dev.ferreiras.mongodb
- resources
- application-test.properties
- application-properties
- test
- MongoDB Database : http://127.0.0.1:27017
- profile active: test
- service socket: 127.0.0.1:8080
- tweak a few knobs to get it up and running
- Live Site URL: API MongoDB
/**
*
* @author ricardo@ferreiras.dev.br
* @version 1.1.030901
* @since 1.0
*
*/
@Repository
public interface PostRepository extends ReactiveMongoRepository<Post, String> {
@Query("""
{
'title': { $regex: ?0, $options: 'i' }
}
""")
Flux<Post> searchByTitle(String text);
@Query(
"""
{
$and: [
{ 'moment': { $gte: ?1 } }, { 'moment': { $lte: ?2 } }, {
$or: [
{'title': { $regex: ?0, $options: 'i' } },
{'body': { $regex: ?0, $options: 'i' } },
{'comments.text': { $regex: ?0, $options: 'i' } }
]
}
]
}
""")
Flux<Post> fullSearch(String text, Instant startMoment, Instant endMoment);
Flux<Post> findByTitleContainingIgnoreCase(String text);
}
- Unit Tests
- HATEOAS
- Versioning
- Swagger Docs
- [https://spring.io/] Awesome Java framework!.
- [https://start.spring.io/] Handy startup tool.
- [https://mvnrepository.com/] Tools that help tackle the beast
- [https://docs.spring.io/spring-data/mongodb/reference/] MongoDB reference doc
- [https://www.reactive-streams.org/] Reactive Streams
- [https://docs.spring.io/spring-framework/reference/web/webflux.html/] Spring Webflux