Reactive REST API Using Spring Boot and RxJava

Reactive REST API Using Spring Boot and RxJava

Check out this post to learn more about reactive programming with Spring Boot and RxJava.

Originally published by Axellageraldinc A  at

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.


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.

The End

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

Originally published by Axellageraldinc A  at


Thanks for reading :heart: If you liked this post, share it with all of your programming buddies! Follow me on Facebook | Twitter

☞ Java Programming Masterclass for Software Developers

☞ JSP, Servlets and JDBC for Beginners: Build a Database App

☞ Java 8 New Features In Simple Way

☞ Java In-Depth: Become a Complete Java Engineer!

☞ Spring & Hibernate for Beginners (includes Spring Boot)

☞ Spring Framework Master Class - Learn Spring the Modern Way!

☞ Spring Boot and OAuth2: Getting the Authorization Code

☞ An Introduction to Spring Boot

☞ Build a Rest API with Spring Boot using MySQL and JPA

☞ Angular 8 + Spring Boot 2.2: Build a CRUD App Today!

☞ Spring Boot vs. Spring MVC vs. Spring: How Do They Compare?

☞ Top 4 Spring Annotations for Java Developer in 2019

java spring-boot rest

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

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

Spring Boot Java Tutorial - REST API using PostgreSQL and JWT

Learn how to create a Rest API using Spring Boot. Learn to use Spring Boot and Java to create a complete REST API. We will use PostgreSQL as the relational database and Spring JdbcTemplate for interacting with that. Apart from this, we will add authentication using JWT (JSON Web Tokens).

Java REST API Comparison: Micronaut, Quarkus, and Spring Boot

Learn how to create simple REST APIs with popular Java frameworks: Micronaut, Quarkus, and Spring Boot.

How to Create a Rest API with Java in Spring

We all know there are multiple ways to create a Rest API. You can use Flask, Django, Rails or Sinatra to do so, but this article will focus on creating said restful API endpoint using Java, Spring Boot and Spring Data Rest.

How to Install OpenJDK 11 on CentOS 8

What is OpenJDK? OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE).

Java Spring Boot First App

Step by Step to your First Spring App