Skip to content

rnhc1000/springMongoDB

Repository files navigation

Reactive RESTFul API - MongoDB

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.

Table of contents

Overview

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'
}

Features

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.

Project Structure

  • docs
    • javadocs
  • src
    • main
    • java
      • br.dev.ferreiras.mongodb
        • config
        • controller
          • handlers
        • dto
        • model
          • entities
          • dto
          • embedded
        • mapper
        • repository
        • services
          • exceptions
    • resources
      • application-test.properties
      • application-properties
    • test

Howto Build and Run

- 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

Screenshot

Links

Built with

My Skills

Code Snippet

/**
 *
 * @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);
}

Continued development

  • Unit Tests
  • HATEOAS
  • Versioning
  • Swagger Docs

Useful resources

Author

Ricardo Ferreira

- Portfolio

My Portfolio...

Releases

No releases published

Packages

No packages published