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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
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

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Spring Data: Data Auditing Using JaVers and MongoDB
  • Developing a Multi-Tenancy Application With Spring Security and JWTs
  • Frequently Used Annotations in Spring Boot Applications
  • Enterprise RIA With Spring 3, Flex 4 and GraniteDS

Trending

  • Scalability 101: How to Build, Measure, and Improve It
  • The Role of Artificial Intelligence in Climate Change Mitigation
  • Mastering React App Configuration With Webpack
  • Streamlining Event Data in Event-Driven Ansible
  1. DZone
  2. Coding
  3. Frameworks
  4. Spring Boot Reactive Tutorial

Spring Boot Reactive Tutorial

Let's make use of Spring 5's Reactive capabilities to make a simple Reactive, asynchronous web app using Spring Boot and other Spring tools.

By 
Mohit Sinha user avatar
Mohit Sinha
·
Aug. 30, 17 · Tutorial
Likes (54)
Comment
Save
Tweet
Share
122.3K Views

Join the DZone community and get the full member experience.

Join For Free

1. Overview

Spring 5, which will release later this year, will support building asynchronous and Reactive applications.

This is a simple tutorial showing the new features in Spring and how to create a web application. The application will connect to a database, have basic authentication, and be Reactive.

2. Reactive Programming

Reactive programming is about building asynchronous, non-blocking, and event-driven applications that can easily scale.

Each event is published to subscribers while ensuring that the subscribers are never overwhelmed.

Mono and Flux are implementations of the Publisher interface. A Flux will observe 0 to N items and eventually terminate successfully or not. A Mono will observe 0 or 1 item, with Mono<Void> hinting at most 0 items.

To learn more about Reactive Programming, you can refer to this article.

3. Dependencies

We'll use Gradle to build our project. I recommend using Spring Initializr for bootstrapping your project.

We'll use:

  • Spring Boot 2
  • Spring Webflux
  • Spring Reactive Data MongoDB
  • Spring Security Reactive Webflux
  • Lombok

Not all the Spring libraries have a stable release yet.

Lombok is used to reduce boilerplate code for models and POJOs. It can generate setters/getters, default constructors, toString, etc. methods automatically.

buildscript {
    ext {
        springBootVersion = '2.0.0.M2'
    }
    ...
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter-data-mongodb-reactive')
    compile('org.springframework.boot:spring-boot-starter-webflux')
    compile('org.springframework.security:spring-security-core')
    compile('org.springframework.security:spring-security-config')
    compile('org.springframework.security:spring-security-webflux')
    compileOnly('org.projectlombok:lombok')
    ...
}


4. Auto-Configuration

We'll leave Spring Boot to automatically configure our application based on the dependencies added.

@SpringBootApplication
@EnableReactiveMongoRepositories
@EnableWebFluxSecurity
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}


For using non-default values in our application configuration, we can specify them as properties and Spring Boot will automatically use them to create beans.

spring.data.mongodb.database=demo


All beans necessary for MongoDB, Web, and Security will be automatically created.

5. Database

We'll be using MongoDB in our example and a simple POJO. A PersonRepository bean will be created automatically.

@Data
@NoArgsConstructor
@Document
public class Person {
    @Id 
    private String id;
    private String name;
}

public interface PersonRespository extends ReactiveMongoRepository<Person, String> {
    Flux<Person> findByName(String name);
}


6. Web API

We'll create REST endpoints for Person.

Spring 5 added support for creating routes functionally while still supporting the traditional, annotation-based way of creating them.

Let's look at both of them with the help of examples.

6.1. Annotation-Based

This is the traditional way of creating endpoints.

@RestController
@RequestMapping("/person")
public class PersonController {

    @Autowired
    private PersonRespository personRespository;

    @GetMapping
    public Flux<Person> index() {
        return personRespository.findAll();
    }
}


This will create a REST endpoint, which will return all the Person records reactively.

6.2. Router Functions

This is a new and concise way of creating endpoints.

@Bean
RouterFunction<?> routes(PersonRespository personRespository) {
    return nest(path("/person"),

            route(RequestPredicates.GET("/{id}"),
                request -> ok().body(personRespository.findById(request.pathVariable("id")), Person.class))

            .andRoute(method(HttpMethod.POST),
                request -> {
                    personRespository.insert(request.bodyToMono(Person.class)).subscribe();
                    return ok().build();
        })
    );
}


The nest method is used to create nested routes, where a group of routes share a common path (prefix), header, or other RequestPredicate.

So, in our case, all the corresponding routes have the common prefix /person.

In the first route, we have exposed a GET API /person/{id}, which will retrieve the corresponding record and return it.

In the second route, we have exposed a POST API /person, which will receive a Person object and save it in the DB.

The cURL commands for the same:

curl http://localhost:8080/person -v -u tom:password

curl http://localhost:8080/person/{id} -v -u tom:password

curl http://localhost:8080/person -X POST -d '{"name":"John Doe","age":20}' -H "Content-Type: application/json" -v -u tom:password


We should define the routes in a Spring configuration file.

7. Security

We'll be using a very simple basic authentication mechanism in our example.

@Bean
UserDetailsRepository userDetailsRepository() {
    UserDetails tom = withUsername("tom").password("password").roles("USER").build();
    UserDetails harry = withUsername("harry").password("password").roles("USER", "ADMIN").build();
    return new MapUserDetailsRepository(tom, harry);
}


We have added some users for our application and assigned different roles to them.

8. Conclusion

I have tried explaining, with a simple example, how to build a simple Reactive web application using Spring Boot.

You can read more about:

  • Spring Web Reactive

  • Spring Data Reactive

  • Spring Functional Web Framework

You can find the complete example on GitHub.

Spring Framework Spring Boot Web application Database Spring Security

Published at DZone with permission of Mohit Sinha, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Spring Data: Data Auditing Using JaVers and MongoDB
  • Developing a Multi-Tenancy Application With Spring Security and JWTs
  • Frequently Used Annotations in Spring Boot Applications
  • Enterprise RIA With Spring 3, Flex 4 and GraniteDS

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • 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:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!