Reactive REST API Using RxJava and Spring Boot

Reactive REST API Using RxJava and Spring Boot

Before you continue reading, I expect you understand how to create simple REST API using Spring Boot and RxJava.

I’m not going to explain what reactive programming is or why you should use it. I hope you’ve already read about it somewhere, and if not, you can Google it. In this post, I’m going to tell you how to use reactive programming specifically with Spring Boot and RxJava. Let's get started.


Before you continue reading, I expect you understand how to create simple REST API using Spring Boot and RxJava. If you haven’t, you can learn more about Spring Boot on Baeldung and you can learn more about RxJava on AndroidHive. They explain those two materials really well.

Reactive REST API

The reactive REST API to be built is just a simple CRUD with authors and books. Here are the endpoints:

 [POST] /api/authors  → add an author

 [POST] /api/books  → add a book

 [PUT] /api/books/{bookId}  → update a book

 [GET] /api/books?limit={limit}&page={page}  → get list of books

 [GET] /api/book/{bookId}  → get a book’s detail

 [DELETE] /api/book/{bookId}  → delete a book


Open your pom.xml and add these dependencies.

    <!--IMPORTANT!!! ADD THIS DEPENDENCY TO SOLVE HttpMediaNotAcceptableException-->

P.S. Keep in mind that you have to add the dependency in line 19–23. If you don’t add it as a dependency, you’ll get HttpMediaNotAcceptableException every time you hit the reactive API. As you can see, I also added mockito as a dependency for mocking objects in unit tests. But I’ll cover the unit testing in another article.


The repository layer is just a regular JPA repository. Below is an example of the BookRepository.

public interface BookRepository extends JpaRepository<Book, String> {
    List<Book> findAllByAuthorId(String authorId);


For the service layer, the return value is not just regular data types, but I wrapped them inside RxJava’s Single. For example, the code below handles the addition of a new book.

    public Single<String> addBook(AddBookRequest addBookRequest) {
        return saveBookToRepository(addBookRequest);
    private Single<String> saveBookToRepository(AddBookRequest addBookRequest) {
        return Single.create(singleSubscriber -> {
            Optional<Author> optionalAuthor = authorRepository.findById(addBookRequest.getAuthorId());
            if (!optionalAuthor.isPresent())
                singleSubscriber.onError(new EntityNotFoundException());
            else {
                String addedBookId =;
    private Book toBook(AddBookRequest addBookRequest) {
        Book book = new Book();
        BeanUtils.copyProperties(addBookRequest, book);
        return book;

As you can see, the return value of the addBook method is a String wrapped inside RxJava’s Single.


            consumes = MediaType.APPLICATION_JSON_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE
    public Single<ResponseEntity<BaseWebResponse>> addBook(@RequestBody AddBookWebRequest addBookWebRequest) {
        return bookService.addBook(toAddBookRequest(addBookWebRequest))
                .map(s -> ResponseEntity.created(URI.create("/api/books/" + s)).body(BaseWebResponse.successNoData()));
    private AddBookRequest toAddBookRequest(AddBookWebRequest addBookWebRequest) {
        AddBookRequest addBookRequest = new AddBookRequest();
        BeanUtils.copyProperties(addBookWebRequest, addBookRequest);
        return addBookRequest;

In the web layer, it just forwards the request to the corresponding service, as shown above for handling the addition of a new book.

Just For Fun

Just for fun, I limited the threads down to 10 threads (default 200 threads) by adding this line of code inside


Then, I did load testing using Apache Benchmark for the API (both blocking and non-blocking or reactive). I did 10k requests with 100 concurrency requests for the [POST] /api/books endpoint.

Blocking API

Load testing for the blocking version of the API

Non-Blocking/Reactive API

Load testing for the non-blocking/reactive version of the API

The End

The whole codes (+ unit tests) can be found on GitHub.

Thanks for reading !

spring-boot java

What's new in Bootstrap 5 and when Bootstrap 5 release date?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Random Password Generator Online

HTML Color Picker online | HEX Color Picker | RGB Color Picker

Java Spring Boot First App

Step by Step to your First Spring App

Spring Boot Tutorials - Spring Boot Full Course

Spring Boot Tutorials | Full Course - What is Spring? Spring Boot is an open source Java-based framework used to create a Micro Service. Spring Boot contains a comprehensive infrastructure support for developing a micro service and enables you to develop enterprise-ready applications that you can “just run”.

Spring Boot Tutorial For Beginner- Spring Boot Full Course

This "Spring Boot Tutorial For Beginner- Spring Boot Full Course" video will help you learn Spring Boot Framework from scratch with examples. This Spring Tutorial is ideal for both beginners as well as professionals who want to master the Spring Boot Framework

Full-stack Reactive Java with Spring Framework, Spring Boot and Project Reactor

Reactive programming offers Java developers a way to build message-driven, elastic, resilient, and responsive services...yet many Java developers don't know where to begin.

Spring Boot Tutorial:Create Java Login Web App using Spring Security and Eclipse

Spring Boot Tutorial:Create Java Login Web App using Spring Security and Eclipse. A comprehensive step by step Java tutorial on creating Java login web app using Spring Boot, Web MVC, Security, Data, JDBC, Eclipse, and Spring Tools. In thi...