DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Spring 5 Web Reactive: Flux, Mono, and JUnit Testing

Trending

  • Supercharge Your Communication With Twilio and Ballerina
  • The Promise of Personal Data for Better Living
  • Parallel Sort
  • Introduction to ESP32 for Beginners Using the Xedge32 Lua IDE

Spring-Reactive Samples: Mono and Single

Join us as we dive into more exploration into Spring's native support for reactive programming.

Biju Kunjummen user avatar by
Biju Kunjummen
·
Oct. 02, 16 · Tutorial
Like (2)
Save
Tweet
Share
18.74K Views

Join the DZone community and get the full member experience.

Join For Free

This is just a little bit of a learning from my previous post, where I had tried out Spring's native support for reactive programming.

Just to quickly recap, my objective was to develop a reactive service that takes in a request that looks like this:

{
    "id":1,
    "delay_by": 2000,
    "payload": "Hello",
    "throw_exception": false
}


And returns a response along these lines:

{
    "id": "1",
    "received": "Hello",
    "payload": "Response Message"
}


I had demonstrated this in two ways that the upcoming Spring's reactive model supports — using the Rector-Core Flux type as a return type and using Rx-java Observable type.

However, the catch with these types is that the response would look something like this:

[{"id":"1","received":"Hello","payload":"From RxJavaService"}]


Essentially an array, and the reason is obvious — Flux and Observable represent zero or more asynchronous emissions, and so Spring Reactive Web has to represent such a result as an array. 

The fix to return the expected JSON is to essentially return a type which represents 1 value — such a type is the Mono in Reactor-Core OR a Single in Rx-Java. Both these types are as capable as their multi-valued counterparts in providing functions which combine and transform their elements.

So with this change, the controller signature with Mono looks like this:

@RequestMapping(path = "/handleMessageReactor", method = RequestMethod.POST)
public Mono<MessageAcknowledgement> handleMessage(@RequestBody Message message) {
    return this.aService.handleMessageMono(message);
}


And with Single like this:

@RequestMapping(path = "/handleMessageRxJava", method = RequestMethod.POST)
public Single<MessageAcknowledgement> handleMessage(@RequestBody Message message) {
    return this.aService.handleMessageSingle(message);
}


I have the sample code in my GitHub repo.

Mono (software)

Published at DZone with permission of Biju Kunjummen, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Spring 5 Web Reactive: Flux, Mono, and JUnit Testing

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: